From fb1f250a74e50b5966946f6083fd8e973d34ffef Mon Sep 17 00:00:00 2001 From: jholdstock Date: Wed, 20 Sep 2023 09:38:50 +0100 Subject: [PATCH] vspd: Create shutdown context earlier. Create the shutdown context earlier so process begins handling shutdown signals earlier. Specifically, the context should be created before creating any resources which need a graceful shutdown such as the database. This is important because without explicit signal handling the process will terminate immediately upon receiving a signal without running deferred tasks. --- cmd/vspd/main.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cmd/vspd/main.go b/cmd/vspd/main.go index 94357c1..dd34d03 100644 --- a/cmd/vspd/main.go +++ b/cmd/vspd/main.go @@ -45,6 +45,10 @@ func run() int { log := cfg.logger("VSP") + // Create a context that is canceled when a shutdown request is received + // through an interrupt signal such as SIGINT (Ctrl+C). + ctx := signal.ShutdownListener(log) + defer log.Criticalf("Shutdown complete") log.Criticalf("Version %s (Go version %s %s/%s)", version.String(), runtime.Version(), runtime.GOOS, runtime.GOARCH) @@ -107,10 +111,6 @@ func run() int { // WaitGroup for services to signal when they have shutdown cleanly. var shutdownWg sync.WaitGroup - // Create a context that is canceled when a shutdown request is received - // through an interrupt signal such as SIGINT (Ctrl+C). - ctx := signal.ShutdownListener(log) - // Start the webapi server. shutdownWg.Add(1) go func() {