From 16ff6ecb5ee2138a49838ebbb2660b4a5005bda8 Mon Sep 17 00:00:00 2001 From: Grendgi Date: Mon, 8 Jun 2026 17:48:02 +0300 Subject: [PATCH] Prioritize newest AI jobs --- internal/migrate/sql/003_ai_jobs_newest_first.up.sql | 5 +++++ internal/store/store.go | 2 +- migrations/003_ai_jobs_newest_first.down.sql | 5 +++++ migrations/003_ai_jobs_newest_first.up.sql | 5 +++++ 4 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 internal/migrate/sql/003_ai_jobs_newest_first.up.sql create mode 100644 migrations/003_ai_jobs_newest_first.down.sql create mode 100644 migrations/003_ai_jobs_newest_first.up.sql diff --git a/internal/migrate/sql/003_ai_jobs_newest_first.up.sql b/internal/migrate/sql/003_ai_jobs_newest_first.up.sql new file mode 100644 index 0000000..1f09cf9 --- /dev/null +++ b/internal/migrate/sql/003_ai_jobs_newest_first.up.sql @@ -0,0 +1,5 @@ +DROP INDEX IF EXISTS ai_jobs_pending_claim_idx; + +CREATE INDEX IF NOT EXISTS ai_jobs_pending_claim_idx + ON ai_jobs (status, priority DESC, scheduled_at DESC, created_at DESC) + WHERE status = 'pending'; diff --git a/internal/store/store.go b/internal/store/store.go index 8a71cef..ec31e3b 100644 --- a/internal/store/store.go +++ b/internal/store/store.go @@ -330,7 +330,7 @@ WITH picked AS ( AND scheduled_at <= NOW() AND (cardinality($1::text[]) = 0 OR task_type = ANY($1::text[])) AND (cardinality($2::text[]) = 0 OR model_profile = ANY($2::text[])) - ORDER BY priority DESC, scheduled_at ASC, created_at ASC + ORDER BY priority DESC, scheduled_at DESC, created_at DESC LIMIT $3 FOR UPDATE SKIP LOCKED ) diff --git a/migrations/003_ai_jobs_newest_first.down.sql b/migrations/003_ai_jobs_newest_first.down.sql new file mode 100644 index 0000000..5d32349 --- /dev/null +++ b/migrations/003_ai_jobs_newest_first.down.sql @@ -0,0 +1,5 @@ +DROP INDEX IF EXISTS ai_jobs_pending_claim_idx; + +CREATE INDEX IF NOT EXISTS ai_jobs_pending_claim_idx + ON ai_jobs (status, priority DESC, scheduled_at ASC, created_at ASC) + WHERE status = 'pending'; diff --git a/migrations/003_ai_jobs_newest_first.up.sql b/migrations/003_ai_jobs_newest_first.up.sql new file mode 100644 index 0000000..1f09cf9 --- /dev/null +++ b/migrations/003_ai_jobs_newest_first.up.sql @@ -0,0 +1,5 @@ +DROP INDEX IF EXISTS ai_jobs_pending_claim_idx; + +CREATE INDEX IF NOT EXISTS ai_jobs_pending_claim_idx + ON ai_jobs (status, priority DESC, scheduled_at DESC, created_at DESC) + WHERE status = 'pending';