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

@@ -8,6 +8,7 @@ import (
"os"
"os/signal"
"syscall"
"time"
"naviwatcher/internal/config"
"naviwatcher/internal/database"
@@ -22,6 +23,10 @@ type App struct {
db *database.DB
mbClient *musicbrainz.MusicBrainzClient
ndClient *navidrome.NavidromeClient
// syncFn, when non-nil, replaces the real syncAndScan call in
// startPeriodicSync so tests can observe the loop without live clients.
syncFn func(ctx context.Context) error
}
// navidromeClientFactory constructs the Navidrome client. It is a package-level
@@ -114,16 +119,50 @@ func (a *App) Close() {
}
func (a *App) run(ctx context.Context) error {
// Compute-only scanner hook: scan all monitored artists for missing
// releases and log the count. Notifier/Web UI are out of scope for this
// plan, so results are only logged. ScanAll is a blocking DB walk over
// every monitored artist; it observes ctx cancellation and returns early.
missing, err := scanner.ScanAll(ctx, a.db, a.cfg.Scanner.FuzzyThreshold)
if err != nil {
// Run an immediate sync+scan so the service produces results without
// waiting a full interval, then kick off the periodic loop goroutine.
// Business logic added in later tasks (notifier, web server) will be
// wired as additional goroutines below.
if err := a.doSync(ctx); err != nil {
if ctx.Err() != nil {
// Context cancelled (e.g. shutdown) — exit cleanly.
return nil
}
log.Printf("Initial sync+scan failed: %v", err)
}
a.startPeriodicSync(ctx)
<-ctx.Done()
return nil
}
// doSync runs the sync pipeline, using the injected syncFn when present (tests)
// or the real syncAndScan otherwise.
func (a *App) doSync(ctx context.Context) error {
if a.syncFn != nil {
return a.syncFn(ctx)
}
return a.syncAndScan(ctx)
}
// syncAndScan runs the full data pipeline once: Navidrome artist sync, the
// MusicBrainz discography pipeline (SyncAll), then the scanner over the
// now-populated DB. It logs results and observes ctx cancellation.
func (a *App) syncAndScan(ctx context.Context) error {
if err := navidrome.SyncArtists(ctx, a.ndClient, a.db); err != nil {
return fmt.Errorf("sync artists: %w", err)
}
discography := musicbrainz.NewDiscographySyncer(a.mbClient)
albums := musicbrainz.NewAlbumSyncer(func(ctx context.Context, db *database.DB) error {
return navidrome.SyncAlbums(ctx, a.ndClient, db)
})
if err := musicbrainz.SyncAll(ctx, a.db, a.mbClient, discography, albums, a.cfg.MusicBrainz.CacheTTL); err != nil {
return fmt.Errorf("sync all: %w", err)
}
missing, err := scanner.ScanAll(ctx, a.db, a.cfg.Scanner.FuzzyThreshold)
if err != nil {
return fmt.Errorf("scan all: %w", err)
}
@@ -131,10 +170,31 @@ func (a *App) run(ctx context.Context) error {
for _, m := range missing {
log.Printf(" missing: artist=%s rgid=%s title=%q", m.ArtistID, m.RGID, m.Title)
}
// Main application loop — blocks until context is cancelled.
// Business logic (notifier, web server) will be wired into separate
// goroutines here in future tasks.
<-ctx.Done()
return nil
}
// startPeriodicSync runs syncAndScan on a ticker at cfg.Sync.Interval. It
// blocks until ctx is cancelled, then returns cleanly. Each tick runs in its
// own goroutine so a slow sync does not block the ticker; a fresh interval is
// still scheduled regardless.
func (a *App) startPeriodicSync(ctx context.Context) {
ticker := time.NewTicker(a.cfg.Sync.Interval)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
log.Println("Periodic sync stopped.")
return
case <-ticker.C:
go func() {
if err := a.doSync(ctx); err != nil {
if ctx.Err() != nil {
return
}
log.Printf("Periodic sync+scan failed: %v", err)
}
}()
}
}
}