Files
mtusi2026lab5-fork/main.go
T

200 lines
5.4 KiB
Go

package main
import (
"context"
"encoding/json"
"log/slog"
"net"
"net/http"
"os"
"os/signal"
"strconv"
"syscall"
"time"
"github.com/jackc/pgx/v5/pgxpool"
)
func main() {
log := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelInfo}))
host := envOr("APP_HOST", "0.0.0.0")
port := envOr("APP_PORT", "8080")
shutdownSec, _ := strconv.Atoi(envOr("SHUTDOWN_TIMEOUT_SECONDS", "5"))
if shutdownSec < 1 {
shutdownSec = 5
}
ttlSec, _ := strconv.Atoi(envOr("SESSION_TTL_SECONDS", "3600"))
if ttlSec < 1 {
ttlSec = 3600
}
idemSec, _ := strconv.Atoi(envOr("IDEMPOTENCY_TTL_SECONDS", "86400"))
if idemSec < 1 {
idemSec = 86400
}
drainSec, _ := strconv.Atoi(envOr("DRAIN_SECONDS", "3"))
ctx := context.Background()
if len(os.Args) > 1 && os.Args[1] == "s3-init" {
if err := s3Init(ctx); err != nil {
log.Error("s3 init failed", "err", err.Error())
os.Exit(1)
}
log.Info("bucket ready")
return
}
db, err := openDB(ctx)
if err != nil {
log.Error("database open failed", "err", err.Error())
os.Exit(1)
}
defer db.Close()
if len(os.Args) > 1 && os.Args[1] == "migrate" {
if err := migrate(ctx, db); err != nil {
log.Error("migrate failed", "err", err.Error())
os.Exit(1)
}
log.Info("migrations applied")
return
}
store, err := openStore()
if err != nil {
log.Error("s3 open failed", "err", err.Error())
os.Exit(1)
}
if len(os.Args) > 1 && (os.Args[1] == "cleanup" || os.Args[1] == "janitor") {
runCleanup(ctx, db, store, log, os.Args[1] == "janitor")
return
}
maxBytes, _ := strconv.ParseInt(envOr("MAX_UPLOAD_BYTES", "10485760"), 10, 64)
maxPixels, _ := strconv.ParseInt(envOr("MAX_IMAGE_PIXELS", "40000000"), 10, 64)
maxAttempts := envInt("JOB_MAX_ATTEMPTS", 5)
if len(os.Args) > 1 && (os.Args[1] == "replay" || os.Args[1] == "failed") {
out := json.NewEncoder(os.Stdout)
if os.Args[1] == "failed" {
items, err := listFailed(ctx, db)
if err != nil {
log.Error("failed jobs query failed", "err", err.Error())
os.Exit(1)
}
for _, item := range items {
_ = out.Encode(item)
}
return
}
if len(os.Args) != 3 || !isUUID(os.Args[2]) {
log.Error("usage: replay <attachment_id>")
os.Exit(2)
}
result, err := replayJob(ctx, db, os.Args[2], maxAttempts)
if err != nil {
log.Error("replay failed", "err", err.Error())
os.Exit(1)
}
_ = out.Encode(map[string]string{"attachment_id": os.Args[2], "result": result})
return
}
broker, err := openBroker()
if err != nil {
log.Error("broker open failed", "err", err.Error())
os.Exit(1)
}
if len(os.Args) > 1 && os.Args[1] == "worker" {
timeout := time.Duration(envInt("JOB_TIMEOUT_SECONDS", 60)) * time.Second
owner, _ := os.Hostname()
w := &Worker{
db: db, store: store, broker: broker, log: log,
owner: owner + "-" + newUUID()[:8],
timeout: timeout,
lease: timeout + 15*time.Second,
backoffBase: time.Duration(envInt("JOB_BACKOFF_BASE_MS", 1000)) * time.Millisecond,
backoffMax: time.Duration(envInt("JOB_BACKOFF_MAX_MS", 30000)) * time.Millisecond,
maxBytes: maxBytes,
maxPixels: maxPixels,
fault: os.Getenv("FAULT_POINT"),
}
runWorker(db, store, broker, log, w, envInt("WORKER_CONCURRENCY", 2))
return
}
cache := openCache(log)
defer cache.Close()
slots := envInt("UPLOAD_CONCURRENCY", 10)
app := NewApp(db, cache, log, time.Duration(ttlSec)*time.Second, time.Duration(idemSec)*time.Second, store,
limits{maxBytes: maxBytes, maxPixels: maxPixels, uploadSlots: slots, maxAttempts: maxAttempts})
relayCtx, stopRelay := context.WithCancel(ctx)
defer stopRelay()
go runRelay(relayCtx, db, broker, log)
addr := net.JoinHostPort(host, port)
srv := &http.Server{
Addr: addr,
Handler: app.Routes(),
ReadHeaderTimeout: 5 * time.Second,
ReadTimeout: 10 * time.Second,
WriteTimeout: 15 * time.Second,
IdleTimeout: 30 * time.Second,
}
go func() {
log.Info("listening", "addr", addr, "env", envOr("APP_ENV", "lab"), "session_ttl_s", ttlSec)
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Error("server error", "err", err.Error())
os.Exit(1)
}
}()
stop := make(chan os.Signal, 1)
signal.Notify(stop, syscall.SIGTERM, syscall.SIGINT)
sig := <-stop
log.Info("shutdown started", "signal", sig.String())
app.SetReady(false)
time.Sleep(time.Duration(drainSec) * time.Second)
shCtx, cancel := context.WithTimeout(context.Background(), time.Duration(shutdownSec)*time.Second)
defer cancel()
if err := srv.Shutdown(shCtx); err != nil {
log.Error("shutdown error", "err", err.Error())
os.Exit(1)
}
log.Info("shutdown finished")
}
func runCleanup(ctx context.Context, db *pgxpool.Pool, store *Store, log *slog.Logger, loop bool) {
graceSec, _ := strconv.Atoi(envOr("CLEANUP_GRACE_SECONDS", "600"))
intervalSec, _ := strconv.Atoi(envOr("CLEANUP_INTERVAL_SECONDS", "300"))
retention := time.Duration(envInt("STAGING_RETENTION_SECONDS", 86400)) * time.Second
for {
removed, err := cleanupOrphans(ctx, db, store, time.Duration(graceSec)*time.Second, retention, log)
if err != nil {
log.Error("cleanup failed", "err", err.Error())
if !loop {
os.Exit(1)
}
} else {
log.Info("cleanup finished", "removed", removed)
}
if !loop {
return
}
time.Sleep(time.Duration(intervalSec) * time.Second)
}
}
func envInt(key string, def int) int {
v, err := strconv.Atoi(os.Getenv(key))
if err != nil || v < 1 {
return def
}
return v
}