Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 4 additions & 1 deletion queries/task/ack.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 29 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;

Expand Down Expand Up @@ -584,4 +587,29 @@ mod tests {
assert!(info.activity.iter().sum::<usize>() >= 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);
}
}