server: shutdown endpoint (#466)

fixes #390
This commit is contained in:
C Cirello
2016-12-29 20:19:42 +01:00
committed by GitHub
parent c2c2a0fb06
commit e3c85d3e7e
5 changed files with 42 additions and 3 deletions

View File

@@ -33,7 +33,7 @@ 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) *Server {
func New(ctx context.Context, ds models.Datastore, mq models.MessageQueue, r *runner.Runner, tasks chan task.Request, enqueue models.Enqueue, opts ...ServerOption) *Server {
s := &Server{
Runner: r,
Router: gin.New(),
@@ -45,6 +45,10 @@ func New(ctx context.Context, ds models.Datastore, mq models.MessageQueue, r *ru
s.Router.Use(prepareMiddleware(ctx))
for _, opt := range opts {
opt(s)
}
return s
}

View File

@@ -0,0 +1,11 @@
package server
import "context"
type ServerOption func(*Server)
func EnableShutdownEndpoint(halt context.CancelFunc) ServerOption {
return func(s *Server) {
s.Router.GET("/shutdown", s.handleShutdown(halt))
}
}

15
api/server/shutdown.go Normal file
View File

@@ -0,0 +1,15 @@
package server
import (
"context"
"net/http"
"github.com/gin-gonic/gin"
)
func (s *Server) handleShutdown(halt context.CancelFunc) func(*gin.Context) {
return func(c *gin.Context) {
halt()
c.JSON(http.StatusOK, "shutting down")
}
}

View File

@@ -2,12 +2,13 @@ package server
import (
"context"
"testing"
"github.com/iron-io/functions/api/datastore"
"github.com/iron-io/functions/api/models"
"github.com/iron-io/functions/api/mqs"
"github.com/iron-io/functions/api/runner"
"github.com/iron-io/functions/api/runner/task"
"testing"
)
type testSpecialHandler struct{}

10
main.go
View File

@@ -92,7 +92,15 @@ func main() {
})
svr.AddFunc(func(ctx context.Context) {
srv := server.New(ctx, ds, mq, rnr, tasks, server.DefaultEnqueue)
srv := server.New(
ctx,
ds,
mq,
rnr,
tasks,
server.DefaultEnqueue,
server.EnableShutdownEndpoint(halt),
)
srv.Run()
<-ctx.Done()
})