forked from mtusi2026/mtusi2026lab6
Prepare verified laboratory 6 implementation and evidence
This commit is contained in:
@@ -0,0 +1,403 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.31.1
|
||||
// source: pipeline.sql
|
||||
|
||||
package queries
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
const acquireJob = `-- name: AcquireJob :one
|
||||
UPDATE attachment_jobs j SET attempt=j.attempt+1,lease_id=$1,lease_until=clock_timestamp()+$2::int*interval '1 second'
|
||||
WHERE j.id=$3 AND j.pipeline_version=$4 AND NOT j.completed
|
||||
AND j.next_attempt<=clock_timestamp() AND j.lease_until<clock_timestamp()
|
||||
AND EXISTS(SELECT 1 FROM attachments a WHERE a.id=j.attachment_id AND a.pipeline_version=j.pipeline_version AND a.status IN ('queued','processing'))
|
||||
RETURNING j.id, j.attachment_id, j.pipeline_version, j.attempt, j.traceparent, j.tracestate, j.lease_id, j.lease_until, j.next_attempt, j.published_at, j.completed, j.created_at, j.replay_count, j.replayed_at
|
||||
`
|
||||
|
||||
type AcquireJobParams struct {
|
||||
LeaseID uuid.UUID
|
||||
LeaseSeconds int32
|
||||
JobID uuid.UUID
|
||||
PipelineVersion int32
|
||||
}
|
||||
|
||||
func (q *Queries) AcquireJob(ctx context.Context, arg AcquireJobParams) (AttachmentJob, error) {
|
||||
row := q.db.QueryRow(ctx, acquireJob,
|
||||
arg.LeaseID,
|
||||
arg.LeaseSeconds,
|
||||
arg.JobID,
|
||||
arg.PipelineVersion,
|
||||
)
|
||||
var i AttachmentJob
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.AttachmentID,
|
||||
&i.PipelineVersion,
|
||||
&i.Attempt,
|
||||
&i.Traceparent,
|
||||
&i.Tracestate,
|
||||
&i.LeaseID,
|
||||
&i.LeaseUntil,
|
||||
&i.NextAttempt,
|
||||
&i.PublishedAt,
|
||||
&i.Completed,
|
||||
&i.CreatedAt,
|
||||
&i.ReplayCount,
|
||||
&i.ReplayedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const advancePipeline = `-- name: AdvancePipeline :one
|
||||
UPDATE attachments SET status='queued',error_code='',variants='[]',pipeline_version=pipeline_version+1,staging_expired=false,updated_at=clock_timestamp() WHERE id=$1 AND (status='ready' OR (status='failed' AND NOT staging_expired)) RETURNING id, chat_id, owner_id, filename, content_type, size_bytes, sha256, status, pipeline_version, error_code, variants, staging_expired, staging_cleaned, created_at, updated_at, accepted_at, first_ready_at
|
||||
`
|
||||
|
||||
func (q *Queries) AdvancePipeline(ctx context.Context, id uuid.UUID) (Attachment, error) {
|
||||
row := q.db.QueryRow(ctx, advancePipeline, id)
|
||||
var i Attachment
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.ChatID,
|
||||
&i.OwnerID,
|
||||
&i.Filename,
|
||||
&i.ContentType,
|
||||
&i.SizeBytes,
|
||||
&i.Sha256,
|
||||
&i.Status,
|
||||
&i.PipelineVersion,
|
||||
&i.ErrorCode,
|
||||
&i.Variants,
|
||||
&i.StagingExpired,
|
||||
&i.StagingCleaned,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.AcceptedAt,
|
||||
&i.FirstReadyAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const completeAttachment = `-- name: CompleteAttachment :execrows
|
||||
UPDATE attachments a SET first_ready_at=COALESCE(a.first_ready_at,clock_timestamp()),status='ready',variants=$1::jsonb,error_code='',updated_at=clock_timestamp()
|
||||
FROM attachment_jobs j WHERE j.id=$2 AND j.attachment_id=a.id AND j.pipeline_version=a.pipeline_version
|
||||
AND j.lease_id=$3 AND j.lease_until>clock_timestamp() AND NOT j.completed AND a.status='processing'
|
||||
`
|
||||
|
||||
type CompleteAttachmentParams struct {
|
||||
Variants []byte
|
||||
JobID uuid.UUID
|
||||
LeaseID uuid.UUID
|
||||
}
|
||||
|
||||
func (q *Queries) CompleteAttachment(ctx context.Context, arg CompleteAttachmentParams) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, completeAttachment, arg.Variants, arg.JobID, arg.LeaseID)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
||||
const completeJob = `-- name: CompleteJob :exec
|
||||
UPDATE attachment_jobs SET completed=true,lease_until='epoch' WHERE id=$1 AND lease_id=$2
|
||||
`
|
||||
|
||||
type CompleteJobParams struct {
|
||||
JobID uuid.UUID
|
||||
LeaseID uuid.UUID
|
||||
}
|
||||
|
||||
func (q *Queries) CompleteJob(ctx context.Context, arg CompleteJobParams) error {
|
||||
_, err := q.db.Exec(ctx, completeJob, arg.JobID, arg.LeaseID)
|
||||
return err
|
||||
}
|
||||
|
||||
const insertJob = `-- name: InsertJob :exec
|
||||
INSERT INTO attachment_jobs(id,attachment_id,pipeline_version,traceparent,tracestate)
|
||||
VALUES($1,$2,$3,$4,$5)
|
||||
`
|
||||
|
||||
type InsertJobParams struct {
|
||||
JobID uuid.UUID
|
||||
AttachmentID uuid.UUID
|
||||
PipelineVersion int32
|
||||
Traceparent string
|
||||
Tracestate string
|
||||
}
|
||||
|
||||
func (q *Queries) InsertJob(ctx context.Context, arg InsertJobParams) error {
|
||||
_, err := q.db.Exec(ctx, insertJob,
|
||||
arg.JobID,
|
||||
arg.AttachmentID,
|
||||
arg.PipelineVersion,
|
||||
arg.Traceparent,
|
||||
arg.Tracestate,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
const pendingJobs = `-- name: PendingJobs :many
|
||||
SELECT j.id, j.attachment_id, j.pipeline_version, j.attempt, j.traceparent, j.tracestate, j.lease_id, j.lease_until, j.next_attempt, j.published_at, j.completed, j.created_at, j.replay_count, j.replayed_at FROM attachment_jobs j JOIN attachments a ON a.id=j.attachment_id AND a.pipeline_version=j.pipeline_version
|
||||
WHERE NOT j.completed AND a.status IN ('queued','processing') AND j.next_attempt<=clock_timestamp()
|
||||
AND j.lease_until<clock_timestamp() AND j.published_at<clock_timestamp()-interval '2 seconds'
|
||||
ORDER BY j.created_at,j.id LIMIT 100
|
||||
`
|
||||
|
||||
func (q *Queries) PendingJobs(ctx context.Context) ([]AttachmentJob, error) {
|
||||
rows, err := q.db.Query(ctx, pendingJobs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
items := []AttachmentJob{}
|
||||
for rows.Next() {
|
||||
var i AttachmentJob
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.AttachmentID,
|
||||
&i.PipelineVersion,
|
||||
&i.Attempt,
|
||||
&i.Traceparent,
|
||||
&i.Tracestate,
|
||||
&i.LeaseID,
|
||||
&i.LeaseUntil,
|
||||
&i.NextAttempt,
|
||||
&i.PublishedAt,
|
||||
&i.Completed,
|
||||
&i.CreatedAt,
|
||||
&i.ReplayCount,
|
||||
&i.ReplayedAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const processingAttachment = `-- name: ProcessingAttachment :one
|
||||
UPDATE attachments SET status='processing',updated_at=clock_timestamp()
|
||||
WHERE id=$1 AND pipeline_version=$2 AND status IN ('queued','processing') RETURNING id, chat_id, owner_id, filename, content_type, size_bytes, sha256, status, pipeline_version, error_code, variants, staging_expired, staging_cleaned, created_at, updated_at, accepted_at, first_ready_at
|
||||
`
|
||||
|
||||
type ProcessingAttachmentParams struct {
|
||||
AttachmentID uuid.UUID
|
||||
PipelineVersion int32
|
||||
}
|
||||
|
||||
func (q *Queries) ProcessingAttachment(ctx context.Context, arg ProcessingAttachmentParams) (Attachment, error) {
|
||||
row := q.db.QueryRow(ctx, processingAttachment, arg.AttachmentID, arg.PipelineVersion)
|
||||
var i Attachment
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.ChatID,
|
||||
&i.OwnerID,
|
||||
&i.Filename,
|
||||
&i.ContentType,
|
||||
&i.SizeBytes,
|
||||
&i.Sha256,
|
||||
&i.Status,
|
||||
&i.PipelineVersion,
|
||||
&i.ErrorCode,
|
||||
&i.Variants,
|
||||
&i.StagingExpired,
|
||||
&i.StagingCleaned,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.AcceptedAt,
|
||||
&i.FirstReadyAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const published = `-- name: Published :exec
|
||||
UPDATE attachment_jobs SET published_at=clock_timestamp() WHERE id=$1
|
||||
`
|
||||
|
||||
func (q *Queries) Published(ctx context.Context, id uuid.UUID) error {
|
||||
_, err := q.db.Exec(ctx, published, id)
|
||||
return err
|
||||
}
|
||||
|
||||
const queueAttachment = `-- name: QueueAttachment :one
|
||||
UPDATE attachments SET accepted_at=COALESCE(accepted_at,clock_timestamp()),status='queued',updated_at=clock_timestamp() WHERE id=$1 AND status='uploading' AND NOT staging_expired RETURNING id, chat_id, owner_id, filename, content_type, size_bytes, sha256, status, pipeline_version, error_code, variants, staging_expired, staging_cleaned, created_at, updated_at, accepted_at, first_ready_at
|
||||
`
|
||||
|
||||
func (q *Queries) QueueAttachment(ctx context.Context, id uuid.UUID) (Attachment, error) {
|
||||
row := q.db.QueryRow(ctx, queueAttachment, id)
|
||||
var i Attachment
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.ChatID,
|
||||
&i.OwnerID,
|
||||
&i.Filename,
|
||||
&i.ContentType,
|
||||
&i.SizeBytes,
|
||||
&i.Sha256,
|
||||
&i.Status,
|
||||
&i.PipelineVersion,
|
||||
&i.ErrorCode,
|
||||
&i.Variants,
|
||||
&i.StagingExpired,
|
||||
&i.StagingCleaned,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.AcceptedAt,
|
||||
&i.FirstReadyAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const replayAttachment = `-- name: ReplayAttachment :one
|
||||
UPDATE attachments SET status='queued',error_code='',updated_at=clock_timestamp() WHERE id=$1 AND status='failed' AND NOT staging_expired RETURNING id, chat_id, owner_id, filename, content_type, size_bytes, sha256, status, pipeline_version, error_code, variants, staging_expired, staging_cleaned, created_at, updated_at, accepted_at, first_ready_at
|
||||
`
|
||||
|
||||
func (q *Queries) ReplayAttachment(ctx context.Context, id uuid.UUID) (Attachment, error) {
|
||||
row := q.db.QueryRow(ctx, replayAttachment, id)
|
||||
var i Attachment
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.ChatID,
|
||||
&i.OwnerID,
|
||||
&i.Filename,
|
||||
&i.ContentType,
|
||||
&i.SizeBytes,
|
||||
&i.Sha256,
|
||||
&i.Status,
|
||||
&i.PipelineVersion,
|
||||
&i.ErrorCode,
|
||||
&i.Variants,
|
||||
&i.StagingExpired,
|
||||
&i.StagingCleaned,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.AcceptedAt,
|
||||
&i.FirstReadyAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const replayJobs = `-- name: ReplayJobs :exec
|
||||
UPDATE attachment_jobs SET replay_count=replay_count+1,replayed_at=clock_timestamp(),completed=false,attempt=0,lease_until='epoch',next_attempt='epoch',published_at='epoch' WHERE attachment_id=$1 AND pipeline_version=$2
|
||||
`
|
||||
|
||||
type ReplayJobsParams struct {
|
||||
AttachmentID uuid.UUID
|
||||
PipelineVersion int32
|
||||
}
|
||||
|
||||
func (q *Queries) ReplayJobs(ctx context.Context, arg ReplayJobsParams) error {
|
||||
_, err := q.db.Exec(ctx, replayJobs, arg.AttachmentID, arg.PipelineVersion)
|
||||
return err
|
||||
}
|
||||
|
||||
const retryAttachment = `-- name: RetryAttachment :execrows
|
||||
UPDATE attachments a SET status=$1,error_code=$2,updated_at=clock_timestamp()
|
||||
FROM attachment_jobs j WHERE j.id=$3 AND j.attachment_id=a.id AND j.pipeline_version=a.pipeline_version
|
||||
AND j.lease_id=$4 AND j.lease_until>clock_timestamp() AND NOT j.completed AND a.status='processing'
|
||||
`
|
||||
|
||||
type RetryAttachmentParams struct {
|
||||
NextStatus string
|
||||
ErrorCode string
|
||||
JobID uuid.UUID
|
||||
LeaseID uuid.UUID
|
||||
}
|
||||
|
||||
func (q *Queries) RetryAttachment(ctx context.Context, arg RetryAttachmentParams) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, retryAttachment,
|
||||
arg.NextStatus,
|
||||
arg.ErrorCode,
|
||||
arg.JobID,
|
||||
arg.LeaseID,
|
||||
)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
||||
const retryJob = `-- name: RetryJob :exec
|
||||
UPDATE attachment_jobs SET lease_until='epoch',next_attempt=clock_timestamp()+$1::int*interval '1 millisecond',completed=$2
|
||||
WHERE id=$3 AND lease_id=$4
|
||||
`
|
||||
|
||||
type RetryJobParams struct {
|
||||
DelayMs int32
|
||||
Completed bool
|
||||
JobID uuid.UUID
|
||||
LeaseID uuid.UUID
|
||||
}
|
||||
|
||||
func (q *Queries) RetryJob(ctx context.Context, arg RetryJobParams) error {
|
||||
_, err := q.db.Exec(ctx, retryJob,
|
||||
arg.DelayMs,
|
||||
arg.Completed,
|
||||
arg.JobID,
|
||||
arg.LeaseID,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
const stagingCleaned = `-- name: StagingCleaned :exec
|
||||
UPDATE attachments SET staging_cleaned=true WHERE id=$1 AND (staging_expired OR status='ready')
|
||||
`
|
||||
|
||||
func (q *Queries) StagingCleaned(ctx context.Context, id uuid.UUID) error {
|
||||
_, err := q.db.Exec(ctx, stagingCleaned, id)
|
||||
return err
|
||||
}
|
||||
|
||||
const stagingCleanup = `-- name: StagingCleanup :many
|
||||
WITH candidates AS (
|
||||
SELECT id FROM attachments WHERE NOT staging_cleaned AND (status='ready' OR (status IN ('failed','uploading') AND updated_at<clock_timestamp()-interval '24 hours'))
|
||||
ORDER BY updated_at,id LIMIT 20
|
||||
)
|
||||
UPDATE attachments a SET staging_expired=(a.status<>'ready') FROM candidates c WHERE a.id=c.id AND NOT a.staging_cleaned
|
||||
AND (a.status='ready' OR (a.status IN ('failed','uploading') AND a.updated_at<clock_timestamp()-interval '24 hours')) RETURNING a.id, a.chat_id, a.owner_id, a.filename, a.content_type, a.size_bytes, a.sha256, a.status, a.pipeline_version, a.error_code, a.variants, a.staging_expired, a.staging_cleaned, a.created_at, a.updated_at, a.accepted_at, a.first_ready_at
|
||||
`
|
||||
|
||||
func (q *Queries) StagingCleanup(ctx context.Context) ([]Attachment, error) {
|
||||
rows, err := q.db.Query(ctx, stagingCleanup)
|
||||
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.Filename,
|
||||
&i.ContentType,
|
||||
&i.SizeBytes,
|
||||
&i.Sha256,
|
||||
&i.Status,
|
||||
&i.PipelineVersion,
|
||||
&i.ErrorCode,
|
||||
&i.Variants,
|
||||
&i.StagingExpired,
|
||||
&i.StagingCleaned,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.AcceptedAt,
|
||||
&i.FirstReadyAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
Reference in New Issue
Block a user