forked from mtusi2026/mtusi2026lab5
feat: port lab4 implementation
This commit is contained in:
@@ -0,0 +1,133 @@
|
||||
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(s3Client, db.New(pool), log)
|
||||
|
||||
report, err := reconciler.Run(ctx, reconcile.Options{
|
||||
DryRun: *dryRun,
|
||||
Grace: *grace,
|
||||
MaxDeletes: *maxDeletes,
|
||||
})
|
||||
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 orphan_objects=%d missing_attachments=%d stale_uploads=%d corrupt_objects=%d\n",
|
||||
len(report.OrphanObjects),
|
||||
len(report.MissingAttachments),
|
||||
len(report.StaleUploads),
|
||||
len(report.CorruptObjects),
|
||||
)
|
||||
fmt.Printf(
|
||||
"skipped fresh=%d referenced=%d inactive=%d\n",
|
||||
report.SkippedFresh,
|
||||
report.SkippedReferenced,
|
||||
report.SkippedInactive,
|
||||
)
|
||||
fmt.Printf(
|
||||
"changes deleted_objects=%d deleted_rows=%d marked_failed=%d\n",
|
||||
report.DeletedObjects,
|
||||
report.DeletedRows,
|
||||
report.MarkedFailed,
|
||||
)
|
||||
|
||||
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 _, id := range report.StaleUploads {
|
||||
fmt.Printf("stale_upload %s\n", id)
|
||||
}
|
||||
for _, message := range report.Errors {
|
||||
fmt.Printf("error %s\n", message)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"mtusi2026-messenger/internal/config"
|
||||
"mtusi2026-messenger/internal/s3"
|
||||
)
|
||||
|
||||
func main() {
|
||||
cfg := config.LoadS3()
|
||||
|
||||
err := s3.WriteConfig("/config/s3.json", cfg)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "s3config:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"mtusi2026-messenger/internal/app"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if len(os.Args) > 1 && os.Args[1] == "healthcheck" {
|
||||
os.Exit(app.Healthcheck())
|
||||
}
|
||||
|
||||
if err := app.Run(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user