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

161 lines
4.6 KiB
Go

package main
import (
"backend/internal/adapter/imagefile"
"backend/internal/adapter/lifecycle"
"backend/internal/adapter/queue"
"backend/internal/adapter/repository/postgres"
"backend/internal/adapter/storage"
"backend/internal/adapter/telemetry"
"backend/internal/application/usecase"
"backend/internal/config"
"context"
"flag"
"fmt"
"github.com/google/uuid"
"github.com/jackc/pgx/v5/pgxpool"
"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)
observability, e := telemetry.Setup(context.Background(), "messenger-worker")
must(e)
defer func() {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
observability.Shutdown(ctx)
}()
dsn, e := config.ReadSecret(cfg.DatabaseURLFile)
must(e)
pc, e := pgxpool.ParseConfig(dsn)
must(e)
pc.MaxConns = 2
pc.ConnConfig.Tracer = telemetry.DBTracer{}
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)
must(observability.Gauges(func(ctx context.Context) (map[string]float64, error) {
m, e := repo.PipelineMetrics(ctx)
values := telemetry.PipelineValues(m)
values["db_pool_used"] = float64(pool.Stat().AcquiredConns())
values["db_pool_capacity"] = float64(pool.Stat().MaxConns())
return values, e
}))
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)
consumerURL, e := config.ReadSecret(cfg.CacheURLFile)
must(e)
publisherURL, e := config.ReadSecret(os.Getenv("BROKER_PUBLISH_URL_FILE"))
must(e)
publisher, client, e := queue.Clients(publisherURL, consumerURL)
must(e)
defer func() { _ = publisher.Close(); _ = 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, telemetry.Objects{Objects: objects}, imagefile.NewTransformer(), int32(attempts))
pipeline.OnResult = observability.JobResult
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(publisher, client, repo, uuid.NewString())
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()
watcher, watchErr := lifecycle.FromEnvironment()
must(watchErr)
monitorDone := make(chan struct{})
if watcher != nil {
must(watcher.Check(ctx))
monitor, err := lifecycle.New(lifecycle.Ports{Watch: watcher.Run, Stop: stop, Report: func() { fmt.Fprintln(os.Stderr, "{\"event\":\"secret_freshness_failed\"}") }})
must(err)
go func() { defer close(monitorDone); monitor.Run(ctx) }()
} else {
close(monitorDone)
}
defer func() { stop(); <-monitorDone }()
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, observability.Worker(pipeline.Process), logError)
stop()
<-done
<-cleanupDone
}
func must(e error) {
if e != nil {
fmt.Fprintln(os.Stderr, "worker initialization/operation failed")
os.Exit(1)
}
}