async calls return the call_id/task_id

This commit is contained in:
Seif Lotfy
2016-10-01 04:04:51 +02:00
parent d8801d5be7
commit a8535120da
3 changed files with 39 additions and 7 deletions

29
api/common/ctx.go Normal file
View File

@@ -0,0 +1,29 @@
package common
import (
"github.com/Sirupsen/logrus"
"golang.org/x/net/context"
)
// WithLogger stores the logger.
func WithLogger(ctx context.Context, l logrus.FieldLogger) context.Context {
return context.WithValue(ctx, "logger", l)
}
// Logger returns the structured logger.
func Logger(ctx context.Context) logrus.FieldLogger {
l, ok := ctx.Value("logger").(logrus.FieldLogger)
if !ok {
return logrus.StandardLogger()
}
return l
}
// Attempt at simplifying this whole logger in the context thing
// Could even make this take a generic map, then the logger that gets returned could be used just like the stdlib too, since it's compatible
func LoggerWithFields(ctx context.Context, fields logrus.Fields) (context.Context, logrus.FieldLogger) {
l := Logger(ctx)
l = l.WithFields(fields)
ctx = WithLogger(ctx, l)
return ctx, l
}

View File

@@ -14,16 +14,16 @@ import (
"github.com/Sirupsen/logrus"
"github.com/gin-gonic/gin"
"github.com/iron-io/functions/api/common"
"github.com/iron-io/functions/api/models"
"github.com/iron-io/functions/api/runner"
titancommon "github.com/iron-io/worker/common"
"github.com/iron-io/worker/runner/drivers"
"github.com/satori/go.uuid"
)
func handleSpecial(c *gin.Context) {
ctx := c.MustGet("ctx").(context.Context)
log := titancommon.Logger(ctx)
log := common.Logger(ctx)
err := Api.UseSpecialHandlers(c)
if err != nil {
@@ -39,12 +39,12 @@ func handleRequest(c *gin.Context, enqueue models.Enqueue) {
}
ctx := c.MustGet("ctx").(context.Context)
log := titancommon.Logger(ctx)
log := common.Logger(ctx)
reqID := uuid.NewV5(uuid.Nil, fmt.Sprintf("%s%s%d", c.Request.RemoteAddr, c.Request.URL.Path, time.Now().Unix())).String()
c.Set("reqID", reqID) // todo: put this in the ctx instead of gin's
log = log.WithFields(logrus.Fields{"request_id": reqID})
ctx, log = common.LoggerWithFields(ctx, logrus.Fields{"call_id": reqID})
var err error
@@ -107,7 +107,7 @@ func handleRequest(c *gin.Context, enqueue models.Enqueue) {
log.WithField("routes", routes).Debug("Got routes from datastore")
for _, el := range routes {
log = log.WithFields(logrus.Fields{
"app": appName, "route": el.Path, "image": el.Image, "request_id": reqID})
"app": appName, "route": el.Path, "image": el.Image})
if params, match := matchRoute(el.Path, route); match {
@@ -156,15 +156,17 @@ func handleRequest(c *gin.Context, enqueue models.Enqueue) {
var result drivers.RunResult
switch el.Type {
case "async":
// TODO: Create Task
// Create Task
priority := int32(0)
task := &models.Task{}
task.Image = &cfg.Image
task.ID = cfg.ID
task.RouteName = cfg.AppName
task.Priority = &priority
// TODO: Push to queue
// Push to queue
enqueue(task)
log.Info("Added new task to queue")
default:
if result, err = Api.Runner.Run(c, cfg); err != nil {
break

View File

@@ -86,6 +86,7 @@ func (s *Server) UseSpecialHandlers(ginC *gin.Context) error {
func (s *Server) handleRunnerRequest(c *gin.Context) {
enqueue := func(task *models.Task) (*models.Task, error) {
c.JSON(http.StatusAccepted, map[string]string{"call_id": task.ID})
return s.MQ.Push(task)
}
handleRequest(c, enqueue)