// Command reconciler periodically removes abandoned attachment objects and // pending metadata rows. It is safe to run concurrently with the API and safe // to re-run; see internal/reconcile for the grace-period rules. package main import ( "context" "flag" "log" "log/slog" "os/signal" "syscall" "time" "gitflic.ru/project/bulla-max/mtusi2026lab/internal/config" "gitflic.ru/project/bulla-max/mtusi2026lab/internal/database" "gitflic.ru/project/bulla-max/mtusi2026lab/internal/objectstore" "gitflic.ru/project/bulla-max/mtusi2026lab/internal/reconcile" "gitflic.ru/project/bulla-max/mtusi2026lab/internal/store" ) func main() { once := flag.Bool("once", false, "run one reconciliation pass and exit") dryRun := flag.Bool("dry-run", false, "log what would be removed without changing either store") grace := flag.Duration("grace", 0, "override RECONCILE_GRACE_SECONDS for this run") interval := flag.Duration("interval", 0, "override RECONCILE_INTERVAL_SECONDS for this run") flag.Parse() cfg, err := config.Load() if err != nil { log.Fatalf("configuration: %v", err) } if *grace > 0 { cfg.ReconcileGrace = *grace } if *interval > 0 { cfg.ReconcileInterval = *interval } rootCtx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) defer stop() pool, err := database.Connect(rootCtx, cfg.DatabaseURL, cfg.DBMaxConns, cfg.StartupWait) if err != nil { log.Fatalf("database: %v", err) } defer pool.Close() objects, err := objectstore.New(objectstore.Config{ Endpoint: cfg.S3Endpoint, Region: cfg.S3Region, Bucket: cfg.S3Bucket, AccessKey: cfg.S3AccessKey, SecretKey: cfg.S3SecretKey, }) if err != nil { log.Fatalf("object storage: %v", err) } worker := reconcile.New( store.New(pool, cfg.DBTimeout, cfg.IdempotencyTTL), objects, cfg.ReconcileGrace, slog.Default(), ) if *once { stats, runErr := worker.RunOnce(rootCtx, *dryRun) logStats(stats, *dryRun) if runErr != nil { log.Fatalf("reconcile: %v", runErr) } return } for { stats, runErr := worker.RunOnce(rootCtx, *dryRun) if runErr != nil { slog.Error("reconcile pass failed", "err", runErr) } else { logStats(stats, *dryRun) } // A timer avoids the ticker behaviour where missed ticks accumulate: // the next pass starts one full interval after this one finished. timer := time.NewTimer(cfg.ReconcileInterval) select { case <-rootCtx.Done(): timer.Stop() return case <-timer.C: } } } func logStats(stats reconcile.Stats, dryRun bool) { slog.Info("reconcile pass complete", "dry_run", dryRun, "stale_pending", stats.StalePending, "orphan_objects", stats.OrphanObjects, "deleted_rows", stats.DeletedRows, "deleted_objects", stats.DeletedObjects, "skipped", stats.Skipped, ) }