diff --git a/scnserver/db/database.go b/scnserver/db/database.go index 506a615..f6ca5a4 100644 --- a/scnserver/db/database.go +++ b/scnserver/db/database.go @@ -85,3 +85,15 @@ func (db *Database) Ping(ctx context.Context) error { func (db *Database) BeginTx(ctx context.Context) (sq.Tx, error) { return db.db.BeginTransaction(ctx, sql.LevelDefault) } + +func (db *Database) Stop(ctx context.Context) error { + _, err := db.db.Exec(ctx, "PRAGMA wal_checkpoint;", sq.PP{}) + if err != nil { + return err + } + err = db.db.Exit() + if err != nil { + return err + } + return nil +} diff --git a/scnserver/go.mod b/scnserver/go.mod index dc9c5a5..0c51b88 100644 --- a/scnserver/go.mod +++ b/scnserver/go.mod @@ -8,7 +8,7 @@ require ( github.com/mattn/go-sqlite3 v1.14.16 github.com/rs/zerolog v1.28.0 github.com/swaggo/swag v1.8.7 - gogs.mikescher.com/BlackForestBytes/goext v0.0.41 + gogs.mikescher.com/BlackForestBytes/goext v0.0.42 ) require ( diff --git a/scnserver/go.sum b/scnserver/go.sum index 76937c9..5d51038 100644 --- a/scnserver/go.sum +++ b/scnserver/go.sum @@ -120,6 +120,8 @@ gogs.mikescher.com/BlackForestBytes/goext v0.0.40 h1:wh5+IRmcMAbwJ8cK6JZvg71IiMU gogs.mikescher.com/BlackForestBytes/goext v0.0.40/go.mod h1:/u9JtMwCP68ix4R9BJ/MT0Lm+QScmqIoyYZFKBGzv9g= gogs.mikescher.com/BlackForestBytes/goext v0.0.41 h1:3p/MtkHZ2gulSdizXql3VnFf2v7WpeOBCmTi0rQYCQw= gogs.mikescher.com/BlackForestBytes/goext v0.0.41/go.mod h1:/u9JtMwCP68ix4R9BJ/MT0Lm+QScmqIoyYZFKBGzv9g= +gogs.mikescher.com/BlackForestBytes/goext v0.0.42 h1:u6+pDRrL9wSvJG7gVsGUO4dA54qzac5LsqoXqi6oo9E= +gogs.mikescher.com/BlackForestBytes/goext v0.0.42/go.mod h1:/u9JtMwCP68ix4R9BJ/MT0Lm+QScmqIoyYZFKBGzv9g= golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 h1:/UOmuWzQfxxo9UtlXMwuQU8CMgg1eZXqTRwkSQJWKOI= golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s= diff --git a/scnserver/logic/application.go b/scnserver/logic/application.go index ff61f15..da43612 100644 --- a/scnserver/logic/application.go +++ b/scnserver/logic/application.go @@ -91,15 +91,15 @@ func (app *Application) Run() { errChan <- httpserver.Serve(ln) }() - stop := make(chan os.Signal, 1) - signal.Notify(stop, os.Interrupt, syscall.SIGTERM) + sigstop := make(chan os.Signal, 1) + signal.Notify(sigstop, os.Interrupt, syscall.SIGTERM) for _, job := range app.Jobs { job.Start() } select { - case <-stop: + case <-sigstop: ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) defer cancel() @@ -135,6 +135,13 @@ func (app *Application) Run() { job.Stop() } + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) + defer cancel() + err := app.Database.Stop(ctx) + if err != nil { + log.Info().Err(err).Msg("Error while stopping the database") + } + app.IsRunning.Set(false) }