feat(lab5): durable async attachment pipeline through grade 4

- RabbitMQ quorum queue, persistent messages and publisher confirms
- S3 staging plus PostgreSQL attachments/jobs outbox transaction
- worker variants (thumbnail, crop, optimized WebP), DB lease and lease sweeper
- jittered retry, permanent failures, explicit replay and admission cap
- deterministic crash hooks, resilience script and evidence for grade 4
- async-aware custom tests, oversized pixel case, docs and changelog
This commit is contained in:
Mikhail Verkovykh
2026-11-15 00:30:36 +03:00
parent 28d68dc30b
commit 9e43faa536
48 changed files with 4077 additions and 333 deletions
+102
View File
@@ -9,6 +9,8 @@ import (
"strconv"
"strings"
"time"
"gitflic.ru/project/bulla-max/mtusi2026lab/internal/model"
)
const (
@@ -60,6 +62,26 @@ type Config struct {
ReconcileGrace time.Duration
ReconcileInterval time.Duration
// Lab5 asynchronous pipeline. BrokerURL is required by the API (outbox
// publisher) and the worker (consumer), but not by the reconciler.
BrokerURL string
WorkerOnly bool
JobQueue string
JobMaxAttempts int
JobTimeout time.Duration
JobLease time.Duration
RetryBase time.Duration
RetryMax time.Duration
WorkerConcurrency int
WorkerPrefetch int
MaxPendingJobs int
LeaseSweep time.Duration
StagingRetention time.Duration
WorkTempDir string
WorkerFaultMode string
CrashAfterEffect bool
CrashAfterClaim bool
}
func Load() (Config, error) {
@@ -141,6 +163,75 @@ func Load() (Config, error) {
}
cfg.ReconcileInterval = time.Duration(reconcileInterval) * time.Second
if cfg.WorkerOnly, err = envBool("WORKER_ONLY", false); err != nil {
return Config{}, err
}
if cfg.CrashAfterEffect, err = envBool("WORKER_CRASH_AFTER_EFFECT", false); err != nil {
return Config{}, err
}
if cfg.CrashAfterClaim, err = envBool("WORKER_CRASH_AFTER_CLAIM", false); err != nil {
return Config{}, err
}
jobAttempts, err := envInt("JOB_MAX_ATTEMPTS", 5, 1, 100)
if err != nil {
return Config{}, err
}
jobTimeout, err := envInt("JOB_TIMEOUT_SECONDS", 60, 5, 3600)
if err != nil {
return Config{}, err
}
jobLease, err := envInt("JOB_LEASE_SECONDS", 90, 10, 7200)
if err != nil {
return Config{}, err
}
retryBase, err := envInt("JOB_RETRY_BASE_SECONDS", 2, 1, 3600)
if err != nil {
return Config{}, err
}
retryMax, err := envInt("JOB_RETRY_MAX_SECONDS", 30, 1, 7200)
if err != nil {
return Config{}, err
}
concurrency, err := envInt("WORKER_CONCURRENCY", 2, 1, 128)
if err != nil {
return Config{}, err
}
prefetch, err := envInt("WORKER_PREFETCH", 2, 1, 512)
if err != nil {
return Config{}, err
}
maxPending, err := envInt("MAX_PENDING_JOBS", 1000, 0, 0)
if err != nil {
return Config{}, err
}
leaseSweep, err := envInt("LEASE_SWEEP_SECONDS", 15, 1, 3600)
if err != nil {
return Config{}, err
}
stagingRetention, err := envInt("STAGING_RETENTION_SECONDS", 86400, 60, 0)
if err != nil {
return Config{}, err
}
cfg.JobQueue = envOr("JOB_QUEUE", model.JobTypeAttachmentProcessV1)
cfg.JobMaxAttempts = jobAttempts
cfg.JobTimeout = time.Duration(jobTimeout) * time.Second
cfg.JobLease = time.Duration(jobLease) * time.Second
cfg.RetryBase = time.Duration(retryBase) * time.Second
cfg.RetryMax = time.Duration(retryMax) * time.Second
cfg.WorkerConcurrency = concurrency
cfg.WorkerPrefetch = prefetch
cfg.MaxPendingJobs = maxPending
cfg.LeaseSweep = time.Duration(leaseSweep) * time.Second
cfg.StagingRetention = time.Duration(stagingRetention) * time.Second
cfg.WorkTempDir = envOr("WORK_TEMP_DIR", filepath.Join(os.TempDir(), "messenger-work"))
cfg.WorkerFaultMode = envOr("WORKER_FAULT_MODE", "none")
switch cfg.WorkerFaultMode {
case "none", "transient-once", "permanent-once":
default:
return Config{}, fmt.Errorf("WORKER_FAULT_MODE must be none, transient-once or permanent-once")
}
if cfg.DatabaseURL, err = secret("DATABASE_URL"); err != nil {
return Config{}, err
}
@@ -175,6 +266,17 @@ func Load() (Config, error) {
return cfg, nil
}
if cfg.BrokerURL, err = secret("BROKER_URL"); err != nil {
return Config{}, err
}
if cfg.BrokerURL == "" {
return Config{}, fmt.Errorf("BROKER_URL or BROKER_URL_FILE is required")
}
if cfg.WorkerOnly {
return cfg, nil
}
if cfg.CacheURL, err = secret("CACHE_URL"); err != nil {
return Config{}, err
}