forked from mtusi2026/mtusi2026lab5
lab5 na4
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"log"
|
||||
"os"
|
||||
"os/signal"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"github.com/redis/go-redis/v9"
|
||||
"mtusi2026/internal/blobstore"
|
||||
"mtusi2026/internal/config"
|
||||
"mtusi2026/internal/jobs"
|
||||
)
|
||||
|
||||
func main() {
|
||||
mode := flag.String("mode", "worker", "worker | relay | failed | replay")
|
||||
id := flag.String("job", "", "failed job UUID")
|
||||
reason := flag.String("reason", "", "audited replay reason (required)")
|
||||
flag.Parse()
|
||||
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGTERM, syscall.SIGINT)
|
||||
defer stop()
|
||||
pool, err := pgxpool.New(ctx, config.GetEnv("DATABASE_URL", ""))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer pool.Close()
|
||||
timeout := config.ParseDurationSecondsEnv("JOB_TIMEOUT_SECONDS", 30)
|
||||
concurrency := config.ParseIntEnv("WORKER_CONCURRENCY", 1)
|
||||
attempts := config.ParseIntEnv("JOB_MAX_ATTEMPTS", 5)
|
||||
if concurrency < 1 || concurrency > 4 || attempts < 1 || timeout < time.Second || timeout > 5*time.Minute {
|
||||
log.Fatal("invalid worker limits")
|
||||
}
|
||||
repo := jobs.Repo{Pool: pool, MaxAttempts: attempts, Lease: timeout + 10*time.Second}
|
||||
if *mode == "failed" {
|
||||
rows, err := repo.Failed(ctx)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
json.NewEncoder(os.Stdout).Encode(rows)
|
||||
return
|
||||
}
|
||||
if *mode == "replay" {
|
||||
if strings.TrimSpace(*reason) == "" {
|
||||
log.Fatal("reason is required")
|
||||
}
|
||||
if err := repo.Replay(ctx, *id, *reason); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
log.Printf("replay queued job=%s", *id)
|
||||
return
|
||||
}
|
||||
opts, err := redis.ParseURL(config.GetEnv("BROKER_URL", "redis://broker:6379/0"))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
broker := redis.NewClient(opts)
|
||||
defer broker.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.Fatal(err)
|
||||
}
|
||||
rt := jobs.Runtime{Repo: repo, Broker: broker, Blobs: blobs, Concurrency: concurrency, Timeout: timeout, Retention: config.ParseDurationSecondsEnv("STAGING_RETENTION_SECONDS", 86400), Fault: config.GetEnv("PIPELINE_FAULT", "")}
|
||||
log.Printf("pipeline mode=%s concurrency=%d prefetch=1 timeout=%s lease=%s attempts=%d", *mode, concurrency, timeout, repo.Lease, attempts)
|
||||
switch *mode {
|
||||
case "worker":
|
||||
if err := rt.Workers(ctx); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
case "relay":
|
||||
rt.Relay(ctx)
|
||||
default:
|
||||
log.Fatal("unknown mode")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user