forked from mtusi2026/mtusi2026lab5
lab5 na4
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"log"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"mtusi2026/internal/blobstore"
|
||||
"mtusi2026/internal/config"
|
||||
"mtusi2026/internal/db"
|
||||
"mtusi2026/internal/reconcile"
|
||||
)
|
||||
|
||||
func main() {
|
||||
dry := flag.Bool("dry-run", false, "only report orphans, delete nothing")
|
||||
grace := flag.Duration("grace", time.Hour, "do not touch objects younger than this")
|
||||
loop := flag.Duration("loop", 0, "repeat with this interval; 0 = run once")
|
||||
missing := flag.Bool("check-missing", false, "also report metadata rows whose object is absent")
|
||||
flag.Parse()
|
||||
|
||||
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGTERM, syscall.SIGINT)
|
||||
defer stop()
|
||||
|
||||
pool, err := db.Connect(ctx, db.Config{
|
||||
DSN: config.GetEnv("DATABASE_URL", ""), MaxConns: 2, MinConns: 0,
|
||||
ConnMaxLifetime: time.Hour, ConnTimeout: 5 * time.Second,
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatalf("db: %v", err)
|
||||
}
|
||||
defer pool.Close()
|
||||
|
||||
blobs, err := blobstore.NewS3(ctx, blobstore.S3Config{
|
||||
Endpoint: config.GetEnv("S3_ENDPOINT", ""), Region: config.GetEnv("S3_REGION", "us-east-1"),
|
||||
Bucket: config.GetEnv("S3_BUCKET", ""), AccessKey: config.GetEnv("S3_ACCESS_KEY", ""),
|
||||
SecretKey: config.GetEnv("S3_SECRET_KEY", ""),
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatalf("s3: %v", err)
|
||||
}
|
||||
|
||||
for {
|
||||
start := time.Now()
|
||||
rep, err := reconcile.Run(ctx, pool, blobs, *grace, *dry, *missing, log.Printf)
|
||||
log.Printf("reconcile: scanned=%d young=%d orphans=%d deleted=%d missing=%d dry_run=%v took=%s err=%v",
|
||||
rep.Scanned, rep.Young, rep.Orphans, rep.Deleted, rep.Missing, *dry, time.Since(start).Round(time.Millisecond), err)
|
||||
if *loop == 0 {
|
||||
if err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
return
|
||||
}
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-time.After(*loop):
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user