feat(attachments): add pipeline and outbox schema and queries

This commit is contained in:
kk10
2026-11-14 14:39:56 +03:00
parent 36d9b0c8f6
commit f2fc4b3e1f
9 changed files with 846 additions and 37 deletions
+323 -9
View File
@@ -12,16 +12,109 @@ import (
"github.com/google/uuid"
)
const claimAttachmentJob = `-- name: ClaimAttachmentJob :one
UPDATE attachments
SET status = 'processing',
attempts = attempts + 1,
lease_until = $1::timestamptz,
updated_at = now()
WHERE id = $2::uuid
AND job_id = $3::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
`
type ClaimAttachmentJobParams struct {
LeaseUntil time.Time
ID uuid.UUID
JobID uuid.UUID
PipelineVersion int
Attempt int
}
func (q *Queries) ClaimAttachmentJob(ctx context.Context, arg ClaimAttachmentJobParams) (Attachment, error) {
row := q.db.QueryRow(ctx, claimAttachmentJob,
arg.LeaseUntil,
arg.ID,
arg.JobID,
arg.PipelineVersion,
arg.Attempt,
)
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 completeAttachmentJob = `-- name: CompleteAttachmentJob :execrows
UPDATE attachments
SET status = 'ready',
error_code = NULL,
lease_until = NULL,
width = $1::int,
height = $2::int,
updated_at = now()
WHERE id = $3::uuid
AND status = 'processing'
AND attempts = $4::int
AND lease_until > now()
`
type CompleteAttachmentJobParams struct {
Width int
Height int
ID uuid.UUID
Attempt int
}
func (q *Queries) CompleteAttachmentJob(ctx context.Context, arg CompleteAttachmentJobParams) (int64, error) {
result, err := q.db.Exec(ctx, completeAttachmentJob,
arg.Width,
arg.Height,
arg.ID,
arg.Attempt,
)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}
const createAttachment = `-- name: CreateAttachment :one
INSERT INTO attachments (
id,
chat_id,
owner_id,
s3_key,
staging_key,
job_id,
filename,
content_type,
size_bytes,
sha256,
width,
height,
error_code
)
SELECT
@@ -30,16 +123,20 @@ SELECT
$3::uuid,
$4::text,
$5::text,
$6::attachment_content_type,
$7::int,
$8::text,
NULLIF($9::text, '')
$6::uuid,
$7::text,
$8::attachment_content_type,
$9::int,
$10::text,
$11::int,
$12::int,
NULLIF($13::text, '')
WHERE EXISTS (
SELECT 1 FROM chat_members
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
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
`
type CreateAttachmentParams struct {
@@ -47,10 +144,14 @@ type CreateAttachmentParams struct {
ChatID uuid.UUID
OwnerID uuid.UUID
S3Key string
StagingKey *string
JobID *uuid.UUID
Filename string
ContentType AttachmentContentType
SizeBytes int
Sha256 string
Width int
Height int
ErrorCode string
}
@@ -60,10 +161,14 @@ func (q *Queries) CreateAttachment(ctx context.Context, arg CreateAttachmentPara
arg.ChatID,
arg.OwnerID,
arg.S3Key,
arg.StagingKey,
arg.JobID,
arg.Filename,
arg.ContentType,
arg.SizeBytes,
arg.Sha256,
arg.Width,
arg.Height,
arg.ErrorCode,
)
var i Attachment
@@ -80,6 +185,13 @@ func (q *Queries) CreateAttachment(ctx context.Context, arg CreateAttachmentPara
&i.ErrorCode,
&i.CreatedAt,
&i.UpdatedAt,
&i.StagingKey,
&i.JobID,
&i.PipelineVersion,
&i.Attempts,
&i.Width,
&i.Height,
&i.LeaseUntil,
)
return i, err
}
@@ -120,8 +232,53 @@ func (q *Queries) DeleteQueuedAttachment(ctx context.Context, id uuid.UUID) (int
return result.RowsAffected(), nil
}
const failAttachmentJob = `-- name: FailAttachmentJob :execrows
UPDATE attachments
SET status = 'failed',
error_code = $1::text,
lease_until = NULL,
updated_at = now()
WHERE id = $2::uuid
AND status = 'processing'
AND attempts = $3::int
AND lease_until > now()
`
type FailAttachmentJobParams struct {
ErrorCode string
ID uuid.UUID
Attempt int
}
func (q *Queries) FailAttachmentJob(ctx context.Context, arg FailAttachmentJobParams) (int64, error) {
result, err := q.db.Exec(ctx, failAttachmentJob, arg.ErrorCode, arg.ID, arg.Attempt)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}
const failExpiredAttachmentJob = `-- name: FailExpiredAttachmentJob :execrows
UPDATE attachments
SET status = 'failed',
error_code = 'retry_exhausted',
lease_until = NULL,
updated_at = now()
WHERE id = $1::uuid
AND status = 'processing'
AND lease_until <= now()
`
func (q *Queries) FailExpiredAttachmentJob(ctx context.Context, id uuid.UUID) (int64, error) {
result, err := q.db.Exec(ctx, failExpiredAttachmentJob, id)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}
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 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, width, height, lease_until FROM attachments
WHERE id = $1::uuid
`
@@ -141,6 +298,13 @@ func (q *Queries) GetAttachmentByID(ctx context.Context, dollar_1 uuid.UUID) (At
&i.ErrorCode,
&i.CreatedAt,
&i.UpdatedAt,
&i.StagingKey,
&i.JobID,
&i.PipelineVersion,
&i.Attempts,
&i.Width,
&i.Height,
&i.LeaseUntil,
)
return i, err
}
@@ -316,6 +480,105 @@ func (q *Queries) ListStaleAttachmentUploads(ctx context.Context, cutoff time.Ti
return items, nil
}
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
AND status = 'processing'
AND job_id = $2::uuid
AND pipeline_version = $3::int
AND attempts = $4::int
AND lease_until > now()
FOR UPDATE
`
type LockAttachmentJobParams struct {
ID uuid.UUID
JobID uuid.UUID
PipelineVersion int
Attempt int
}
func (q *Queries) LockAttachmentJob(ctx context.Context, arg LockAttachmentJobParams) (Attachment, error) {
row := q.db.QueryRow(ctx, lockAttachmentJob,
arg.ID,
arg.JobID,
arg.PipelineVersion,
arg.Attempt,
)
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 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
WHERE status = 'processing'
AND lease_until <= now()
ORDER BY lease_until, id
LIMIT $1::int
FOR UPDATE SKIP LOCKED
`
func (q *Queries) LockExpiredAttachmentJobs(ctx context.Context, batchSize int) ([]Attachment, error) {
rows, err := q.db.Query(ctx, lockExpiredAttachmentJobs, batchSize)
if err != nil {
return nil, err
}
defer rows.Close()
items := []Attachment{}
for rows.Next() {
var i Attachment
if err := rows.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,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const markAttachmentFailedIfReady = `-- name: MarkAttachmentFailedIfReady :execrows
UPDATE attachments
SET status = 'failed',
@@ -341,11 +604,11 @@ 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
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, dollar_1 uuid.UUID) (Attachment, error) {
row := q.db.QueryRow(ctx, markAttachmentReady, dollar_1)
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,
@@ -360,6 +623,57 @@ func (q *Queries) MarkAttachmentReady(ctx context.Context, dollar_1 uuid.UUID) (
&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',
lease_until = NULL,
updated_at = now()
WHERE id = $1::uuid
AND status = 'processing'
AND lease_until <= now()
`
func (q *Queries) RecoverExpiredAttachmentJob(ctx context.Context, id uuid.UUID) (int64, error) {
result, err := q.db.Exec(ctx, recoverExpiredAttachmentJob, id)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}
const retryAttachmentJob = `-- name: RetryAttachmentJob :execrows
UPDATE attachments
SET status = 'queued',
error_code = $1::text,
lease_until = NULL,
updated_at = now()
WHERE id = $2::uuid
AND status = 'processing'
AND attempts = $3::int
AND lease_until > now()
`
type RetryAttachmentJobParams struct {
ErrorCode string
ID uuid.UUID
Attempt int
}
func (q *Queries) RetryAttachmentJob(ctx context.Context, arg RetryAttachmentJobParams) (int64, error) {
result, err := q.db.Exec(ctx, retryAttachmentJob, arg.ErrorCode, arg.ID, arg.Attempt)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}