From 20fdd31c128a101c31e19c3b8c8c5a54fb7cb30b 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: Fri, 13 Nov 2026 09:12:45 +0300 Subject: [PATCH] fix(worker): lab5 enforce pipeline version check on claim --- backend/cmd/worker/main.go | 4 ++-- backend/internal/domain/repository.go | 2 +- backend/internal/storage/postgres/attachments.go | 8 ++++---- backend/internal/worker/pipeline.go | 10 +++++----- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/backend/cmd/worker/main.go b/backend/cmd/worker/main.go index 26ffa06..b4fc91c 100644 --- a/backend/cmd/worker/main.go +++ b/backend/cmd/worker/main.go @@ -86,9 +86,9 @@ func main() { continue } - if err := pipeline.Process(ctx, msg.AttachmentID); err != nil { + if err := pipeline.Process(ctx, msg.AttachmentID, msg.PipelineVersion); err != nil { log.Printf("worker: ошибка %s: %v", msg.AttachmentID, err) - _ = d.Nack(false, true) // requeue + _ = d.Nack(false, true) } else { _ = d.Ack(false) } diff --git a/backend/internal/domain/repository.go b/backend/internal/domain/repository.go index 66d5142..5ca2014 100644 --- a/backend/internal/domain/repository.go +++ b/backend/internal/domain/repository.go @@ -59,7 +59,7 @@ type OutboxRepository interface { } type AttachmentJobRepository interface { - ClaimForProcessing(ctx context.Context, id string, lease time.Duration) (*Attachment, error) + ClaimForProcessing(ctx context.Context, id string, version int, 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 diff --git a/backend/internal/storage/postgres/attachments.go b/backend/internal/storage/postgres/attachments.go index 9c77802..56ebc65 100644 --- a/backend/internal/storage/postgres/attachments.go +++ b/backend/internal/storage/postgres/attachments.go @@ -195,15 +195,15 @@ func (r *AttachmentRepo) ExistingKeys(ctx context.Context, keys []string) ([]str return out, mapError(rows.Err()) } -func (r *AttachmentRepo) ClaimForProcessing(ctx context.Context, id string, lease time.Duration) (*domain.Attachment, error) { +func (r *AttachmentRepo) ClaimForProcessing(ctx context.Context, id string, version int, lease time.Duration) (*domain.Attachment, error) { const q = ` UPDATE attachments - SET status = 'processing', locked_until = now() + $2, updated_at = now(), attempts = attempts + 1 -- <--- ИНКРЕМЕНТ - WHERE id = $1::uuid AND status = 'queued' + SET status = 'processing', locked_until = now() + $2, updated_at = now(), attempts = attempts + 1 + WHERE id = $1::uuid AND status = 'queued' AND pipeline_version = $3 RETURNING id, chat_id, staging_key, pipeline_version, object_key, content_type, attempts ` var a domain.Attachment - err := r.pool.QueryRow(ctx, q, id, lease).Scan(&a.ID, &a.ChatID, &a.StagingKey, &a.PipelineVersion, &a.ObjectKey, &a.ContentType, &a.Attempts) + err := r.pool.QueryRow(ctx, q, id, lease, version).Scan(&a.ID, &a.ChatID, &a.StagingKey, &a.PipelineVersion, &a.ObjectKey, &a.ContentType, &a.Attempts) if err != nil { if err == pgx.ErrNoRows { return nil, nil diff --git a/backend/internal/worker/pipeline.go b/backend/internal/worker/pipeline.go index fbc98d8..cb84823 100644 --- a/backend/internal/worker/pipeline.go +++ b/backend/internal/worker/pipeline.go @@ -42,19 +42,19 @@ func NewPipeline( } } -func (p *Pipeline) Process(ctx context.Context, attachmentID string) error { - log.Printf("worker: начало обработки %s", attachmentID) +func (p *Pipeline) Process(ctx context.Context, attachmentID string, pipelineVersion int) error { + log.Printf("worker: начало обработки %s (версия пайплайна %d)", attachmentID, pipelineVersion) jobCtx, cancel := context.WithTimeout(ctx, 30*time.Second) defer cancel() - // 1. Забираем задачу (защита от дублей). Если уже взята или готова — nil. - a, err := p.jobs.ClaimForProcessing(jobCtx, attachmentID, p.lease) + // 1. Забираем задачу с проверкой версии + a, err := p.jobs.ClaimForProcessing(jobCtx, attachmentID, pipelineVersion, p.lease) if err != nil { return fmt.Errorf("захват задачи: %w", err) } if a == nil { - log.Printf("worker: задача %s уже взята или выполнена", attachmentID) + log.Printf("worker: задача %s уже взята, выполнена или версия устарела", attachmentID) return nil // no-op }