From 63634361cef535afc66356a6c3ba7ac63652c02b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Tue, 18 Jul 2023 16:33:01 +0200 Subject: [PATCH] Manager: make periodic database integrity check configurable Instead of always performing the periodic integrity check, make it possible to disable it or run it at different intervals. Currently for the Blender Studio it's crunch time, so the check should really only run when there is someone looking at the system (i.e. at restarts for upgrade purposes). --- cmd/flamenco-manager/main.go | 4 +++- internal/manager/config/config.go | 7 +++++-- internal/manager/config/defaults.go | 1 + internal/manager/persistence/integrity.go | 20 +++++++++++++++----- 4 files changed, 24 insertions(+), 8 deletions(-) diff --git a/cmd/flamenco-manager/main.go b/cmd/flamenco-manager/main.go index 572559ec..bc92f214 100644 --- a/cmd/flamenco-manager/main.go +++ b/cmd/flamenco-manager/main.go @@ -195,7 +195,9 @@ func runFlamencoManager() bool { wg.Add(1) go func() { defer wg.Done() - persist.PeriodicIntegrityCheck(mainCtx, mainCtxCancel) + persist.PeriodicIntegrityCheck(mainCtx, + configService.Get().DBIntegrityCheck, + mainCtxCancel) }() // Start the web server. diff --git a/internal/manager/config/config.go b/internal/manager/config/config.go index ec581873..66e6cc5d 100644 --- a/internal/manager/config/config.go +++ b/internal/manager/config/config.go @@ -73,8 +73,11 @@ type Base struct { Meta ConfMeta `yaml:"_meta"` ManagerName string `yaml:"manager_name"` - DatabaseDSN string `yaml:"database"` - Listen string `yaml:"listen"` + + DatabaseDSN string `yaml:"database"` + DBIntegrityCheck time.Duration `yaml:"database_check_period"` + + Listen string `yaml:"listen"` SSDPDiscovery bool `yaml:"autodiscoverable"` diff --git a/internal/manager/config/defaults.go b/internal/manager/config/defaults.go index 2468467f..fb72a889 100644 --- a/internal/manager/config/defaults.go +++ b/internal/manager/config/defaults.go @@ -20,6 +20,7 @@ var defaultConfig = Conf{ Listen: ":8080", // ListenHTTPS: ":8433", DatabaseDSN: "flamenco-manager.sqlite", + DBIntegrityCheck: 1 * time.Hour, SSDPDiscovery: true, LocalManagerStoragePath: "./flamenco-manager-storage", SharedStoragePath: "", // Empty string means "first run", and should trigger the config setup assistant. diff --git a/internal/manager/persistence/integrity.go b/internal/manager/persistence/integrity.go index b79e84d5..2a87eb4d 100644 --- a/internal/manager/persistence/integrity.go +++ b/internal/manager/persistence/integrity.go @@ -14,7 +14,6 @@ var ErrIntegrity = errors.New("database integrity check failed") const ( integrityCheckTimeout = 2 * time.Second - integrityCheckPeriod = 1 * time.Hour ) type PragmaIntegrityCheckResult struct { @@ -30,15 +29,26 @@ type PragmaForeignKeyCheckResult struct { // PeriodicIntegrityCheck periodically checks the database integrity. // This function only returns when the context is done. -func (db *DB) PeriodicIntegrityCheck(ctx context.Context, onErrorCallback func()) { - log.Debug().Msg("database: periodic integrity check loop starting") - defer log.Debug().Msg("database: periodic integrity check loop stopping") +func (db *DB) PeriodicIntegrityCheck( + ctx context.Context, + period time.Duration, + onErrorCallback func(), +) { + if period == 0 { + log.Info().Msg("database: periodic integrity check disabled") + return + } + + log.Info(). + Stringer("period", period). + Msg("database: periodic integrity check starting") + defer log.Debug().Msg("database: periodic integrity check stopping") for { select { case <-ctx.Done(): return - case <-time.After(integrityCheckPeriod): + case <-time.After(period): } ok := db.performIntegrityCheck(ctx)