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)