134 lines
4.2 KiB
Go
134 lines
4.2 KiB
Go
package main
|
||
|
||
import (
|
||
"context"
|
||
"encoding/json"
|
||
"flag"
|
||
"fmt"
|
||
"log/slog"
|
||
"os"
|
||
"os/signal"
|
||
"strings"
|
||
"syscall"
|
||
"time"
|
||
|
||
"highload/internal/config"
|
||
"highload/internal/database"
|
||
"highload/internal/database/sqlc"
|
||
"highload/internal/pipeline"
|
||
"highload/internal/repository/postgres"
|
||
"highload/internal/storage/s3"
|
||
)
|
||
|
||
func main() {
|
||
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
||
defer stop()
|
||
if err := run(ctx, os.Args[1:]); err != nil {
|
||
slog.Error("attachment jobs command failed", "error", err)
|
||
os.Exit(1)
|
||
}
|
||
}
|
||
|
||
func run(ctx context.Context, args []string) error {
|
||
const usage = "usage: jobs failed | replay | upgrade | cleanup"
|
||
if len(args) == 0 {
|
||
return fmt.Errorf("%s", usage)
|
||
}
|
||
flags := flag.NewFlagSet(args[0], flag.ContinueOnError)
|
||
job := flags.String("job-id", "", "failed job ID")
|
||
request := flags.String("request-id", "", "unique operator request UUID")
|
||
operator := flags.String("operator", "", "operator ID")
|
||
reason := flags.String("reason", "", "replay reason")
|
||
extra := flags.Int("extra-attempts", 1, "budget for replay (1–5)")
|
||
attachment := flags.String("attachment-id", "", "attachment ID to upgrade")
|
||
version := flags.Int("to-version", 0, "target pipeline version (2)")
|
||
dry := flags.Bool("dry-run", false, "report eligible objects without deleting")
|
||
once := flags.Bool("once", false, "execute one cleanup pass")
|
||
if err := flags.Parse(args[1:]); err != nil {
|
||
return err
|
||
}
|
||
command := args[0]
|
||
if command != "failed" && command != "cleanup" && command != "replay" && command != "upgrade" {
|
||
return fmt.Errorf("%s", usage)
|
||
}
|
||
if command == "replay" && (*job == "" || *request == "" || strings.TrimSpace(*operator) == "" ||
|
||
strings.TrimSpace(*reason) == "" || *extra < 1 || *extra > 5) {
|
||
return fmt.Errorf("replay needs job-id, request-id, operator, reason and 1–5 extra-attempts")
|
||
}
|
||
if command == "upgrade" && (*attachment == "" || *version != 2) {
|
||
return fmt.Errorf("upgrade needs attachment-id and to-version=2")
|
||
}
|
||
attemptLimit := 5
|
||
if command == "upgrade" {
|
||
var err error
|
||
attemptLimit, err = config.EnvInt("JOB_MAX_ATTEMPTS", 5, 1, 20)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
}
|
||
url, storageConfig, err := config.LoadAttachmentCleanup()
|
||
if err != nil {
|
||
return err
|
||
}
|
||
pool, err := database.NewPostgres(ctx, url)
|
||
if err != nil {
|
||
return fmt.Errorf("PostgreSQL unavailable")
|
||
}
|
||
defer pool.Close()
|
||
repo := postgres.NewAttachmentRepository(sqlc.New(pool), pool, int32(attemptLimit))
|
||
if command == "failed" {
|
||
dbCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||
defer cancel()
|
||
rows, err := repo.ListFailedJobs(dbCtx)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
type failed struct {
|
||
JobID string `json:"job_id"`
|
||
AttachmentID string `json:"attachment_id"`
|
||
ErrorCode string `json:"error_code"`
|
||
Attempts int32 `json:"attempts"`
|
||
UpdatedAt time.Time `json:"updated_at"`
|
||
}
|
||
result := make([]failed, 0, len(rows))
|
||
for _, row := range rows {
|
||
result = append(result, failed{row.ID.String(), row.AttachmentID.String(), row.ErrorCode.String, row.AttemptsStarted, row.UpdatedAt.Time})
|
||
}
|
||
return json.NewEncoder(os.Stdout).Encode(result)
|
||
}
|
||
objects, err := s3.NewStorage(ctx, &storageConfig)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
if command == "replay" {
|
||
changed, err := repo.ReplayFailed(ctx, *job, *request, *operator, *reason, int32(*extra), objects.Head)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
return json.NewEncoder(os.Stdout).Encode(map[string]any{"job_id": *job, "replayed": changed})
|
||
}
|
||
if command == "upgrade" {
|
||
jobID, queued, err := repo.UpgradePipeline(ctx, *attachment, objects.Head)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
return json.NewEncoder(os.Stdout).Encode(map[string]any{
|
||
"attachment_id": *attachment, "pipeline_version": *version, "job_id": jobID, "queued": queued,
|
||
})
|
||
}
|
||
cfg, err := config.LoadPipeline()
|
||
if err != nil {
|
||
return err
|
||
}
|
||
cleanup := pipeline.Cleanup{Repo: repo, Objects: objects, Grace: cfg.CleanupGrace, Retention: cfg.Retention, DryRun: *dry}
|
||
if *once {
|
||
count, err := cleanup.RunOnce(ctx)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
return json.NewEncoder(os.Stdout).Encode(map[string]any{"objects": count, "dry_run": *dry})
|
||
}
|
||
cleanup.Serve(ctx, cfg.CleanupPeriod)
|
||
return nil
|
||
}
|