fix(worker): lab5 enforce pipeline version check on claim

This commit is contained in:
Голиков Maximillian Антон
2026-11-13 09:12:45 +03:00
parent d40b339906
commit 20fdd31c12
4 changed files with 12 additions and 12 deletions
+2 -2
View File
@@ -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)
}
+1 -1
View File
@@ -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
@@ -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
+5 -5
View File
@@ -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
}