lab5: асинхронная очередь, worker и идемпотентный pipeline
API принимает 202 после durable staging и TX intent (job+outbox); отдельные relay и worker публикуют/обрабатывают задания с publisher confirm, lease, retry/backoff, DLQ и replay. Добавлены версии pipeline, варианты thumbnail/crop/optimized, presign в verified staging, status-aware sweeper и admission control.
This commit is contained in:
@@ -0,0 +1,210 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"mtusi-messenger/internal/config"
|
||||
"mtusi-messenger/internal/logger"
|
||||
"mtusi-messenger/internal/storage/postgres"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if len(os.Args) < 2 {
|
||||
usage()
|
||||
os.Exit(2)
|
||||
}
|
||||
|
||||
cfg, err := config.Load()
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "load config:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
log := logger.New(logger.WithLevel(cfg.SlogLevel()), logger.WithFormat(logger.FormatConsole))
|
||||
slog.SetDefault(log)
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
|
||||
store, err := postgres.New(ctx, cfg.Postgres, time.Duration(cfg.Idempotency.TTLSeconds)*time.Second)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "connect database:", err)
|
||||
cancel()
|
||||
os.Exit(1) //nolint:gocritic // main exits after DB setup failure
|
||||
}
|
||||
defer store.Close()
|
||||
|
||||
repo := store.Pipeline()
|
||||
|
||||
switch os.Args[1] {
|
||||
case "failed":
|
||||
runFailed(ctx, repo)
|
||||
case "replay":
|
||||
runReplay(ctx, repo, cfg)
|
||||
case "reprocess":
|
||||
runReprocess(ctx, repo, cfg)
|
||||
case "duplicate":
|
||||
runDuplicate(ctx, repo)
|
||||
case "inspect":
|
||||
runInspect(ctx, repo)
|
||||
default:
|
||||
usage()
|
||||
os.Exit(2)
|
||||
}
|
||||
}
|
||||
|
||||
func usage() {
|
||||
fmt.Fprintln(os.Stderr, `usage: jobctl <command> [flags]
|
||||
|
||||
failed -limit N
|
||||
replay -job JOB_ID [-attempts N]
|
||||
reprocess -attachment ATTACHMENT_ID -pipeline N
|
||||
duplicate -attachment ATTACHMENT_ID | -job JOB_ID
|
||||
inspect -attachment ATTACHMENT_ID`)
|
||||
}
|
||||
|
||||
func runFailed(ctx context.Context, repo *postgres.PipelineRepository) {
|
||||
flags := flag.NewFlagSet("failed", flag.ExitOnError)
|
||||
limit := flags.Int("limit", 50, "maximum jobs")
|
||||
_ = flags.Parse(os.Args[2:])
|
||||
|
||||
jobs, err := repo.ListFailedJobs(ctx, *limit)
|
||||
if err != nil {
|
||||
fail(err)
|
||||
}
|
||||
|
||||
printJSON(jobs)
|
||||
}
|
||||
|
||||
func runReplay(ctx context.Context, repo *postgres.PipelineRepository, cfg config.Config) {
|
||||
flags := flag.NewFlagSet("replay", flag.ExitOnError)
|
||||
jobID := flags.String("job", "", "job id")
|
||||
attempts := flags.Int("attempts", cfg.Processing.MaxAttempts, "additional attempts")
|
||||
_ = flags.Parse(os.Args[2:])
|
||||
|
||||
id, err := uuid.Parse(*jobID)
|
||||
if err != nil {
|
||||
fail(fmt.Errorf("invalid job id: %w", err))
|
||||
}
|
||||
|
||||
job, err := repo.ReplayJob(ctx, id, *attempts)
|
||||
if err != nil {
|
||||
fail(err)
|
||||
}
|
||||
|
||||
printJSON(job)
|
||||
}
|
||||
|
||||
func runReprocess(ctx context.Context, repo *postgres.PipelineRepository, cfg config.Config) {
|
||||
flags := flag.NewFlagSet("reprocess", flag.ExitOnError)
|
||||
attachmentID := flags.String("attachment", "", "attachment id")
|
||||
version := flags.Int("pipeline", 2, "target pipeline version")
|
||||
_ = flags.Parse(os.Args[2:])
|
||||
|
||||
id, err := uuid.Parse(*attachmentID)
|
||||
if err != nil {
|
||||
fail(fmt.Errorf("invalid attachment id: %w", err))
|
||||
}
|
||||
|
||||
jobID, err := uuid.NewV7()
|
||||
if err != nil {
|
||||
fail(err)
|
||||
}
|
||||
|
||||
job, err := repo.Reprocess(ctx, id, *version, cfg.Processing.MaxAttempts, jobID)
|
||||
if err != nil {
|
||||
fail(err)
|
||||
}
|
||||
|
||||
printJSON(job)
|
||||
}
|
||||
|
||||
func runDuplicate(ctx context.Context, repo *postgres.PipelineRepository) {
|
||||
flags := flag.NewFlagSet("duplicate", flag.ExitOnError)
|
||||
attachmentID := flags.String("attachment", "", "attachment id")
|
||||
jobID := flags.String("job", "", "specific job id (old pipeline version)")
|
||||
_ = flags.Parse(os.Args[2:])
|
||||
|
||||
if *jobID != "" {
|
||||
id, err := uuid.Parse(*jobID)
|
||||
if err != nil {
|
||||
fail(fmt.Errorf("invalid job id: %w", err))
|
||||
}
|
||||
|
||||
message, err := repo.DuplicateJob(ctx, id)
|
||||
if err != nil {
|
||||
fail(err)
|
||||
}
|
||||
|
||||
printJSON(message)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
id, err := uuid.Parse(*attachmentID)
|
||||
if err != nil {
|
||||
fail(fmt.Errorf("attachment or job id is required: %w", err))
|
||||
}
|
||||
|
||||
message, err := repo.Duplicate(ctx, id)
|
||||
if err != nil {
|
||||
fail(err)
|
||||
}
|
||||
|
||||
printJSON(message)
|
||||
}
|
||||
|
||||
func runInspect(ctx context.Context, repo *postgres.PipelineRepository) {
|
||||
flags := flag.NewFlagSet("inspect", flag.ExitOnError)
|
||||
attachmentID := flags.String("attachment", "", "attachment id")
|
||||
_ = flags.Parse(os.Args[2:])
|
||||
|
||||
id, err := uuid.Parse(*attachmentID)
|
||||
if err != nil {
|
||||
fail(fmt.Errorf("invalid attachment id: %w", err))
|
||||
}
|
||||
|
||||
attachment, err := repo.GetForID(ctx, id)
|
||||
if err != nil {
|
||||
fail(err)
|
||||
}
|
||||
|
||||
variants, err := repo.ListVariants(ctx, id, attachment.PipelineVersion)
|
||||
if err != nil {
|
||||
fail(err)
|
||||
}
|
||||
|
||||
var jobOutput any
|
||||
|
||||
job, jobErr := repo.GetJobByAttachment(ctx, id, attachment.PipelineVersion)
|
||||
if jobErr == nil {
|
||||
jobOutput = job
|
||||
}
|
||||
|
||||
printJSON(map[string]any{
|
||||
"attachment": attachment,
|
||||
"variants": variants,
|
||||
"job": jobOutput,
|
||||
})
|
||||
}
|
||||
|
||||
func printJSON(value any) {
|
||||
encoder := json.NewEncoder(os.Stdout)
|
||||
encoder.SetIndent("", " ")
|
||||
if err := encoder.Encode(value); err != nil {
|
||||
fail(err)
|
||||
}
|
||||
}
|
||||
|
||||
func fail(err error) {
|
||||
fmt.Fprintln(os.Stderr, "error:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
Reference in New Issue
Block a user