forked from mtusi2026/mtusi2026lab5
feat(worker): prepare replay queries and audit schema
This commit is contained in:
@@ -23,7 +23,7 @@ WHERE id = $2::uuid
|
||||
AND pipeline_version = $4::int
|
||||
AND status = 'queued'
|
||||
AND attempts + 1 = $5::int
|
||||
RETURNING id, chat_id, owner_id, s3_key, filename, content_type, size_bytes, sha256, status, error_code, created_at, updated_at, staging_key, job_id, pipeline_version, attempts, width, height, lease_until
|
||||
RETURNING id, chat_id, owner_id, s3_key, filename, content_type, size_bytes, sha256, status, error_code, created_at, updated_at, staging_key, job_id, pipeline_version, attempts, retry_base, width, height, lease_until
|
||||
`
|
||||
|
||||
type ClaimAttachmentJobParams struct {
|
||||
@@ -60,6 +60,7 @@ func (q *Queries) ClaimAttachmentJob(ctx context.Context, arg ClaimAttachmentJob
|
||||
&i.JobID,
|
||||
&i.PipelineVersion,
|
||||
&i.Attempts,
|
||||
&i.RetryBase,
|
||||
&i.Width,
|
||||
&i.Height,
|
||||
&i.LeaseUntil,
|
||||
@@ -138,7 +139,7 @@ WHERE EXISTS (
|
||||
WHERE chat_id = $2
|
||||
AND user_id = $3
|
||||
)
|
||||
RETURNING id, chat_id, owner_id, s3_key, filename, content_type, size_bytes, sha256, status, error_code, created_at, updated_at, staging_key, job_id, pipeline_version, attempts, width, height, lease_until
|
||||
RETURNING id, chat_id, owner_id, s3_key, filename, content_type, size_bytes, sha256, status, error_code, created_at, updated_at, staging_key, job_id, pipeline_version, attempts, retry_base, width, height, lease_until
|
||||
`
|
||||
|
||||
type CreateAttachmentParams struct {
|
||||
@@ -191,6 +192,7 @@ func (q *Queries) CreateAttachment(ctx context.Context, arg CreateAttachmentPara
|
||||
&i.JobID,
|
||||
&i.PipelineVersion,
|
||||
&i.Attempts,
|
||||
&i.RetryBase,
|
||||
&i.Width,
|
||||
&i.Height,
|
||||
&i.LeaseUntil,
|
||||
@@ -267,7 +269,7 @@ func (q *Queries) FailExpiredAttachmentJob(ctx context.Context, id uuid.UUID) (i
|
||||
}
|
||||
|
||||
const getAttachmentByID = `-- name: GetAttachmentByID :one
|
||||
SELECT id, chat_id, owner_id, s3_key, filename, content_type, size_bytes, sha256, status, error_code, created_at, updated_at, staging_key, job_id, pipeline_version, attempts, width, height, lease_until FROM attachments
|
||||
SELECT id, chat_id, owner_id, s3_key, filename, content_type, size_bytes, sha256, status, error_code, created_at, updated_at, staging_key, job_id, pipeline_version, attempts, retry_base, width, height, lease_until FROM attachments
|
||||
WHERE id = $1::uuid
|
||||
`
|
||||
|
||||
@@ -291,6 +293,7 @@ func (q *Queries) GetAttachmentByID(ctx context.Context, dollar_1 uuid.UUID) (At
|
||||
&i.JobID,
|
||||
&i.PipelineVersion,
|
||||
&i.Attempts,
|
||||
&i.RetryBase,
|
||||
&i.Width,
|
||||
&i.Height,
|
||||
&i.LeaseUntil,
|
||||
@@ -371,6 +374,48 @@ func (q *Queries) ListAttachmentsForReconcile(ctx context.Context) ([]ListAttach
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const listFailedAttachments = `-- name: ListFailedAttachments :many
|
||||
SELECT id, job_id, error_code, attempts, updated_at
|
||||
FROM attachments
|
||||
WHERE status = 'failed'
|
||||
ORDER BY updated_at, id
|
||||
LIMIT $1::int
|
||||
`
|
||||
|
||||
type ListFailedAttachmentsRow struct {
|
||||
ID uuid.UUID
|
||||
JobID *uuid.UUID
|
||||
ErrorCode *string
|
||||
Attempts int
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
func (q *Queries) ListFailedAttachments(ctx context.Context, batchSize int) ([]ListFailedAttachmentsRow, error) {
|
||||
rows, err := q.db.Query(ctx, listFailedAttachments, batchSize)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
items := []ListFailedAttachmentsRow{}
|
||||
for rows.Next() {
|
||||
var i ListFailedAttachmentsRow
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.JobID,
|
||||
&i.ErrorCode,
|
||||
&i.Attempts,
|
||||
&i.UpdatedAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const listMessageAttachmentIDs = `-- name: ListMessageAttachmentIDs :many
|
||||
SELECT attachment_id
|
||||
FROM message_attachments
|
||||
@@ -431,7 +476,7 @@ func (q *Queries) ListMessageAttachmentsByMessageIDs(ctx context.Context, messag
|
||||
}
|
||||
|
||||
const lockAttachmentByStagingKey = `-- name: LockAttachmentByStagingKey :one
|
||||
SELECT id, chat_id, owner_id, s3_key, filename, content_type, size_bytes, sha256, status, error_code, created_at, updated_at, staging_key, job_id, pipeline_version, attempts, width, height, lease_until FROM attachments
|
||||
SELECT id, chat_id, owner_id, s3_key, filename, content_type, size_bytes, sha256, status, error_code, created_at, updated_at, staging_key, job_id, pipeline_version, attempts, retry_base, width, height, lease_until FROM attachments
|
||||
WHERE staging_key = $1::text
|
||||
FOR UPDATE
|
||||
`
|
||||
@@ -456,6 +501,7 @@ func (q *Queries) LockAttachmentByStagingKey(ctx context.Context, stagingKey str
|
||||
&i.JobID,
|
||||
&i.PipelineVersion,
|
||||
&i.Attempts,
|
||||
&i.RetryBase,
|
||||
&i.Width,
|
||||
&i.Height,
|
||||
&i.LeaseUntil,
|
||||
@@ -464,7 +510,7 @@ func (q *Queries) LockAttachmentByStagingKey(ctx context.Context, stagingKey str
|
||||
}
|
||||
|
||||
const lockAttachmentJob = `-- name: LockAttachmentJob :one
|
||||
SELECT id, chat_id, owner_id, s3_key, filename, content_type, size_bytes, sha256, status, error_code, created_at, updated_at, staging_key, job_id, pipeline_version, attempts, width, height, lease_until FROM attachments
|
||||
SELECT id, chat_id, owner_id, s3_key, filename, content_type, size_bytes, sha256, status, error_code, created_at, updated_at, staging_key, job_id, pipeline_version, attempts, retry_base, width, height, lease_until FROM attachments
|
||||
WHERE id = $1::uuid
|
||||
AND status = 'processing'
|
||||
AND job_id = $2::uuid
|
||||
@@ -506,6 +552,7 @@ func (q *Queries) LockAttachmentJob(ctx context.Context, arg LockAttachmentJobPa
|
||||
&i.JobID,
|
||||
&i.PipelineVersion,
|
||||
&i.Attempts,
|
||||
&i.RetryBase,
|
||||
&i.Width,
|
||||
&i.Height,
|
||||
&i.LeaseUntil,
|
||||
@@ -514,7 +561,7 @@ func (q *Queries) LockAttachmentJob(ctx context.Context, arg LockAttachmentJobPa
|
||||
}
|
||||
|
||||
const lockExpiredAttachmentJobs = `-- name: LockExpiredAttachmentJobs :many
|
||||
SELECT id, chat_id, owner_id, s3_key, filename, content_type, size_bytes, sha256, status, error_code, created_at, updated_at, staging_key, job_id, pipeline_version, attempts, width, height, lease_until FROM attachments
|
||||
SELECT id, chat_id, owner_id, s3_key, filename, content_type, size_bytes, sha256, status, error_code, created_at, updated_at, staging_key, job_id, pipeline_version, attempts, retry_base, width, height, lease_until FROM attachments
|
||||
WHERE status = 'processing'
|
||||
AND lease_until <= now()
|
||||
ORDER BY lease_until, id
|
||||
@@ -548,6 +595,7 @@ func (q *Queries) LockExpiredAttachmentJobs(ctx context.Context, batchSize int)
|
||||
&i.JobID,
|
||||
&i.PipelineVersion,
|
||||
&i.Attempts,
|
||||
&i.RetryBase,
|
||||
&i.Width,
|
||||
&i.Height,
|
||||
&i.LeaseUntil,
|
||||
@@ -562,6 +610,46 @@ func (q *Queries) LockExpiredAttachmentJobs(ctx context.Context, batchSize int)
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const lockFailedAttachmentForReplay = `-- name: LockFailedAttachmentForReplay :one
|
||||
SELECT id, chat_id, owner_id, s3_key, filename, content_type, size_bytes, sha256, status, error_code, created_at, updated_at, staging_key, job_id, pipeline_version, attempts, retry_base, width, height, lease_until FROM attachments
|
||||
WHERE id = $1::uuid
|
||||
AND status = 'failed'
|
||||
AND staging_key IS NOT NULL
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM attachment_variants
|
||||
WHERE attachment_id = attachments.id
|
||||
)
|
||||
FOR UPDATE
|
||||
`
|
||||
|
||||
func (q *Queries) LockFailedAttachmentForReplay(ctx context.Context, id uuid.UUID) (Attachment, error) {
|
||||
row := q.db.QueryRow(ctx, lockFailedAttachmentForReplay, id)
|
||||
var i Attachment
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.ChatID,
|
||||
&i.OwnerID,
|
||||
&i.S3Key,
|
||||
&i.Filename,
|
||||
&i.ContentType,
|
||||
&i.SizeBytes,
|
||||
&i.Sha256,
|
||||
&i.Status,
|
||||
&i.ErrorCode,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.StagingKey,
|
||||
&i.JobID,
|
||||
&i.PipelineVersion,
|
||||
&i.Attempts,
|
||||
&i.RetryBase,
|
||||
&i.Width,
|
||||
&i.Height,
|
||||
&i.LeaseUntil,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const markAttachmentFailedIfReady = `-- name: MarkAttachmentFailedIfReady :execrows
|
||||
UPDATE attachments
|
||||
SET status = 'failed',
|
||||
@@ -601,6 +689,30 @@ func (q *Queries) RecoverExpiredAttachmentJob(ctx context.Context, id uuid.UUID)
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
||||
const replayAttachmentJob = `-- name: ReplayAttachmentJob :execrows
|
||||
UPDATE attachments
|
||||
SET status = 'queued',
|
||||
retry_base = attempts,
|
||||
error_code = NULL,
|
||||
lease_until = NULL,
|
||||
updated_at = now()
|
||||
WHERE id = $1::uuid
|
||||
AND status = 'failed'
|
||||
AND staging_key IS NOT NULL
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM attachment_variants
|
||||
WHERE attachment_id = attachments.id
|
||||
)
|
||||
`
|
||||
|
||||
func (q *Queries) ReplayAttachmentJob(ctx context.Context, id uuid.UUID) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, replayAttachmentJob, id)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
||||
const retryAttachmentJob = `-- name: RetryAttachmentJob :execrows
|
||||
UPDATE attachments
|
||||
SET status = 'queued',
|
||||
|
||||
Reference in New Issue
Block a user