186 lines
4.1 KiB
Go
186 lines
4.1 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"flag"
|
|
"fmt"
|
|
"os"
|
|
"os/signal"
|
|
"strings"
|
|
"syscall"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
"github.com/joho/godotenv"
|
|
|
|
"mtusi2026-messenger/internal/config"
|
|
"mtusi2026-messenger/internal/db"
|
|
"mtusi2026-messenger/internal/logger"
|
|
"mtusi2026-messenger/internal/postgres"
|
|
s3store "mtusi2026-messenger/internal/s3"
|
|
"mtusi2026-messenger/internal/worker"
|
|
)
|
|
|
|
var (
|
|
errConflictingReplayFlags = errors.New("replay and list-failed cannot be combined")
|
|
errInvalidReplayID = errors.New("replay must be an attachment UUID")
|
|
errMissingReplayAudit = errors.New("replay requires nonempty operator and reason")
|
|
)
|
|
|
|
func main() {
|
|
maxPixels := flag.Int("process-image", 0, "process an image from stdin with the given pixel limit")
|
|
pipelineVersion := flag.Int("pipeline-version", 1, "pipeline version for process-image")
|
|
replay := flag.String("replay", "", "replay a failed attachment by UUID; requires retained staging and no existing variants")
|
|
operator := flag.String("operator", "", "operator requesting replay")
|
|
reason := flag.String("reason", "", "reason for replay")
|
|
listFailed := flag.Bool("list-failed", false, "list up to 100 failed attachments")
|
|
flag.Parse()
|
|
|
|
if *maxPixels > 0 {
|
|
if err := worker.ProcessImage(*maxPixels, *pipelineVersion); err != nil {
|
|
if errors.Is(err, worker.ErrInvalidSource) {
|
|
os.Exit(2)
|
|
}
|
|
|
|
fmt.Fprintln(os.Stderr, "process image:", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
|
defer stop()
|
|
|
|
var err error
|
|
switch {
|
|
case *listFailed && *replay != "":
|
|
err = errConflictingReplayFlags
|
|
case *listFailed:
|
|
err = runListFailed(ctx)
|
|
case *replay != "":
|
|
err = runReplay(ctx, *replay, *operator, *reason)
|
|
default:
|
|
err = runWorker(ctx)
|
|
}
|
|
|
|
if err != nil {
|
|
fmt.Fprintln(os.Stderr, "worker:", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
func runListFailed(ctx context.Context) error {
|
|
ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
|
|
defer cancel()
|
|
|
|
_, pool, err := loadDatabase(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer pool.Close()
|
|
|
|
rows, err := db.New(pool).ListFailedAttachments(ctx, 100)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
for _, row := range rows {
|
|
code := "-"
|
|
if row.ErrorCode != nil {
|
|
code = *row.ErrorCode
|
|
}
|
|
|
|
fmt.Printf("%s attempts=%d error_code=%s\n", row.ID, row.Attempts, code)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func runReplay(ctx context.Context, replay, operator, reason string) error {
|
|
replayID, err := uuid.Parse(replay)
|
|
if err != nil {
|
|
return errInvalidReplayID
|
|
}
|
|
|
|
operator, reason = strings.TrimSpace(operator), strings.TrimSpace(reason)
|
|
if operator == "" || reason == "" {
|
|
return errMissingReplayAudit
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
|
|
defer cancel()
|
|
|
|
cfg, pool, err := loadDatabase(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer pool.Close()
|
|
|
|
log := logger.New(cfg.Logging.Level)
|
|
storage, err := s3store.NewAPI(ctx, cfg.S3, log)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := worker.Replay(ctx, pool, storage, replayID, operator, reason); err != nil {
|
|
return err
|
|
}
|
|
|
|
log.Info("attachment replay queued", "attachment_id", replayID, "operator", operator)
|
|
return nil
|
|
}
|
|
|
|
func runWorker(ctx context.Context) error {
|
|
cfg, pool, err := loadDatabase(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer pool.Close()
|
|
|
|
brokerURL, err := cfg.Broker.URL()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
log := logger.New(cfg.Logging.Level)
|
|
storage, err := s3store.NewAPI(ctx, cfg.S3, log)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
w := worker.New(
|
|
pool,
|
|
storage,
|
|
brokerURL,
|
|
config.LoadWorker(),
|
|
cfg.S3.MaxUploadBytes,
|
|
cfg.S3.MaxImagePixels,
|
|
log,
|
|
)
|
|
w.Run(ctx)
|
|
|
|
return nil
|
|
}
|
|
|
|
func loadDatabase(ctx context.Context) (config.Config, *pgxpool.Pool, error) {
|
|
if err := godotenv.Load(); err != nil && !os.IsNotExist(err) {
|
|
return config.Config{}, nil, err
|
|
}
|
|
|
|
cfg := config.Load()
|
|
databaseURL, err := cfg.Database.URL()
|
|
if err != nil {
|
|
return config.Config{}, nil, err
|
|
}
|
|
|
|
pool, err := postgres.New(ctx, databaseURL)
|
|
if err != nil {
|
|
return config.Config{}, nil, err
|
|
}
|
|
|
|
return cfg, pool, nil
|
|
}
|