mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
fn: graceful shutdown adjustments (#498)
Graceful shutdown should wait for Shutdown call instead of ListenAndServe. This is because ListenAndServe returns immediately upon Shutdown call.
This commit is contained in:
committed by
Reed Allman
parent
ef43e2c772
commit
af8eed098d
@@ -41,7 +41,7 @@ func init() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func contextWithSignal(ctx context.Context, signals ...os.Signal) context.Context {
|
func contextWithSignal(ctx context.Context, signals ...os.Signal) (context.Context, context.CancelFunc) {
|
||||||
newCTX, halt := context.WithCancel(ctx)
|
newCTX, halt := context.WithCancel(ctx)
|
||||||
c := make(chan os.Signal, 1)
|
c := make(chan os.Signal, 1)
|
||||||
signal.Notify(c, signals...)
|
signal.Notify(c, signals...)
|
||||||
@@ -59,5 +59,5 @@ func contextWithSignal(ctx context.Context, signals ...os.Signal) context.Contex
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
return newCTX
|
return newCTX, halt
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -254,11 +254,11 @@ func extractFields(c *gin.Context) logrus.Fields {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) Start(ctx context.Context) {
|
func (s *Server) Start(ctx context.Context) {
|
||||||
ctx = contextWithSignal(ctx, os.Interrupt)
|
newctx, cancel := contextWithSignal(ctx, os.Interrupt)
|
||||||
s.startGears(ctx)
|
s.startGears(newctx, cancel)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) startGears(ctx context.Context) {
|
func (s *Server) startGears(ctx context.Context, cancel context.CancelFunc) {
|
||||||
// By default it serves on :8080 unless a
|
// By default it serves on :8080 unless a
|
||||||
// PORT environment variable was defined.
|
// PORT environment variable was defined.
|
||||||
listen := fmt.Sprintf(":%d", viper.GetInt(EnvPort))
|
listen := fmt.Sprintf(":%d", viper.GetInt(EnvPort))
|
||||||
@@ -281,13 +281,21 @@ func (s *Server) startGears(ctx context.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
<-ctx.Done() // listening for signals...
|
err := server.ListenAndServe()
|
||||||
server.Shutdown(context.Background()) // we can wait
|
if err != nil && err != http.ErrServerClosed {
|
||||||
|
logrus.WithError(err).Error("server error")
|
||||||
|
cancel()
|
||||||
|
} else {
|
||||||
|
logrus.Info("server stopped")
|
||||||
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
err := server.ListenAndServe()
|
// listening for signals or listener errors...
|
||||||
if err != nil {
|
<-ctx.Done()
|
||||||
logrus.WithError(err).Error("error opening server")
|
|
||||||
|
// TODO: do not wait forever during graceful shutdown (add graceful shutdown timeout)
|
||||||
|
if err := server.Shutdown(context.Background()); err != nil {
|
||||||
|
logrus.WithError(err).Error("server shutdown error")
|
||||||
}
|
}
|
||||||
|
|
||||||
s.Agent.Close() // after we stop taking requests, wait for all tasks to finish
|
s.Agent.Close() // after we stop taking requests, wait for all tasks to finish
|
||||||
|
|||||||
Reference in New Issue
Block a user