diff --git a/.sqlx/query-372f1019c59dddf3cf6389f5a3bf77683c7bf0eb4711c57e3196820d18991cfa.json b/.sqlx/query-372f1019c59dddf3cf6389f5a3bf77683c7bf0eb4711c57e3196820d18991cfa.json deleted file mode 100644 index fd4c1b0..0000000 --- a/.sqlx/query-372f1019c59dddf3cf6389f5a3bf77683c7bf0eb4711c57e3196820d18991cfa.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "db_name": "SQLite", - "query": "WITH j AS (\n SELECT\n value ->> 'task_id' AS task_id,\n value ->> 'attempt' AS attempt,\n value ->> 'result' AS result,\n value ->> 'status' AS status\n FROM\n json_each(?1)\n)\nUPDATE\n Jobs\nSET\n status = j.status,\n attempts = j.attempt,\n last_result = j.result,\n done_at = strftime('%s', 'now')\nFROM\n j\nWHERE\n Jobs.id = j.task_id\n AND Jobs.lock_by = ?2\n", - "describe": { - "columns": [], - "parameters": { - "Right": 2 - }, - "nullable": [] - }, - "hash": "372f1019c59dddf3cf6389f5a3bf77683c7bf0eb4711c57e3196820d18991cfa" -} diff --git a/.sqlx/query-90625a041d5011e575d67961618f7b716c7bdcf099b75e0aa4a86a320f69854d.json b/.sqlx/query-90625a041d5011e575d67961618f7b716c7bdcf099b75e0aa4a86a320f69854d.json new file mode 100644 index 0000000..c8d1987 --- /dev/null +++ b/.sqlx/query-90625a041d5011e575d67961618f7b716c7bdcf099b75e0aa4a86a320f69854d.json @@ -0,0 +1,12 @@ +{ + "db_name": "SQLite", + "query": "WITH j AS (\n SELECT\n value ->> 'task_id' AS task_id,\n value ->> 'attempt' AS attempt,\n value ->> 'result' AS result,\n value ->> 'status' AS status\n FROM\n json_each(?1)\n)\nUPDATE\n Jobs\nSET\n status = j.status,\n attempts = j.attempt,\n last_result = j.result,\n -- A deferred task goes back to Pending and must be unlocked to be fetched again.\n lock_by = CASE WHEN j.status = 'Pending' THEN NULL ELSE lock_by END,\n lock_at = CASE WHEN j.status = 'Pending' THEN NULL ELSE lock_at END,\n done_at = CASE WHEN j.status = 'Pending' THEN NULL ELSE strftime('%s', 'now') END\nFROM\n j\nWHERE\n Jobs.id = j.task_id\n AND Jobs.lock_by = ?2\n", + "describe": { + "columns": [], + "parameters": { + "Right": 2 + }, + "nullable": [] + }, + "hash": "90625a041d5011e575d67961618f7b716c7bdcf099b75e0aa4a86a320f69854d" +} diff --git a/CHANGELOG.md b/CHANGELOG.md index 1b070d3..c08687f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +- fix: a task deferred with `DeferredError` or `RetryAfterError` is unlocked so it runs again - perf: compute the queue statistics in a single pass over `Jobs` instead of 18 `UNION ALL` aggregate branches, and drop the per-queue correlated subqueries for the workers and the activity - perf: add `idx_jobs_status_type_recent` and `idx_jobs_status_recent` so listing recent tasks no longer sorts the whole table diff --git a/queries/task/ack.sql b/queries/task/ack.sql index c4a9f7d..d887666 100644 --- a/queries/task/ack.sql +++ b/queries/task/ack.sql @@ -13,7 +13,10 @@ SET status = j.status, attempts = j.attempt, last_result = j.result, - done_at = strftime('%s', 'now') + -- A deferred task goes back to Pending and must be unlocked to be fetched again. + lock_by = CASE WHEN j.status = 'Pending' THEN NULL ELSE lock_by END, + lock_at = CASE WHEN j.status = 'Pending' THEN NULL ELSE lock_at END, + done_at = CASE WHEN j.status = 'Pending' THEN NULL ELSE strftime('%s', 'now') END FROM j WHERE diff --git a/src/lib.rs b/src/lib.rs index 22f02ff..57c5df5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -302,7 +302,10 @@ mod tests { use futures::{StreamExt, future::ready, stream}; use serde::{Deserialize, Serialize}; use sqlx::SqlitePool; - use std::time::{Duration, Instant}; + use std::{ + sync::atomic::{AtomicUsize, Ordering}, + time::{Duration, Instant}, + }; use super::*; @@ -584,4 +587,29 @@ mod tests { assert!(info.activity.iter().sum::() >= 1); assert!(info.workers.is_empty()); } + + #[tokio::test] + async fn deferred_task_runs_again() { + static RUNS: AtomicUsize = AtomicUsize::new(0); + let pool = SqlitePool::connect(":memory:").await.unwrap(); + SqliteStorage::setup(&pool).await.unwrap(); + let mut backend = SqliteStorage::new(&pool); + backend.push(1usize).await.unwrap(); + + async fn defer_once(_: usize, wrk: WorkerContext) -> Result<(), BoxDynError> { + if RUNS.fetch_add(1, Ordering::SeqCst) == 0 { + return Err(DeferredError::new("not yet").into()); + } + wrk.stop().unwrap(); + Ok(()) + } + let worker = WorkerBuilder::new("deferred") + .backend(backend) + .build(defer_once); + tokio::time::timeout(Duration::from_secs(10), worker.run()) + .await + .expect("the deferred task never ran again") + .unwrap(); + assert_eq!(RUNS.load(Ordering::SeqCst), 2); + } }