forked from mtusi2026/mtusi2026lab4
lab 4 na 4
This commit is contained in:
+20
-2
@@ -10,6 +10,7 @@ import (
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"mtusi2026/internal/blobstore"
|
||||
"mtusi2026/internal/cache"
|
||||
"mtusi2026/internal/config"
|
||||
"mtusi2026/internal/db"
|
||||
@@ -75,10 +76,27 @@ func main() {
|
||||
}
|
||||
var draining atomic.Bool
|
||||
|
||||
var blobs blobstore.Store
|
||||
if ep := config.GetEnv("S3_ENDPOINT", ""); ep != "" {
|
||||
b, err := blobstore.NewS3(ctx, blobstore.S3Config{
|
||||
Endpoint: ep,
|
||||
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 config: %v", err)
|
||||
}
|
||||
blobs = b
|
||||
}
|
||||
|
||||
mux := httpapi.NewRouterWithCache(s, pool, sessionStore, chatCache, httpapi.Options{
|
||||
InstanceID: instanceID,
|
||||
Redis: redisClient,
|
||||
Draining: &draining,
|
||||
Blobs: blobs,
|
||||
MaxUploads: config.ParseIntEnv("MAX_UPLOADS", 4),
|
||||
})
|
||||
|
||||
port := config.GetEnv("PORT", "8080")
|
||||
@@ -86,8 +104,8 @@ func main() {
|
||||
Addr: ":" + port,
|
||||
Handler: mux,
|
||||
ReadHeaderTimeout: 5 * time.Second,
|
||||
ReadTimeout: 15 * time.Second,
|
||||
WriteTimeout: 15 * time.Second,
|
||||
ReadTimeout: 60 * time.Second,
|
||||
WriteTimeout: 60 * time.Second,
|
||||
IdleTimeout: 60 * time.Second,
|
||||
}
|
||||
|
||||
|
||||
@@ -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