package postgres import ( "context" "encoding/json" "errors" "fmt" "time" "github.com/google/uuid" "github.com/jackc/pgx/v5" "gitea.privcloud.tech/dizi/mtusi2026lab6/internal/model" ) const attachmentJobTopic = "attachment.jobs" const attachmentJobColumns = ` id, attachment_id, pipeline_version, attempt, status, available_at, lease_token, lease_owner, lease_until, error_code, created_at, updated_at` func (s *Store) QueueAttachmentWithJobAndOutbox( ctx context.Context, attachmentID string, stagingKey string, job model.AttachmentJob, traceparent string, tracestate string, ) (model.Attachment, error) { if job.ID == "" || job.PipelineVersion < 1 { return model.Attachment{}, model.ErrInvalidInput } if job.Attempt == 0 { job.Attempt = 1 } if job.Attempt != 1 { return model.Attachment{}, model.ErrInvalidInput } now := time.Now().UTC() if job.CreatedAt.IsZero() { job.CreatedAt = now } if job.AvailableAt.IsZero() { job.AvailableAt = now } tx, err := s.pool.BeginTx(ctx, pgx.TxOptions{}) if err != nil { return model.Attachment{}, fmt.Errorf("begin queue attachment transaction: %w", err) } defer func() { _ = tx.Rollback(context.WithoutCancel(ctx)) }() query := ` UPDATE attachments SET status = 'queued', pipeline_version = $3, staging_key = $2, ready_at = NULL, error_code = NULL, updated_at = clock_timestamp() WHERE id = $1 AND object_key = $2 AND status = 'pending' RETURNING ` + attachmentColumns var attachment model.Attachment err = scanAttachment(tx.QueryRow(ctx, query, attachmentID, stagingKey, job.PipelineVersion), &attachment) if errors.Is(err, pgx.ErrNoRows) { return s.alreadyQueuedAttachment(ctx, tx, attachmentID, stagingKey, job) } if err != nil { return model.Attachment{}, fmt.Errorf("queue attachment: %w", err) } job.AttachmentID = attachmentID job.Status = model.AttachmentJobQueued job.UpdatedAt = job.CreatedAt if err := insertAttachmentJob(ctx, tx, job); err != nil { return model.Attachment{}, err } if err := insertAttachmentJobOutbox(ctx, tx, job, traceparent, tracestate); err != nil { return model.Attachment{}, err } if err := tx.Commit(ctx); err != nil { return model.Attachment{}, fmt.Errorf("commit queued attachment: %w", err) } return publicAttachment(attachment), nil } func (s *Store) QueueAttachmentPipelineVersion( ctx context.Context, attachmentID string, job model.AttachmentJob, traceparent string, tracestate string, ) (model.Attachment, error) { if job.ID == "" || job.PipelineVersion < 2 { return model.Attachment{}, model.ErrInvalidInput } job.Attempt = 1 now := time.Now().UTC() job.CreatedAt = now job.AvailableAt = now tx, err := s.pool.BeginTx(ctx, pgx.TxOptions{}) if err != nil { return model.Attachment{}, fmt.Errorf("begin queue attachment pipeline version: %w", err) } defer func() { _ = tx.Rollback(context.WithoutCancel(ctx)) }() const selectAttachment = `SELECT ` + attachmentColumns + ` FROM attachments WHERE id = $1 FOR UPDATE` var attachment model.Attachment if err := scanAttachment(tx.QueryRow(ctx, selectAttachment, attachmentID), &attachment); err != nil { if errors.Is(err, pgx.ErrNoRows) { return model.Attachment{}, ErrNotFound } return model.Attachment{}, fmt.Errorf("lock attachment for pipeline update: %w", err) } if attachment.Status != model.AttachmentReady || job.PipelineVersion != attachment.PipelineVersion+1 { return model.Attachment{}, ErrConflict } job.AttachmentID = attachment.ID job.Status = model.AttachmentJobQueued job.UpdatedAt = now const queueAttachment = ` UPDATE attachments SET status = 'queued', pipeline_version = $2, staging_key = object_key, ready_at = NULL, error_code = NULL, updated_at = clock_timestamp() WHERE id = $1 RETURNING ` + attachmentColumns if err := scanAttachment(tx.QueryRow(ctx, queueAttachment, attachment.ID, job.PipelineVersion), &attachment); err != nil { return model.Attachment{}, fmt.Errorf("queue attachment pipeline version: %w", err) } if err := insertAttachmentJob(ctx, tx, job); err != nil { return model.Attachment{}, err } if err := insertAttachmentJobOutbox(ctx, tx, job, traceparent, tracestate); err != nil { return model.Attachment{}, err } if err := tx.Commit(ctx); err != nil { return model.Attachment{}, fmt.Errorf("commit queued attachment pipeline version: %w", err) } return publicAttachment(attachment), nil } func (s *Store) alreadyQueuedAttachment( ctx context.Context, tx pgx.Tx, attachmentID string, stagingKey string, job model.AttachmentJob, ) (model.Attachment, error) { query := ` SELECT ` + attachmentColumnsWithAlias("a") + ` FROM attachments a JOIN attachment_jobs j ON j.attachment_id = a.id AND j.pipeline_version = a.pipeline_version WHERE a.id = $1 AND a.staging_key = $2 AND a.status IN ('queued', 'processing', 'ready') AND j.id = $3 AND j.pipeline_version = $4` var attachment model.Attachment if err := scanAttachment(tx.QueryRow(ctx, query, attachmentID, stagingKey, job.ID, job.PipelineVersion), &attachment); err != nil { if errors.Is(err, pgx.ErrNoRows) { return model.Attachment{}, ErrConflict } return model.Attachment{}, fmt.Errorf("find queued attachment: %w", err) } if err := tx.Commit(ctx); err != nil { return model.Attachment{}, fmt.Errorf("commit repeated queue attachment: %w", err) } return publicAttachment(attachment), nil } func insertAttachmentJob(ctx context.Context, tx pgx.Tx, job model.AttachmentJob) error { const query = ` INSERT INTO attachment_jobs ( id, attachment_id, pipeline_version, attempt, status, available_at, created_at, updated_at ) VALUES ($1, $2, $3, $4, 'queued', $5, $6, $6)` if _, err := tx.Exec( ctx, query, job.ID, job.AttachmentID, job.PipelineVersion, job.Attempt, job.AvailableAt, job.CreatedAt, ); err != nil { return fmt.Errorf("insert attachment job: %w", err) } return nil } func insertAttachmentJobOutbox( ctx context.Context, tx pgx.Tx, job model.AttachmentJob, traceparent string, tracestate string, ) error { payload, err := json.Marshal(model.AttachmentJobMessage{ SchemaVersion: 1, JobID: job.ID, Type: "attachment.process.v1", AttachmentID: job.AttachmentID, PipelineVersion: job.PipelineVersion, Attempt: job.Attempt, CreatedAt: job.CreatedAt, Traceparent: traceparent, Tracestate: tracestate, }) if err != nil { return fmt.Errorf("marshal attachment job event: %w", err) } const query = ` INSERT INTO outbox_events ( id, topic, partition_key, payload, job_id, attempt, available_at ) VALUES ($1, $2, $3, $4, $5, $6, $7)` if _, err := tx.Exec( ctx, query, uuid.NewString(), attachmentJobTopic, job.AttachmentID, payload, job.ID, job.Attempt, job.AvailableAt, ); err != nil { return fmt.Errorf("insert attachment job outbox event: %w", err) } return nil } func (s *Store) AttachmentJob(ctx context.Context, jobID string) (model.AttachmentJob, error) { const query = `SELECT ` + attachmentJobColumns + ` FROM attachment_jobs WHERE id = $1` var job model.AttachmentJob if err := scanAttachmentJob(s.pool.QueryRow(ctx, query, jobID), &job); err != nil { if errors.Is(err, pgx.ErrNoRows) { return model.AttachmentJob{}, ErrNotFound } return model.AttachmentJob{}, fmt.Errorf("get attachment job: %w", err) } return job, nil } func (s *Store) ClaimAttachmentJob( ctx context.Context, jobID string, attachmentID string, pipelineVersion int, attempt int, workerID string, leaseDuration time.Duration, ) (model.AttachmentJobLease, error) { if leaseDuration <= 0 || workerID == "" { return model.AttachmentJobLease{}, model.ErrInvalidInput } tx, err := s.pool.BeginTx(ctx, pgx.TxOptions{}) if err != nil { return model.AttachmentJobLease{}, fmt.Errorf("begin claim attachment job: %w", err) } defer func() { _ = tx.Rollback(context.WithoutCancel(ctx)) }() job, attachment, err := lockAttachmentJob(ctx, tx, jobID) if err != nil { return model.AttachmentJobLease{}, err } if attachment.ID != attachmentID || job.AttachmentID != attachmentID { return model.AttachmentJobLease{}, model.ErrInvalidInput } if attachment.PipelineVersion != pipelineVersion || job.PipelineVersion != pipelineVersion { return model.AttachmentJobLease{}, model.ErrStalePipeline } if job.Status == model.AttachmentJobReady || attachment.Status == model.AttachmentReady { return model.AttachmentJobLease{}, model.ErrJobComplete } if job.Attempt != attempt { return model.AttachmentJobLease{}, model.ErrStaleAttempt } if job.Status == model.AttachmentJobProcessing { return model.AttachmentJobLease{}, model.ErrJobBusy } if job.Status != model.AttachmentJobQueued { return model.AttachmentJobLease{}, ErrConflict } if job.AvailableAt.After(time.Now()) { return model.AttachmentJobLease{}, model.ErrJobNotDue } job.LeaseToken = uuid.NewString() job.LeaseOwner = workerID leaseUntil := time.Now().UTC().Add(leaseDuration) job.LeaseUntil = &leaseUntil job.Status = model.AttachmentJobProcessing const updateJob = ` UPDATE attachment_jobs SET status = 'processing', lease_token = $2, lease_owner = $3, lease_until = $4, error_code = NULL, updated_at = clock_timestamp() WHERE id = $1` if _, err := tx.Exec(ctx, updateJob, job.ID, job.LeaseToken, job.LeaseOwner, leaseUntil); err != nil { return model.AttachmentJobLease{}, fmt.Errorf("claim attachment job: %w", err) } const updateAttachment = ` UPDATE attachments SET status = 'processing', error_code = NULL, updated_at = clock_timestamp() WHERE id = $1 AND pipeline_version = $2 AND status = 'queued'` result, err := tx.Exec(ctx, updateAttachment, attachment.ID, pipelineVersion) if err != nil { return model.AttachmentJobLease{}, fmt.Errorf("mark attachment processing: %w", err) } if result.RowsAffected() != 1 { return model.AttachmentJobLease{}, model.ErrStalePipeline } if err := tx.Commit(ctx); err != nil { return model.AttachmentJobLease{}, fmt.Errorf("commit attachment job claim: %w", err) } attachment.Status = model.AttachmentProcessing attachment.ErrorCode = nil return model.AttachmentJobLease{Job: job, Attachment: attachment}, nil } func lockAttachmentJob(ctx context.Context, tx pgx.Tx, jobID string) (model.AttachmentJob, model.Attachment, error) { query := ` SELECT ` + prefixedAttachmentJobColumns("j") + `, ` + attachmentColumnsWithAlias("a") + ` FROM attachment_jobs j JOIN attachments a ON a.id = j.attachment_id WHERE j.id = $1 FOR UPDATE OF j, a` var job model.AttachmentJob var attachment model.Attachment var leaseToken, leaseOwner *string var stagingKey *string err := tx.QueryRow(ctx, query, jobID).Scan( &job.ID, &job.AttachmentID, &job.PipelineVersion, &job.Attempt, &job.Status, &job.AvailableAt, &leaseToken, &leaseOwner, &job.LeaseUntil, &job.ErrorCode, &job.CreatedAt, &job.UpdatedAt, &attachment.ID, &attachment.ChatID, &attachment.OwnerID, &attachment.Filename, &attachment.ContentType, &attachment.SizeBytes, &attachment.SHA256, &attachment.ObjectKey, &attachment.Status, &attachment.CreatedAt, &attachment.UpdatedAt, &attachment.ReadyAt, &attachment.PipelineVersion, &attachment.ErrorCode, &stagingKey, ) if errors.Is(err, pgx.ErrNoRows) { return model.AttachmentJob{}, model.Attachment{}, ErrNotFound } if err != nil { return model.AttachmentJob{}, model.Attachment{}, fmt.Errorf("lock attachment job: %w", err) } if leaseToken != nil { job.LeaseToken = *leaseToken } if leaseOwner != nil { job.LeaseOwner = *leaseOwner } if stagingKey != nil { attachment.StagingKey = *stagingKey } return job, attachment, nil } func prefixedAttachmentJobColumns(alias string) string { return fmt.Sprintf( "%[1]s.id, %[1]s.attachment_id, %[1]s.pipeline_version, %[1]s.attempt, %[1]s.status, "+ "%[1]s.available_at, %[1]s.lease_token, %[1]s.lease_owner, %[1]s.lease_until, "+ "%[1]s.error_code, %[1]s.created_at, %[1]s.updated_at", alias, ) } func scanAttachmentJob(row rowScanner, job *model.AttachmentJob) error { var leaseToken, leaseOwner *string err := row.Scan( &job.ID, &job.AttachmentID, &job.PipelineVersion, &job.Attempt, &job.Status, &job.AvailableAt, &leaseToken, &leaseOwner, &job.LeaseUntil, &job.ErrorCode, &job.CreatedAt, &job.UpdatedAt, ) if leaseToken != nil { job.LeaseToken = *leaseToken } if leaseOwner != nil { job.LeaseOwner = *leaseOwner } return err } func (s *Store) CompleteAttachmentJob( ctx context.Context, lease model.AttachmentJobLease, variants []model.Variant, ) (model.JobMutationResult, error) { if !validCompletedVariants(variants, lease.Job.PipelineVersion) { return "", model.ErrInvalidInput } tx, err := s.pool.BeginTx(ctx, pgx.TxOptions{}) if err != nil { return "", fmt.Errorf("begin complete attachment job: %w", err) } defer func() { _ = tx.Rollback(context.WithoutCancel(ctx)) }() job, attachment, result, err := lockJobMutation(ctx, tx, lease) if err != nil || result != model.JobMutationApplied { return result, err } const deleteVariants = ` DELETE FROM attachment_variants WHERE attachment_id = $1 AND pipeline_version = $2` if _, err := tx.Exec(ctx, deleteVariants, attachment.ID, job.PipelineVersion); err != nil { return "", fmt.Errorf("replace attachment variants: %w", err) } var originalKey string const insertVariant = ` INSERT INTO attachment_variants ( attachment_id, pipeline_version, name, content_type, size_bytes, sha256, width, height, object_key ) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)` for _, variant := range variants { if variant.Name == "original" { originalKey = variant.ObjectKey } if _, err := tx.Exec( ctx, insertVariant, attachment.ID, job.PipelineVersion, variant.Name, variant.ContentType, variant.SizeBytes, variant.SHA256, variant.Width, variant.Height, variant.ObjectKey, ); err != nil { return "", fmt.Errorf("insert attachment variant %s: %w", variant.Name, err) } } const completeJob = ` UPDATE attachment_jobs SET status = 'ready', lease_token = NULL, lease_owner = NULL, lease_until = NULL, error_code = NULL, updated_at = clock_timestamp() WHERE id = $1 AND lease_token = $2` if _, err := tx.Exec(ctx, completeJob, job.ID, lease.Job.LeaseToken); err != nil { return "", fmt.Errorf("complete attachment job: %w", err) } const completeAttachment = ` UPDATE attachments SET status = 'ready', object_key = $3, ready_at = clock_timestamp(), staging_key = NULL, error_code = NULL, updated_at = clock_timestamp() WHERE id = $1 AND pipeline_version = $2` if _, err := tx.Exec(ctx, completeAttachment, attachment.ID, job.PipelineVersion, originalKey); err != nil { return "", fmt.Errorf("complete attachment: %w", err) } if err := tx.Commit(ctx); err != nil { return "", fmt.Errorf("commit completed attachment job: %w", err) } return model.JobMutationApplied, nil } func validCompletedVariants(variants []model.Variant, pipelineVersion int) bool { if len(variants) != 4 || pipelineVersion < 1 { return false } required := map[string]struct{}{ "original": {}, "thumbnail": {}, "crop": {}, "optimized": {}, } for _, variant := range variants { if variant.ObjectKey == "" || variant.PipelineVersion != pipelineVersion { return false } if _, exists := required[variant.Name]; !exists { return false } delete(required, variant.Name) } return len(required) == 0 } func (s *Store) RetryAttachmentJob( ctx context.Context, lease model.AttachmentJobLease, errorCode string, nextAttemptAt time.Time, traceparent string, tracestate string, ) (model.JobMutationResult, error) { if !validErrorCode(errorCode) || nextAttemptAt.IsZero() { return "", model.ErrInvalidInput } tx, err := s.pool.BeginTx(ctx, pgx.TxOptions{}) if err != nil { return "", fmt.Errorf("begin retry attachment job: %w", err) } defer func() { _ = tx.Rollback(context.WithoutCancel(ctx)) }() job, attachment, result, err := lockJobMutation(ctx, tx, lease) if err != nil || result != model.JobMutationApplied { return result, err } job.Attempt++ job.Status = model.AttachmentJobQueued job.AvailableAt = nextAttemptAt.UTC() job.ErrorCode = &errorCode const retryJob = ` UPDATE attachment_jobs SET status = 'queued', attempt = $3, available_at = $4, lease_token = NULL, lease_owner = NULL, lease_until = NULL, error_code = $5, updated_at = clock_timestamp() WHERE id = $1 AND lease_token = $2` if _, err := tx.Exec( ctx, retryJob, job.ID, lease.Job.LeaseToken, job.Attempt, job.AvailableAt, errorCode, ); err != nil { return "", fmt.Errorf("retry attachment job: %w", err) } const queueAttachment = ` UPDATE attachments SET status = 'queued', error_code = NULL, updated_at = clock_timestamp() WHERE id = $1 AND pipeline_version = $2` if _, err := tx.Exec(ctx, queueAttachment, attachment.ID, job.PipelineVersion); err != nil { return "", fmt.Errorf("requeue attachment: %w", err) } if err := insertAttachmentJobOutbox(ctx, tx, job, traceparent, tracestate); err != nil { return "", err } if err := tx.Commit(ctx); err != nil { return "", fmt.Errorf("commit attachment job retry: %w", err) } return model.JobMutationApplied, nil } func (s *Store) FailAttachmentJob( ctx context.Context, lease model.AttachmentJobLease, errorCode string, ) (model.JobMutationResult, error) { if !validErrorCode(errorCode) { return "", model.ErrInvalidInput } tx, err := s.pool.BeginTx(ctx, pgx.TxOptions{}) if err != nil { return "", fmt.Errorf("begin fail attachment job: %w", err) } defer func() { _ = tx.Rollback(context.WithoutCancel(ctx)) }() job, attachment, result, err := lockJobMutation(ctx, tx, lease) if err != nil || result != model.JobMutationApplied { return result, err } const failJob = ` UPDATE attachment_jobs SET status = 'failed', lease_token = NULL, lease_owner = NULL, lease_until = NULL, error_code = $3, updated_at = clock_timestamp() WHERE id = $1 AND lease_token = $2` if _, err := tx.Exec(ctx, failJob, job.ID, lease.Job.LeaseToken, errorCode); err != nil { return "", fmt.Errorf("fail attachment job: %w", err) } const failAttachment = ` UPDATE attachments SET status = 'failed', error_code = $3, ready_at = NULL, updated_at = clock_timestamp() WHERE id = $1 AND pipeline_version = $2` if _, err := tx.Exec(ctx, failAttachment, attachment.ID, job.PipelineVersion, errorCode); err != nil { return "", fmt.Errorf("fail attachment: %w", err) } if err := tx.Commit(ctx); err != nil { return "", fmt.Errorf("commit failed attachment job: %w", err) } return model.JobMutationApplied, nil } func lockJobMutation( ctx context.Context, tx pgx.Tx, lease model.AttachmentJobLease, ) (model.AttachmentJob, model.Attachment, model.JobMutationResult, error) { job, attachment, err := lockAttachmentJob(ctx, tx, lease.Job.ID) if err != nil { return model.AttachmentJob{}, model.Attachment{}, "", err } if attachment.PipelineVersion != lease.Job.PipelineVersion || job.PipelineVersion != lease.Job.PipelineVersion { return job, attachment, model.JobMutationStale, nil } if job.Status == model.AttachmentJobReady && attachment.Status == model.AttachmentReady { return job, attachment, model.JobMutationAlreadyComplete, nil } if job.PipelineVersion != lease.Job.PipelineVersion || job.Attempt != lease.Job.Attempt || job.Status != model.AttachmentJobProcessing || job.LeaseToken == "" || job.LeaseToken != lease.Job.LeaseToken || job.LeaseUntil == nil || !job.LeaseUntil.After(time.Now()) { return job, attachment, model.JobMutationStale, nil } return job, attachment, model.JobMutationApplied, nil } func validErrorCode(errorCode string) bool { return len(errorCode) >= 1 && len(errorCode) <= 64 }