ЛР 5 — БВТ2303 — Голиков — на 5 #69

Closed
Maximillan_Mercer wants to merge 46 commits from Maximillan_Mercer/mtusi2026lab5-fork:solution/BVT2303-Golikov into main
4 changed files with 12 additions and 12 deletions
Showing only changes of commit 20fdd31c12 - Show all commits
+2 -2
View File
@@ -86,9 +86,9 @@ func main() {
continue 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) log.Printf("worker: ошибка %s: %v", msg.AttachmentID, err)
_ = d.Nack(false, true) // requeue _ = d.Nack(false, true)
} else { } else {
_ = d.Ack(false) _ = d.Ack(false)
} }
+1 -1
View File
@@ -59,7 +59,7 @@ type OutboxRepository interface {
} }
type AttachmentJobRepository 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) CompleteJob(ctx context.Context, id string, pipelineVersion int, variants []AttachmentVariant) (bool, error)
FailJob(ctx context.Context, id string, errorCode string) error FailJob(ctx context.Context, id string, errorCode string) error
ScheduleRetry(ctx context.Context, id string, nextAttemptAt time.Time, 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()) 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 = ` const q = `
UPDATE attachments UPDATE attachments
SET status = 'processing', locked_until = now() + $2, updated_at = now(), attempts = attempts + 1 -- <--- ИНКРЕМЕНТ SET status = 'processing', locked_until = now() + $2, updated_at = now(), attempts = attempts + 1
WHERE id = $1::uuid AND status = 'queued' WHERE id = $1::uuid AND status = 'queued' AND pipeline_version = $3
RETURNING id, chat_id, staging_key, pipeline_version, object_key, content_type, attempts RETURNING id, chat_id, staging_key, pipeline_version, object_key, content_type, attempts
` `
var a domain.Attachment 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 != nil {
if err == pgx.ErrNoRows { if err == pgx.ErrNoRows {
return nil, nil return nil, nil
+5 -5
View File
@@ -42,19 +42,19 @@ func NewPipeline(
} }
} }
func (p *Pipeline) Process(ctx context.Context, attachmentID string) error { func (p *Pipeline) Process(ctx context.Context, attachmentID string, pipelineVersion int) error {
log.Printf("worker: начало обработки %s", attachmentID) log.Printf("worker: начало обработки %s (версия пайплайна %d)", attachmentID, pipelineVersion)
jobCtx, cancel := context.WithTimeout(ctx, 30*time.Second) jobCtx, cancel := context.WithTimeout(ctx, 30*time.Second)
defer cancel() defer cancel()
// 1. Забираем задачу (защита от дублей). Если уже взята или готова — nil. // 1. Забираем задачу с проверкой версии
a, err := p.jobs.ClaimForProcessing(jobCtx, attachmentID, p.lease) a, err := p.jobs.ClaimForProcessing(jobCtx, attachmentID, pipelineVersion, p.lease)
if err != nil { if err != nil {
return fmt.Errorf("захват задачи: %w", err) return fmt.Errorf("захват задачи: %w", err)
} }
if a == nil { if a == nil {
log.Printf("worker: задача %s уже взята или выполнена", attachmentID) log.Printf("worker: задача %s уже взята, выполнена или версия устарела", attachmentID)
return nil // no-op return nil // no-op
} }