diff options
Diffstat (limited to 'makima/src/db/repository.rs')
| -rw-r--r-- | makima/src/db/repository.rs | 36 |
1 files changed, 21 insertions, 15 deletions
diff --git a/makima/src/db/repository.rs b/makima/src/db/repository.rs index 6b3f15f..4298fa5 100644 --- a/makima/src/db/repository.rs +++ b/makima/src/db/repository.rs @@ -812,15 +812,18 @@ pub async fn get_pending_tasks_for_contract( ) -> Result<Vec<Task>, sqlx::Error> { sqlx::query_as::<_, Task>( r#" - SELECT * FROM tasks - WHERE contract_id = $1 AND owner_id = $2 - AND status = 'pending' - AND is_supervisor = false - AND retry_count < max_retries + SELECT t.* FROM tasks t + WHERE t.contract_id = $1 AND t.owner_id = $2 + AND t.status = 'pending' + AND t.retry_count < t.max_retries + AND (t.is_supervisor = false + OR EXISTS (SELECT 1 FROM contracts c + WHERE c.id = t.contract_id + AND (c.directive_id IS NOT NULL OR c.is_directive_orchestrator = true))) ORDER BY - interrupted_at DESC NULLS LAST, - priority DESC, - created_at ASC + t.interrupted_at DESC NULLS LAST, + t.priority DESC, + t.created_at ASC "#, ) .bind(contract_id) @@ -836,13 +839,16 @@ pub async fn get_all_pending_task_contracts( ) -> Result<Vec<(Uuid, Uuid)>, sqlx::Error> { sqlx::query_as::<_, (Uuid, Uuid)>( r#" - SELECT DISTINCT contract_id, owner_id - FROM tasks - WHERE contract_id IS NOT NULL - AND status = 'pending' - AND is_supervisor = false - AND retry_count < max_retries - ORDER BY owner_id, contract_id + SELECT DISTINCT t.contract_id, t.owner_id + FROM tasks t + WHERE t.contract_id IS NOT NULL + AND t.status = 'pending' + AND t.retry_count < t.max_retries + AND (t.is_supervisor = false + OR EXISTS (SELECT 1 FROM contracts c + WHERE c.id = t.contract_id + AND (c.directive_id IS NOT NULL OR c.is_directive_orchestrator = true))) + ORDER BY t.owner_id, t.contract_id "#, ) .fetch_all(pool) |
