forked from mtusi2026/mtusi2026lab4
76 lines
2.1 KiB
Go
76 lines
2.1 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"flag"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"time"
|
|
|
|
"mtusi2026lab4/internal/adapter/objectstore"
|
|
"mtusi2026lab4/internal/adapter/postgres"
|
|
"mtusi2026lab4/internal/config"
|
|
"mtusi2026lab4/internal/database"
|
|
"mtusi2026lab4/internal/reconcile"
|
|
)
|
|
|
|
func main() {
|
|
apply := flag.Bool("apply", false, "apply safe repairs; default is dry-run")
|
|
gracePeriod := flag.Duration("grace-period", 15*time.Minute,
|
|
"ignore metadata and objects newer than this duration")
|
|
timeout := flag.Duration("timeout", 2*time.Minute, "maximum reconciliation runtime")
|
|
flag.Parse()
|
|
|
|
if *gracePeriod <= 0 || *timeout <= 0 {
|
|
log.Fatal("[FATAL] grace-period and timeout must be positive")
|
|
}
|
|
cfg, err := config.Load()
|
|
if err != nil {
|
|
log.Fatalf("[FATAL] Invalid configuration: %v", err)
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), *timeout)
|
|
defer cancel()
|
|
databasePool, err := database.Open(ctx, cfg)
|
|
if err != nil {
|
|
log.Fatalf("[FATAL] PostgreSQL initialization failed: %v", err)
|
|
}
|
|
defer databasePool.Close()
|
|
metadataStore := postgres.NewPostgreSQLStore(databasePool, cfg.DatabaseQueryTimeout)
|
|
|
|
objectStore, err := objectstore.NewMinIOStore(
|
|
cfg.S3Endpoint,
|
|
cfg.S3Region,
|
|
cfg.S3Bucket,
|
|
cfg.S3AccessKeyID,
|
|
cfg.S3SecretAccessKey,
|
|
cfg.S3OperationTimeout,
|
|
)
|
|
if err != nil {
|
|
log.Fatalf("[FATAL] Invalid object storage configuration: %v", err)
|
|
}
|
|
if err = objectStore.Ping(ctx); err != nil {
|
|
log.Fatalf("[FATAL] Object storage unavailable: %v", err)
|
|
}
|
|
|
|
reconciler, err := reconcile.New(metadataStore, objectStore)
|
|
if err != nil {
|
|
log.Fatalf("[FATAL] Reconciler initialization failed: %v", err)
|
|
}
|
|
result, err := reconciler.Run(ctx, *gracePeriod, *apply)
|
|
if err != nil {
|
|
log.Fatalf("[FATAL] Reconciliation failed: %v", err)
|
|
}
|
|
|
|
encoder := json.NewEncoder(os.Stdout)
|
|
encoder.SetIndent("", " ")
|
|
if err = encoder.Encode(result); err != nil {
|
|
log.Fatalf("[FATAL] Encode reconciliation result: %v", err)
|
|
}
|
|
if !*apply && len(result.Issues) > 0 {
|
|
fmt.Fprintln(os.Stderr, "dry-run found reconciliation issues; rerun with --apply after review")
|
|
}
|
|
}
|