From 05e3b3397c888807719d70af4ed3c73397d9374f Mon Sep 17 00:00:00 2001 From: xengineering Date: Sat, 11 Feb 2023 18:17:01 +0100 Subject: Move shutdown code to main.go This has nothing to do with the database. Because the db is now a global pointer the shutdown code can live in main.go. --- database.go | 13 ------------- main.go | 16 ++++++++++++++++ 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/database.go b/database.go index ea11cfa..5d9181c 100644 --- a/database.go +++ b/database.go @@ -7,11 +7,9 @@ import ( "path/filepath" "io" "os" - "os/signal" "os/user" "os/exec" "strconv" - "syscall" "database/sql" _ "github.com/go-sql-driver/mysql" @@ -41,17 +39,6 @@ func setupDatabase() *sql.DB { migrate(db) - // allow graceful shutdown - var listener = make(chan os.Signal) - signal.Notify(listener, syscall.SIGTERM) - signal.Notify(listener, syscall.SIGINT) - go func() { - signal := <-listener - log.Printf("\nGot signal '%+v'. Shutting down ...\n", signal) - dbCleanup(db) - os.Exit(0) // TODO this does not belong to a database - write utils file 'shutdown.go' - }() - log.Printf("Connected to database: %s\n", target) return db diff --git a/main.go b/main.go index 405ff0a..c1e0571 100644 --- a/main.go +++ b/main.go @@ -3,7 +3,10 @@ package main import ( "log" + "os" + "os/signal" "database/sql" + "syscall" ) var config RuntimeConfig @@ -13,5 +16,18 @@ func main() { log.Printf("Started Ceres recipe server.\n") config = GetRuntimeConfig() db = setupDatabase() + provideShutdown() runServer() } + +func provideShutdown() { + var listener = make(chan os.Signal) + signal.Notify(listener, syscall.SIGTERM) + signal.Notify(listener, syscall.SIGINT) + go func() { + signal := <-listener + log.Printf("Got signal '%+v'. Shutdown is started.\n", signal) + dbCleanup(db) + os.Exit(0) + }() +} -- cgit v1.2.3-70-g09d2