Cleanup main (#470)

* main: clean up

* server: replace magical constants and use them for app name tracking
This commit is contained in:
Travis Reeder
2017-01-03 14:59:26 -08:00
committed by GitHub
parent e75e115180
commit 1c8d12b09e
18 changed files with 203 additions and 129 deletions

View File

@@ -9,13 +9,23 @@ import (
"path"
"github.com/Sirupsen/logrus"
"github.com/ccirello/supervisor"
"github.com/gin-gonic/gin"
"github.com/iron-io/functions/api"
"github.com/iron-io/functions/api/models"
"github.com/iron-io/functions/api/runner"
"github.com/iron-io/functions/api/runner/task"
"github.com/iron-io/runner/common"
)
const (
EnvLogLevel = "log_level"
EnvMQURL = "mq_url"
EnvDBURL = "db_url"
EnvPort = "port" // be careful, Gin expects this variable to be "port"
EnvAPIURL = "api_url"
)
type Server struct {
Datastore models.Datastore
Runner *runner.Runner
@@ -23,6 +33,8 @@ type Server struct {
MQ models.MessageQueue
Enqueue models.Enqueue
apiURL string
specialHandlers []SpecialHandler
appCreateListeners []AppCreateListener
appUpdateListeners []AppUpdateListener
@@ -33,14 +45,26 @@ type Server struct {
singleflight singleflight // singleflight assists Datastore
}
func New(ctx context.Context, ds models.Datastore, mq models.MessageQueue, r *runner.Runner, tasks chan task.Request, enqueue models.Enqueue, opts ...ServerOption) *Server {
func New(ctx context.Context, ds models.Datastore, mq models.MessageQueue, apiURL string, opts ...ServerOption) *Server {
metricLogger := runner.NewMetricLogger()
funcLogger := runner.NewFuncLogger()
rnr, err := runner.New(ctx, funcLogger, metricLogger)
if err != nil {
logrus.WithError(err).Fatalln("Failed to create a runner")
return nil
}
tasks := make(chan task.Request)
s := &Server{
Runner: r,
Runner: rnr,
Router: gin.New(),
Datastore: ds,
MQ: mq,
tasks: tasks,
Enqueue: enqueue,
Enqueue: DefaultEnqueue,
apiURL: apiURL,
}
s.Router.Use(prepareMiddleware(ctx))
@@ -57,11 +81,11 @@ func prepareMiddleware(ctx context.Context) gin.HandlerFunc {
ctx, _ := common.LoggerWithFields(ctx, extractFields(c))
if appName := c.Param("app"); appName != "" {
ctx = context.WithValue(ctx, "appName", appName)
c.Set(api.AppName, appName)
}
if routePath := c.Param("route"); routePath != "" {
ctx = context.WithValue(ctx, "routePath", routePath)
c.Set(api.Path, routePath)
}
c.Set("ctx", ctx)
@@ -120,12 +144,36 @@ func extractFields(c *gin.Context) logrus.Fields {
return fields
}
func (s *Server) Run() {
func (s *Server) Start(ctx context.Context) {
s.bindHandlers()
s.startGears(ctx)
close(s.tasks)
}
func (s *Server) startGears(ctx context.Context) {
svr := &supervisor.Supervisor{
MaxRestarts: supervisor.AlwaysRestart,
Log: func(msg interface{}) {
logrus.Debug("supervisor: ", msg)
},
}
// By default it serves on :8080 unless a
// PORT environment variable was defined.
go s.Router.Run()
svr.AddFunc(func(ctx context.Context) {
go s.Router.Run()
<-ctx.Done()
})
svr.AddFunc(func(ctx context.Context) {
runner.StartWorkers(ctx, s.Runner, s.tasks)
})
svr.AddFunc(func(ctx context.Context) {
runner.RunAsyncRunner(ctx, s.apiURL, s.tasks, s.Runner)
})
svr.Serve(ctx)
}
func (s *Server) bindHandlers() {