feat: clean up staging objects after processing and during reconciliation

This commit is contained in:
kk10
2026-11-16 11:48:30 +03:00
parent 19152e7d18
commit b35faa5af8
7 changed files with 270 additions and 66 deletions
+53 -39
View File
@@ -67,6 +67,17 @@ func (q *Queries) ClaimAttachmentJob(ctx context.Context, arg ClaimAttachmentJob
return i, err
}
const clearAttachmentStagingKey = `-- name: ClearAttachmentStagingKey :exec
UPDATE attachments
SET staging_key = NULL
WHERE id = $1::uuid
`
func (q *Queries) ClearAttachmentStagingKey(ctx context.Context, id uuid.UUID) error {
_, err := q.db.Exec(ctx, clearAttachmentStagingKey, id)
return err
}
const completeAttachmentJob = `-- name: CompleteAttachmentJob :execrows
UPDATE attachments
SET status = 'ready',
@@ -320,15 +331,17 @@ func (q *Queries) ListAttachmentsByIDs(ctx context.Context, arg ListAttachmentsB
}
const listAttachmentsForReconcile = `-- name: ListAttachmentsForReconcile :many
SELECT id, s3_key, status, size_bytes
SELECT id, s3_key, staging_key, status, size_bytes, updated_at
FROM attachments
`
type ListAttachmentsForReconcileRow struct {
ID uuid.UUID
S3Key string
Status AttachmentStatus
SizeBytes int
ID uuid.UUID
S3Key string
StagingKey *string
Status AttachmentStatus
SizeBytes int
UpdatedAt time.Time
}
func (q *Queries) ListAttachmentsForReconcile(ctx context.Context) ([]ListAttachmentsForReconcileRow, error) {
@@ -343,8 +356,10 @@ func (q *Queries) ListAttachmentsForReconcile(ctx context.Context) ([]ListAttach
if err := rows.Scan(
&i.ID,
&i.S3Key,
&i.StagingKey,
&i.Status,
&i.SizeBytes,
&i.UpdatedAt,
); err != nil {
return nil, err
}
@@ -415,6 +430,39 @@ func (q *Queries) ListMessageAttachmentsByMessageIDs(ctx context.Context, messag
return items, nil
}
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
WHERE staging_key = $1::text
FOR UPDATE
`
func (q *Queries) LockAttachmentByStagingKey(ctx context.Context, stagingKey string) (Attachment, error) {
row := q.db.QueryRow(ctx, lockAttachmentByStagingKey, stagingKey)
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.Width,
&i.Height,
&i.LeaseUntil,
)
return i, err
}
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
WHERE id = $1::uuid
@@ -535,40 +583,6 @@ func (q *Queries) MarkAttachmentFailedIfReady(ctx context.Context, arg MarkAttac
return result.RowsAffected(), nil
}
const markAttachmentReady = `-- name: MarkAttachmentReady :one
UPDATE attachments
SET status = 'ready', updated_at = now()
WHERE id = $1::uuid
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
`
func (q *Queries) MarkAttachmentReady(ctx context.Context, id uuid.UUID) (Attachment, error) {
row := q.db.QueryRow(ctx, markAttachmentReady, 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.Width,
&i.Height,
&i.LeaseUntil,
)
return i, err
}
const recoverExpiredAttachmentJob = `-- name: RecoverExpiredAttachmentJob :execrows
UPDATE attachments
SET status = 'queued',