Files
mtusi2026lab7/cmd/worker/main.go
T
Khakimov Dmitrij 2a7418fc45
Skeleton contracts / contracts (pull_request) Blocked by required conditions
lab 7 done
2026-11-29 21:59:16 +03:00

128 lines
4.0 KiB
Go

package main
import (
"context"
"encoding/json"
"errors"
"flag"
"gitflic.ru/akselbant/mtusi2026lab1-fork/internal/attachment"
"gitflic.ru/akselbant/mtusi2026lab1-fork/internal/config"
"gitflic.ru/akselbant/mtusi2026lab1-fork/internal/observability"
"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()
shutdownTelemetry, telemetryErr := observability.Init(context.Background(), "messenger-worker")
if telemetryErr != nil {
log.Fatal("telemetry configuration failed")
}
defer shutdownTelemetry()
cfg, err := config.LoadService()
if err != nil {
log.Fatal(err)
}
options, err := attachment.LoadOptions()
if err != nil {
log.Fatal(err)
}
pc, err := cfg.PostgresConfig()
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.ConnConfig.Tracer = observability.DBTracer{}
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()
observability.ObservePool(pool, options.Concurrency)
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
}
redisOptions, err := cfg.RedisOptions()
if err != nil {
log.Fatal("invalid Redis TLS configuration")
}
r := redis.NewClient(redisOptions)
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):
}
}
}