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() +}