Files
mtusi2026lab5/cmd/worker/main.go
T

194 lines
4.7 KiB
Go

// Command worker runs image consumers or an explicit operator action.
package main
import (
"context"
"encoding/json"
"errors"
"log/slog"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"time"
"github.com/google/uuid"
"github.com/jackc/pgx/v5/pgxpool"
"messenger/internal/attachment"
"messenger/internal/broker"
"messenger/internal/imaging"
"messenger/internal/pipeline"
"messenger/internal/platform/config"
"messenger/internal/platform/postgres"
"messenger/internal/platform/s3store"
"messenger/internal/processing"
)
func main() {
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGTERM, syscall.SIGINT)
defer stop()
if err := run(ctx, os.Args[1:]); err != nil {
slog.Error("worker stopped", "error", err)
os.Exit(1)
}
}
const usage = "use failed, replay <id> <operator>, reprocess <id> <version>, or duplicate <id> <version> <attempt>"
type command struct {
name, actor, barrierDir string
id uuid.UUID
version, attempt int
}
func parseCommand(args []string) (command, error) {
var c command
if len(args) == 0 {
return c, nil
}
if dir, ok := strings.CutPrefix(args[0], "--barriers="); ok && len(args) == 1 && dir != "" {
c.barrierDir = dir
return c, nil
}
c.name = args[0]
switch c.name {
case "--prepare-large-png", "failed":
if len(args) == 1 {
return c, nil
}
return c, errors.New(usage)
case "replay", "reprocess":
if len(args) != 3 {
return c, errors.New(usage)
}
case "duplicate":
if len(args) != 4 {
return c, errors.New(usage)
}
default:
return c, errors.New(usage)
}
var err error
c.id, err = uuid.Parse(args[1])
if err != nil {
return c, errors.New("invalid attachment UUID")
}
if c.name == "replay" {
c.actor = strings.TrimSpace(args[2])
if c.actor == "" || len(c.actor) > 128 {
return c, errors.New("replay requires an operator name of up to 128 bytes")
}
return c, nil
}
c.version, err = strconv.Atoi(args[2])
if err != nil || c.version < 1 {
return c, errors.New("invalid pipeline version")
}
if c.name == "duplicate" {
c.attempt, err = strconv.Atoi(args[3])
if err != nil || c.attempt < 1 {
return c, errors.New("invalid attempt number")
}
}
return c, nil
}
func run(ctx context.Context, args []string) error {
cmd, err := parseCommand(args)
if err != nil {
return err
}
if cmd.name == "--prepare-large-png" {
return imaging.PrepareLargePNG(os.Stdin)
}
if cmd.name != "" {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, 15*time.Second)
defer cancel()
}
db, err := config.LoadDatabaseURL(os.LookupEnv)
if err != nil {
return err
}
pool, err := postgres.NewPool(ctx, db, 5)
if err != nil {
return err
}
defer pool.Close()
store := pipeline.NewStore(pool)
switch cmd.name {
case "failed":
jobs, err := store.Failed(ctx)
if err != nil {
return err
}
return json.NewEncoder(os.Stdout).Encode(jobs)
case "duplicate":
url, err := config.LoadBrokerURL(os.LookupEnv)
if err != nil {
return err
}
return duplicate(ctx, pool, url, cmd.id, cmd.version, cmd.attempt)
}
s3, err := config.LoadS3(os.LookupEnv)
if err != nil {
return err
}
blobs, err := s3store.New(s3)
if err != nil {
return err
}
exists := func(ctx context.Context, key string) (bool, error) {
_, err := blobs.Stat(ctx, key)
if errors.Is(err, attachment.ErrObjectMissing) {
return false, nil
}
return err == nil, err
}
switch cmd.name {
case "replay":
return store.Replay(ctx, cmd.id, cmd.actor, exists)
case "reprocess":
return store.Reprocess(ctx, cmd.id, cmd.version, exists)
}
cfg, err := config.LoadJobs(os.LookupEnv)
if err != nil {
return err
}
w := processing.Worker{Store: store, Blobs: blobs, Config: cfg, Log: slog.Default()}
if cmd.barrierDir != "" {
if err := os.MkdirAll(cmd.barrierDir, 0700); err != nil {
return err
}
w.Checkpoint = processing.FileBarrier(cmd.barrierDir)
}
if err := imaging.CleanScratch(); err != nil {
return err
}
w.Run(ctx)
return nil
}
// duplicate sends a previously issued envelope through the actual broker. It
// cannot change SQL state or manufacture a future attempt.
func duplicate(ctx context.Context, pool *pgxpool.Pool, url string, id uuid.UUID, version, attempt int) error {
j := pipeline.Job{SchemaVersion: 1, Type: "attachment.process.v1", AttachmentID: id, PipelineVersion: version, Attempt: attempt}
err := pool.QueryRow(ctx, `SELECT job_id,created_at FROM attachment_jobs
WHERE attachment_id=$1 AND pipeline_version=$2 AND $3::integer BETWEEN 1 AND attempt`, id, version, attempt).Scan(&j.JobID, &j.CreatedAt)
if err != nil {
return err
}
p, err := broker.NewPublisher(ctx, url)
if err != nil {
return err
}
defer p.Close()
body, err := json.Marshal(j)
if err != nil {
return err
}
return p.Publish(ctx, body)
}