feat: wire main loop with periodic sync+scan and sync_interval config

Replace compute-only run() with an immediate sync+scan followed by a
ticker-driven periodic loop, add the sync.interval config field (default
6h) with defaults and validation, and add tests for the scheduling logic.
This commit is contained in:
2026-07-19 22:27:06 +03:00
parent dc4bdcdab0
commit 0635ca8a87
6 changed files with 313 additions and 30 deletions

View File

@@ -15,6 +15,7 @@ type Config struct {
MusicBrainz MusicBrainzConfig `yaml:"musicbrainz"`
Telegram TelegramConfig `yaml:"telegram"`
Scanner ScannerConfig `yaml:"scanner"`
Sync SyncConfig `yaml:"sync"`
}
// ServerConfig holds HTTP server settings.
@@ -46,6 +47,15 @@ type TelegramConfig struct {
CronSchedule string `yaml:"cron_schedule"`
}
// SyncConfig holds periodic sync pipeline settings.
type SyncConfig struct {
Interval time.Duration `yaml:"interval"`
}
// DefaultSyncInterval is the default period between full sync+scan runs when
// sync.interval is not specified in the config file.
const DefaultSyncInterval = 6 * time.Hour
// ScannerConfig holds scanner engine parameters.
//
// Type filtering is handled in musicbrainz/api.go, not here: only Album/Single/EP
@@ -95,6 +105,9 @@ func applyDefaults(cfg *Config) {
if cfg.MusicBrainz.CacheTTL == 0 {
cfg.MusicBrainz.CacheTTL = 24 * time.Hour
}
if cfg.Sync.Interval == 0 {
cfg.Sync.Interval = DefaultSyncInterval
}
}
// validate checks that required fields are set and values are within acceptable ranges.
@@ -117,5 +130,8 @@ func validate(cfg *Config) error {
if cfg.MusicBrainz.UserAgent == "" {
return fmt.Errorf("musicbrainz.user_agent is required")
}
if cfg.Sync.Interval <= 0 {
return fmt.Errorf("sync.interval must be positive, got %v", cfg.Sync.Interval)
}
return nil
}