From a3f38d8edf1614d753d55d0ed2e6b887bafe99bd Mon Sep 17 00:00:00 2001 From: SmallCoccinelle <89733524+SmallCoccinelle@users.noreply.github.com> Date: Tue, 7 Sep 2021 06:28:40 +0200 Subject: [PATCH] When stopping, close the database (#1686) * When stopping, close the database This patch is likely to cause errornous behavior in the application. This is due to the fact that the application doesn't gracefully shut down, but is forcefully terminated. However, the purpose is to uncover what needs to be done, to make it a more graceful shutdown. --- main.go | 6 ++++++ pkg/database/database.go | 7 +++++++ pkg/manager/manager.go | 7 +++++++ 3 files changed, 20 insertions(+) diff --git a/main.go b/main.go index e08271a7f..653438e01 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,7 @@ import ( "syscall" "github.com/stashapp/stash/pkg/api" + "github.com/stashapp/stash/pkg/logger" "github.com/stashapp/stash/pkg/manager" _ "github.com/golang-migrate/migrate/v4/database/sqlite3" @@ -21,6 +22,11 @@ func main() { // stop any profiling at exit defer pprof.StopCPUProfile() blockForever() + + err := manager.GetInstance().Shutdown() + if err != nil { + logger.Errorf("Error when closing: %s", err) + } } func blockForever() { diff --git a/pkg/database/database.go b/pkg/database/database.go index c992601d8..50735b95d 100644 --- a/pkg/database/database.go +++ b/pkg/database/database.go @@ -89,6 +89,13 @@ func Initialize(databasePath string) error { return nil } +func Close() error { + WriteMu.Lock() + defer WriteMu.Unlock() + + return DB.Close() +} + func open(databasePath string, disableForeignKeys bool) *sqlx.DB { // https://github.com/mattn/go-sqlite3 url := "file:" + databasePath + "?_journal=WAL" diff --git a/pkg/manager/manager.go b/pkg/manager/manager.go index 638332619..6581ec3fe 100644 --- a/pkg/manager/manager.go +++ b/pkg/manager/manager.go @@ -378,3 +378,10 @@ func (s *singleton) GetSystemStatus() *models.SystemStatus { ConfigPath: &configFile, } } + +// Shutdown gracefully stops the manager +func (s *singleton) Shutdown() error { + // TODO: Each part of the manager needs to gracefully stop at some point + // for now, we just close the database. + return database.Close() +}