133 lines
3.1 KiB
Go
133 lines
3.1 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"flag"
|
|
"fmt"
|
|
"log/slog"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
|
|
"github.com/joho/godotenv"
|
|
|
|
"mtusi2026-messenger/internal/config"
|
|
"mtusi2026-messenger/internal/db"
|
|
"mtusi2026-messenger/internal/postgres"
|
|
"mtusi2026-messenger/internal/reconcile"
|
|
s3store "mtusi2026-messenger/internal/s3"
|
|
)
|
|
|
|
func main() {
|
|
if err := run(); err != nil {
|
|
fmt.Fprintln(os.Stderr, "reconcile:", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
func run() error {
|
|
if err := godotenv.Load(); err != nil && !os.IsNotExist(err) {
|
|
return err
|
|
}
|
|
|
|
dryRun := flag.Bool("dry-run", true, "report findings without changing PostgreSQL or S3")
|
|
grace := flag.Duration("grace", reconcile.DefaultGrace, "minimum age before an object or row may be repaired")
|
|
maxDeletes := flag.Int("max-deletes", reconcile.DefaultMaxDeletes, "maximum destructive repairs per run")
|
|
asJSON := flag.Bool("json", false, "print the report as JSON")
|
|
flag.Parse()
|
|
|
|
cfg := config.Load()
|
|
log := slog.New(slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{Level: cfg.Logging.Level}))
|
|
|
|
databaseURL, err := cfg.Database.URL()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
|
defer stop()
|
|
|
|
pool, err := postgres.New(ctx, databaseURL)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer pool.Close()
|
|
|
|
s3Client, err := s3store.New(ctx, cfg.S3, log)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
reconciler := reconcile.New(pool, s3Client, db.New(pool), log)
|
|
|
|
report, err := reconciler.Run(ctx, reconcile.Options{
|
|
DryRun: *dryRun,
|
|
Grace: *grace,
|
|
MaxDeletes: *maxDeletes,
|
|
StagingRetention: cfg.S3.StagingRetention,
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if *asJSON {
|
|
encoder := json.NewEncoder(os.Stdout)
|
|
encoder.SetIndent("", " ")
|
|
if err := encoder.Encode(report); err != nil {
|
|
return err
|
|
}
|
|
} else {
|
|
printReport(report)
|
|
}
|
|
|
|
if len(report.Errors) > 0 {
|
|
return fmt.Errorf("completed with %d errors", len(report.Errors))
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func printReport(report reconcile.Report) {
|
|
fmt.Printf("dry_run=%t duration_ms=%d\n", report.DryRun, report.DurationMS)
|
|
fmt.Printf(
|
|
"scanned objects=%d rows=%d list_pages=%d\n",
|
|
report.ScannedObjects,
|
|
report.ScannedRows,
|
|
report.ListPages,
|
|
)
|
|
fmt.Printf(
|
|
"found staging_objects=%d orphan_objects=%d missing_attachments=%d corrupt_objects=%d\n",
|
|
len(report.StagingObjects),
|
|
len(report.OrphanObjects),
|
|
len(report.MissingAttachments),
|
|
len(report.CorruptObjects),
|
|
)
|
|
fmt.Printf(
|
|
"skipped fresh=%d inactive=%d\n",
|
|
report.SkippedFresh,
|
|
report.SkippedInactive,
|
|
)
|
|
fmt.Printf(
|
|
"changes deleted_objects=%d marked_failed=%d\n",
|
|
report.DeletedObjects,
|
|
report.MarkedFailed,
|
|
)
|
|
|
|
for _, key := range report.StagingObjects {
|
|
fmt.Printf("staging_object %s\n", key)
|
|
}
|
|
for _, key := range report.OrphanObjects {
|
|
fmt.Printf("orphan_object %s\n", key)
|
|
}
|
|
for _, key := range report.CorruptObjects {
|
|
fmt.Printf("corrupt_object %s\n", key)
|
|
}
|
|
for _, id := range report.MissingAttachments {
|
|
fmt.Printf("missing_attachment %s\n", id)
|
|
}
|
|
for _, message := range report.Errors {
|
|
fmt.Printf("error %s\n", message)
|
|
}
|
|
}
|