feat(worker): add safe failed attachment replay CLI
This commit is contained in:
+117
-19
@@ -7,19 +7,34 @@ import (
|
||||
"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")
|
||||
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 {
|
||||
@@ -35,44 +50,107 @@ func main() {
|
||||
return
|
||||
}
|
||||
|
||||
if err := run(); err != nil {
|
||||
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 run() error {
|
||||
if err := godotenv.Load(); err != nil && !os.IsNotExist(err) {
|
||||
return err
|
||||
}
|
||||
func runListFailed(ctx context.Context) error {
|
||||
ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
|
||||
defer cancel()
|
||||
|
||||
cfg := config.Load()
|
||||
log := logger.New(cfg.Logging.Level)
|
||||
databaseURL, err := cfg.Database.URL()
|
||||
_, 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
|
||||
}
|
||||
|
||||
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
||||
defer stop()
|
||||
|
||||
pool, err := postgres.New(ctx, databaseURL)
|
||||
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
|
||||
}
|
||||
|
||||
worker.New(
|
||||
w := worker.New(
|
||||
pool,
|
||||
storage,
|
||||
brokerURL,
|
||||
@@ -80,7 +158,27 @@ func run() error {
|
||||
cfg.S3.MaxUploadBytes,
|
||||
cfg.S3.MaxImagePixels,
|
||||
log,
|
||||
).Run(ctx)
|
||||
)
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user