diff options
Diffstat (limited to 'workhorse/main.go')
-rw-r--r-- | workhorse/main.go | 24 |
1 files changed, 21 insertions, 3 deletions
diff --git a/workhorse/main.go b/workhorse/main.go index 47ab63a875a..de282b2c670 100644 --- a/workhorse/main.go +++ b/workhorse/main.go @@ -1,6 +1,7 @@ package main import ( + "context" "flag" "fmt" "io/ioutil" @@ -8,6 +9,7 @@ import ( "net/http" _ "net/http/pprof" "os" + "os/signal" "syscall" "time" @@ -102,7 +104,7 @@ func buildConfig(arg0 string, args []string) (*bootConfig, *config.Config, error fset.UintVar(&cfg.APILimit, "apiLimit", 0, "Number of API requests allowed at single time") fset.UintVar(&cfg.APIQueueLimit, "apiQueueLimit", 0, "Number of API requests allowed to be queued") fset.DurationVar(&cfg.APIQueueTimeout, "apiQueueDuration", queueing.DefaultTimeout, "Maximum queueing duration of requests") - fset.DurationVar(&cfg.APICILongPollingDuration, "apiCiLongPollingDuration", 50, "Long polling duration for job requesting for runners (default 50s - enabled)") + fset.DurationVar(&cfg.APICILongPollingDuration, "apiCiLongPollingDuration", 50, "Long polling duration for job requesting for runners") fset.BoolVar(&cfg.PropagateCorrelationID, "propagateCorrelationID", false, "Reuse existing Correlation-ID from the incoming request header `X-Request-ID` if present") if err := fset.Parse(args); err != nil { @@ -144,6 +146,7 @@ func buildConfig(arg0 string, args []string) (*bootConfig, *config.Config, error cfg.ObjectStorageCredentials = cfgFromFile.ObjectStorageCredentials cfg.ImageResizerConfig = cfgFromFile.ImageResizerConfig cfg.AltDocumentRoot = cfgFromFile.AltDocumentRoot + cfg.ShutdownTimeout = cfgFromFile.ShutdownTimeout return boot, cfg, nil } @@ -225,7 +228,22 @@ func run(boot bootConfig, cfg config.Config) error { up := wrapRaven(upstream.NewUpstream(cfg, accessLogger)) - go func() { finalErrors <- http.Serve(listener, up) }() + done := make(chan os.Signal, 1) + signal.Notify(done, syscall.SIGINT, syscall.SIGTERM) - return <-finalErrors + server := http.Server{Handler: up} + go func() { finalErrors <- server.Serve(listener) }() + + select { + case err := <-finalErrors: + return err + case sig := <-done: + log.WithFields(log.Fields{"shutdown_timeout_s": cfg.ShutdownTimeout.Duration.Seconds(), "signal": sig.String()}).Infof("shutdown initiated") + + ctx, cancel := context.WithTimeout(context.Background(), cfg.ShutdownTimeout.Duration) // lint:allow context.Background + defer cancel() + + redis.Shutdown() + return server.Shutdown(ctx) + } } |