From b022f8f7fc5073369a8e4ad322ee1eb8968282ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=BE=D0=BB=D0=B8=D0=BA=D0=BE=D0=B2=20Maximillian?= =?UTF-8?q?=20=D0=90=D0=BD=D1=82=D0=BE=D0=BD?= Date: Tue, 10 Nov 2026 11:02:57 +0300 Subject: [PATCH] feat(db): lab5 add failed_jobs table and repository --- backend/internal/domain/failed_jobs.go | 17 ++++++++ backend/internal/domain/repository.go | 6 +++ .../internal/storage/postgres/failed_jobs.go | 42 +++++++++++++++++++ backend/internal/storage/postgres/storage.go | 1 + backend/migrations/0009_failed_jobs.sql | 25 +++++++++++ 5 files changed, 91 insertions(+) create mode 100644 backend/internal/domain/failed_jobs.go create mode 100644 backend/internal/storage/postgres/failed_jobs.go create mode 100644 backend/migrations/0009_failed_jobs.sql diff --git a/backend/internal/domain/failed_jobs.go b/backend/internal/domain/failed_jobs.go new file mode 100644 index 0000000..b3f177c --- /dev/null +++ b/backend/internal/domain/failed_jobs.go @@ -0,0 +1,17 @@ +package domain + +import "time" + +// FailedJob — реестр окончательно проваленных задач (DLQ-сторона). +// Заполняется worker'ом после исчерпания retry или при permanent-ошибке. +// Читается для операторского просмотра и для явного replay (Этап 5). +type FailedJob struct { + ID string + AttachmentID string + ReasonCode string // из jobcodes.go + Attempts int + LastErrorAt time.Time + ResolvedAt *time.Time // nil = не разобрана + Resolution string // "replayed", "manual_fix", ... + CreatedAt time.Time +} diff --git a/backend/internal/domain/repository.go b/backend/internal/domain/repository.go index 6ace771..6fa4c78 100644 --- a/backend/internal/domain/repository.go +++ b/backend/internal/domain/repository.go @@ -57,9 +57,15 @@ type AttachmentJobRepository interface { ClaimForProcessing(ctx context.Context, id string, lease time.Duration) (*Attachment, error) CompleteJob(ctx context.Context, id string, pipelineVersion int, variants []AttachmentVariant) (bool, error) FailJob(ctx context.Context, id string, errorCode string) error + ScheduleRetry(ctx context.Context, id string, nextAttemptAt time.Time, errorCode string) error } type AttachmentVariantRepository interface { Upsert(ctx context.Context, tx Tx, v *AttachmentVariant) error ListByAttachment(ctx context.Context, id string) ([]AttachmentVariant, error) } + +type FailedJobRepository interface { + Record(ctx context.Context, f *FailedJob) error + Resolve(ctx context.Context, attachmentID string, resolution string) error +} diff --git a/backend/internal/storage/postgres/failed_jobs.go b/backend/internal/storage/postgres/failed_jobs.go new file mode 100644 index 0000000..9ddcbc4 --- /dev/null +++ b/backend/internal/storage/postgres/failed_jobs.go @@ -0,0 +1,42 @@ +package postgres + +import ( + "context" + "time" + + "github.com/google/uuid" + "github.com/jackc/pgx/v5/pgxpool" + + "high-loud-apps-labs/internal/domain" +) + +type FailedJobRepo struct { + pool *pgxpool.Pool +} + +func NewFailedJobRepo(pool *pgxpool.Pool) *FailedJobRepo { + return &FailedJobRepo{pool: pool} +} + +func (r *FailedJobRepo) Record(ctx context.Context, f *domain.FailedJob) error { + if f.ID == "" { + f.ID = uuid.NewString() + } + if f.LastErrorAt.IsZero() { + f.LastErrorAt = time.Now().UTC() + } + const q = ` + INSERT INTO failed_jobs (id, attachment_id, reason_code, attempts, last_error_at, created_at) + VALUES ($1::uuid, $2::uuid, $3, $4, $5, $5) + ` + _, err := r.pool.Exec(ctx, q, f.ID, f.AttachmentID, f.ReasonCode, f.Attempts, f.LastErrorAt) + return mapError(err) +} + +func (r *FailedJobRepo) Resolve(ctx context.Context, attachmentID string, resolution string) error { + const q = `UPDATE failed_jobs SET resolved_at = now(), resolution = $2 WHERE attachment_id = $1::uuid AND resolved_at IS NULL` + _, err := r.pool.Exec(ctx, q, attachmentID, resolution) + return mapError(err) +} + +var _ domain.FailedJobRepository = (*FailedJobRepo)(nil) diff --git a/backend/internal/storage/postgres/storage.go b/backend/internal/storage/postgres/storage.go index db660c2..2a6110d 100644 --- a/backend/internal/storage/postgres/storage.go +++ b/backend/internal/storage/postgres/storage.go @@ -19,3 +19,4 @@ func (s *Storage) Sessions() *SessionRepo { return &SessionRepo{pool: func (s *Storage) Attachments() *AttachmentRepo { return &AttachmentRepo{pool: s.pool} } func (s *Storage) Outbox() *OutboxRepo { return &OutboxRepo{pool: s.pool} } func (s *Storage) Variants() *AttachmentVariantRepo { return &AttachmentVariantRepo{pool: s.pool} } +func (s *Storage) FailedJobs() *FailedJobRepo { return &FailedJobRepo{pool: s.pool} } diff --git a/backend/migrations/0009_failed_jobs.sql b/backend/migrations/0009_failed_jobs.sql new file mode 100644 index 0000000..21ae264 --- /dev/null +++ b/backend/migrations/0009_failed_jobs.sql @@ -0,0 +1,25 @@ +-- +goose Up +-- +goose StatementBegin + +CREATE TABLE failed_jobs ( + id UUID PRIMARY KEY, + attachment_id UUID NOT NULL REFERENCES attachments(id) ON DELETE CASCADE, + reason_code TEXT NOT NULL, + attempts INT NOT NULL, + last_error_at TIMESTAMPTZ NOT NULL DEFAULT now(), + resolved_at TIMESTAMPTZ, + resolution TEXT, + created_at TIMESTAMPTZ NOT NULL DEFAULT now(), + + CONSTRAINT failed_jobs_reason_len CHECK (char_length(reason_code) BETWEEN 1 AND 64), + CONSTRAINT failed_jobs_attempts_pos CHECK (attempts > 0) +); + +CREATE INDEX failed_jobs_unresolved_idx ON failed_jobs (created_at) WHERE resolved_at IS NULL; + +-- +goose StatementEnd + +-- +goose Down +-- +goose StatementBegin +DROP TABLE IF EXISTS failed_jobs; +-- +goose StatementEnd \ No newline at end of file