Files
2026-11-10 22:13:40 +03:00

116 lines
3.7 KiB
Go

package main
import (
"context"
"encoding/json"
"errors"
"flag"
"gitflic.ru/akselbant/mtusi2026lab1-fork/internal/attachment"
"gitflic.ru/akselbant/mtusi2026lab1-fork/internal/config"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/redis/go-redis/v9"
"log"
"os"
"os/signal"
"syscall"
"time"
)
func main() {
if len(os.Args) > 1 && os.Args[1] == "render" {
if len(os.Args) != 5 {
os.Exit(2)
}
if err := attachment.Render(context.Background(), os.Args[2], os.Args[3], os.Args[4]); err != nil {
if errors.Is(err, attachment.ErrInvalidImage) {
os.Exit(2)
}
os.Exit(1)
}
return
}
failed := flag.Bool("failed", false, "list failed job registry")
replay := flag.String("replay", "", "failed job UUID to replay")
operator := flag.String("operator", "", "operator identity")
reason := flag.String("reason", "", "replay reason")
flag.Parse()
cfg, err := config.Load()
if err != nil {
log.Fatal(err)
}
options, err := attachment.LoadOptions()
if err != nil {
log.Fatal(err)
}
pc, err := pgxpool.ParseConfig("sslmode=disable")
if err != nil {
log.Fatal("invalid DB config")
}
pc.ConnConfig.Host = cfg.PostgresHost
pc.ConnConfig.Port = cfg.PostgresPort
pc.ConnConfig.User = cfg.PostgresUser
pc.ConnConfig.Password = cfg.PostgresPassword
pc.ConnConfig.Database = cfg.PostgresDatabase
pc.ConnConfig.ConnectTimeout = 3 * time.Second
pc.ConnConfig.RuntimeParams["timezone"] = "UTC"
pc.ConnConfig.RuntimeParams["statement_timeout"] = "5000"
pc.MaxConns = int32(options.Concurrency + 4)
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer cancel()
pool, err := pgxpool.NewWithConfig(ctx, pc)
if err != nil {
log.Fatal("DB configuration failed")
}
defer pool.Close()
store, err := attachment.NewStore(cfg.S3Endpoint, cfg.S3Bucket, cfg.S3AccessKey, cfg.S3SecretKey, cfg.S3UseSSL)
if err != nil {
log.Fatal("S3 configuration failed")
}
service := attachment.NewAsync(pool, store, options)
if *failed {
rows, e := pool.Query(ctx, `SELECT id::text,attachment_id::text,attempts,max_attempts,coalesce(last_error,''),updated_at FROM attachment_jobs WHERE state='failed' ORDER BY updated_at DESC LIMIT 100`)
if e != nil {
log.Fatal("cannot query failed jobs")
}
defer rows.Close()
records := []map[string]interface{}{}
for rows.Next() {
var id, att, code string
var attempt, max int
var updated time.Time
if e = rows.Scan(&id, &att, &attempt, &max, &code, &updated); e != nil {
log.Fatal("invalid failed job")
}
records = append(records, map[string]interface{}{"job_id": id, "attachment_id": att, "attempts": attempt, "max_attempts": max, "error_code": code, "updated_at": updated.UTC()})
}
if rows.Err() != nil {
log.Fatal("failed jobs query interrupted")
}
json.NewEncoder(os.Stdout).Encode(records)
return
}
if *replay != "" {
operation, stop := context.WithTimeout(ctx, 10*time.Second)
defer stop()
if err = service.Replay(operation, *replay, *operator, *reason); err != nil {
log.Fatal(err)
}
log.Print("Replay committed with audit record")
return
}
r := redis.NewClient(&redis.Options{Addr: cfg.RedisAddr, DialTimeout: 2 * time.Second, ReadTimeout: 2 * time.Second, WriteTimeout: 2 * time.Second, MaxRetries: -1, ContextTimeoutEnabled: true})
defer r.Close()
log.Printf("Worker started: concurrency=%d prefetch=%d timeout=%s lease=%s pixel_budget=%d", options.Concurrency, options.Prefetch, options.Timeout, options.Lease, options.PixelBudget)
for ctx.Err() == nil {
err = service.RunWorker(ctx, r)
if err != nil {
log.Print("worker broker connection will retry")
}
select {
case <-ctx.Done():
return
case <-time.After(time.Second):
}
}
}