package watchdog import ( "log/slog" "orbits-server/internal/server/bootstrap" "orbits-server/internal/server/database" "time" "gorm.io/gorm" ) type State struct { FS map[string]struct{} DB map[string]database.File } type Result struct { FSOrphans []string DBOrphans []database.File } func Kickoff(env bootstrap.Environment, db *gorm.DB) { interval := time.Second * time.Duration(env.WatchdogInterval) ticker := time.NewTicker(interval) go func() { defer ticker.Stop() run(env, db) for range ticker.C { run(env, db) } }() } func run(env bootstrap.Environment, db *gorm.DB) { slog.Debug("watchdog cycle start") fsState, err := scanFS(env) if err != nil { return } dbState, err := scanDB(db) if err != nil { return } result := reconcile(fsState, dbState) if len(result.FSOrphans) > 0 { slog.Info("filesystem orphans detected") applyFS(env, db, result.FSOrphans) } if len(result.DBOrphans) > 0 { slog.Info("database orphans detected") applyDB(db, result.DBOrphans) } }