package main import ( "context" "encoding/json" "flag" "fmt" "log/slog" "os" "os/signal" "syscall" "time" "github.com/google/uuid" "mtusi-messenger/internal/config" "mtusi-messenger/internal/imageproc" "mtusi-messenger/internal/logger" "mtusi-messenger/internal/storage/postgres" ) func main() { if len(os.Args) < 2 { usage() os.Exit(2) } switch os.Args[1] { case "failed", "replay", "reprocess", "duplicate", "inspect": default: usage() os.Exit(2) } if err := run(); err != nil { fmt.Fprintln(os.Stderr, "error:", err) os.Exit(1) } } func run() error { ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) defer stop() cfg, err := config.LoadOperator() if err != nil { return fmt.Errorf("load config: %w", err) } log := logger.New(logger.WithLevel(cfg.SlogLevel()), logger.WithFormat(logger.FormatConsole)) slog.SetDefault(log) ctx, cancel := context.WithTimeout(ctx, 30*time.Second) defer cancel() store, err := postgres.New(ctx, cfg.Postgres, time.Duration(cfg.Idempotency.TTLSeconds)*time.Second) if err != nil { return fmt.Errorf("connect database: %w", err) } defer store.Close() repo := store.Pipeline() switch os.Args[1] { case "failed": return runFailed(ctx, repo) case "replay": return runReplay(ctx, repo, cfg) case "reprocess": return runReprocess(ctx, repo, cfg) case "duplicate": return runDuplicate(ctx, repo) case "inspect": return runInspect(ctx, repo) default: return fmt.Errorf("unknown command %q", os.Args[1]) } } func usage() { fmt.Fprintln(os.Stderr, `usage: jobctl [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) error { flags := flag.NewFlagSet("failed", flag.ContinueOnError) limit := flags.Int("limit", 50, "maximum jobs") if err := flags.Parse(os.Args[2:]); err != nil { return err } jobs, err := repo.ListFailedJobs(ctx, *limit) if err != nil { return err } return printJSON(jobs) } func runReplay(ctx context.Context, repo *postgres.PipelineRepository, cfg config.Config) error { flags := flag.NewFlagSet("replay", flag.ContinueOnError) jobID := flags.String("job", "", "job id") attempts := flags.Int("attempts", cfg.Processing.MaxAttempts, "additional attempts") if err := flags.Parse(os.Args[2:]); err != nil { return err } id, err := uuid.Parse(*jobID) if err != nil { return fmt.Errorf("invalid job id: %w", err) } job, err := repo.ReplayJob(ctx, id, *attempts) if err != nil { return err } return printJSON(job) } func runReprocess(ctx context.Context, repo *postgres.PipelineRepository, cfg config.Config) error { flags := flag.NewFlagSet("reprocess", flag.ContinueOnError) attachmentID := flags.String("attachment", "", "attachment id") version := flags.Int("pipeline", 2, "target pipeline version") if err := flags.Parse(os.Args[2:]); err != nil { return err } if _, ok := imageproc.Pipeline(*version); !ok { return fmt.Errorf("unsupported pipeline version %d", *version) } id, err := uuid.Parse(*attachmentID) if err != nil { return fmt.Errorf("invalid attachment id: %w", err) } jobID, err := uuid.NewV7() if err != nil { return err } job, err := repo.Reprocess( ctx, id, *version, cfg.Processing.MaxAttempts, jobID, ) if err != nil { return err } return printJSON(job) } func runDuplicate(ctx context.Context, repo *postgres.PipelineRepository) error { flags := flag.NewFlagSet("duplicate", flag.ContinueOnError) attachmentID := flags.String("attachment", "", "attachment id") jobID := flags.String("job", "", "specific job id (old pipeline version)") if err := flags.Parse(os.Args[2:]); err != nil { return err } if *jobID != "" { id, err := uuid.Parse(*jobID) if err != nil { return fmt.Errorf("invalid job id: %w", err) } message, err := repo.DuplicateJob(ctx, id) if err != nil { return err } return printJSON(message) } id, err := uuid.Parse(*attachmentID) if err != nil { return fmt.Errorf("attachment or job id is required: %w", err) } message, err := repo.Duplicate(ctx, id) if err != nil { return err } return printJSON(message) } func runInspect(ctx context.Context, repo *postgres.PipelineRepository) error { flags := flag.NewFlagSet("inspect", flag.ContinueOnError) attachmentID := flags.String("attachment", "", "attachment id") if err := flags.Parse(os.Args[2:]); err != nil { return err } id, err := uuid.Parse(*attachmentID) if err != nil { return fmt.Errorf("invalid attachment id: %w", err) } attachment, err := repo.GetForID(ctx, id) if err != nil { return err } variants, err := repo.ListVariants(ctx, id, attachment.PipelineVersion) if err != nil { return err } var jobOutput any job, jobErr := repo.GetJobByAttachment(ctx, id, attachment.PipelineVersion) if jobErr == nil { jobOutput = job } return printJSON(map[string]any{ "attachment": attachment, "variants": variants, "job": jobOutput, }) } func printJSON(value any) error { encoder := json.NewEncoder(os.Stdout) encoder.SetIndent("", " ") if err := encoder.Encode(value); err != nil { return fmt.Errorf("write output: %w", err) } return nil }