Files
mtusi2026lab5/backend/cmd/worker/main.go

135 lines
3.4 KiB
Go

package main
import (
"backend/internal/adapter/imagefile"
"backend/internal/adapter/queue"
"backend/internal/adapter/repository/postgres"
"backend/internal/adapter/storage"
"backend/internal/application/usecase"
"backend/internal/config"
"context"
"flag"
"fmt"
"github.com/google/uuid"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/redis/go-redis/v9"
"os"
"os/signal"
"strconv"
"syscall"
"time"
)
func main() {
replay := flag.String("replay", "", "replay failed attachment UUID")
advance := flag.String("advance", "", "new version for ready/failed attachment UUID")
fault := flag.String("fault", "", "test-only crash point: after_original/after_objects/after_commit")
flag.Parse()
cfg, e := config.Load()
must(e)
dsn, e := config.ReadSecret(cfg.DatabaseURLFile)
must(e)
pc, e := pgxpool.ParseConfig(dsn)
must(e)
pc.MaxConns = 2
pc.ConnConfig.ConnectTimeout = time.Second
pc.ConnConfig.RuntimeParams["statement_timeout"] = "2000"
pc.ConnConfig.RuntimeParams["lock_timeout"] = "1000"
pool, e := pgxpool.NewWithConfig(context.Background(), pc)
must(e)
defer pool.Close()
repo := postgres.New(pool, nil)
if *replay != "" || *advance != "" {
s := *replay
if s == "" {
s = *advance
}
id, e := uuid.Parse(s)
must(e)
var ok bool
if *replay != "" {
ok, e = repo.ReplayJob(context.Background(), id)
} else {
ok, e = repo.AdvancePipeline(context.Background(), id)
}
must(e)
fmt.Printf("{\"changed\":%v}\n", ok)
return
}
s3path := os.Getenv("S3_CONFIG_FILE")
if s3path == "" {
s3path = "/run/secrets/s3_credentials"
}
s3cfg, e := storage.ReadConfig(s3path)
must(e)
objects, e := storage.FromConfig(s3cfg)
must(e)
url, e := config.ReadSecret(cfg.CacheURLFile)
must(e)
opts, e := redis.ParseURL(url)
must(e)
opts.MaxRetries = 0
opts.DialTimeout = time.Second
opts.ReadTimeout = 2 * time.Second
opts.WriteTimeout = time.Second
client := redis.NewClient(opts)
defer func() { _ = client.Close() }()
attempts := 5
if s := os.Getenv("WORKER_MAX_ATTEMPTS"); s != "" {
attempts, e = strconv.Atoi(s)
must(e)
}
if attempts < 1 || attempts > 10 {
must(fmt.Errorf("attempts out of bounds"))
}
pipeline := usecase.NewPipeline(repo, objects, imagefile.NewTransformer(), int32(attempts))
if *fault != "" {
switch *fault {
case "after_original", "after_objects", "after_commit":
default:
must(fmt.Errorf("invalid fault point"))
}
pipeline.Fault = func(point string) error {
if point == *fault {
os.Exit(88)
}
return nil
}
}
broker := queue.New(client, repo, uuid.NewString())
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()
logError := func(error) { fmt.Fprintln(os.Stderr, "{\"event\":\"pipeline_dependency_error\"}") }
cleanupDone := make(chan struct{})
go func() {
defer close(cleanupDone)
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
cleanup, cancel := context.WithTimeout(ctx, 5*time.Second)
e := pipeline.Cleanup(cleanup)
cancel()
if e != nil {
logError(e)
}
}
}
}()
done := make(chan struct{})
go func() { defer close(done); broker.RunPublisher(ctx, logError) }()
broker.RunWorker(ctx, pipeline.Process, logError)
stop()
<-done
<-cleanupDone
}
func must(e error) {
if e != nil {
fmt.Fprintln(os.Stderr, "worker initialization/operation failed")
os.Exit(1)
}
}