Skip to content
Open
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
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions crates/queue/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ aws-smithy-http-client = { workspace = true, optional = true }
clap.workspace = true
comfy-table.workspace = true
futures-util.workspace = true
getrandom.workspace = true
inventory.workspace = true
jiff.workspace = true
laravel-rust-queue-macros.workspace = true
Expand Down
14 changes: 11 additions & 3 deletions crates/queue/src/queue/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,13 @@ fn later(now: Timestamp, delay: Duration) -> Timestamp {
now.checked_add(delay).unwrap_or(Timestamp::MAX)
}

/// Where a worker starts among the `count` candidates after the oldest one. Workers that lose the oldest job to
/// another worker then try different jobs instead of all trying the next one.
fn start(count: usize) -> usize {
let random = usize::try_from(getrandom::u32().unwrap_or_default()).unwrap_or_default();
random.checked_rem(count).unwrap_or_default()
}

#[derive(Clone)]
struct Table(Builder);

Expand All @@ -136,7 +143,7 @@ impl Table {
async fn reserve(&self, queue: &str) -> Result<Option<Reserved>> {
loop {
let now = system_timestamp();
let candidates: Vec<(i64, i64)> = self
let mut candidates: Vec<(i64, i64)> = self
.0
.clone()
.select(["id", "attempts"])
Expand All @@ -146,9 +153,10 @@ impl Table {
.use_write_connection()
.get()
.await?;
if candidates.is_empty() {
let Some((_, rest)) = candidates.split_first_mut() else {
return Ok(None);
}
};
rest.rotate_left(start(rest.len()));

for (id, attempts) in candidates {
if let Some(reserved) = self.claim(id, attempts, now).await? {
Expand Down
17 changes: 17 additions & 0 deletions tests/queue_worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,22 @@ async fn sqlite_workers_retry_fail_and_recover_jobs() {
worker_contract(&app).await;
}

#[cfg(any(feature = "database-postgres", feature = "database-mysql"))]
async fn racing_workers_contract(app: &Application) {
for note in 0..40 {
dispatch(app, Note(format!("note {note:02}"))).await;
}

let (first, second, third) = tokio::join!(work(app, ""), work(app, ""), work(app, ""));
first.assert_successful();
second.assert_successful();
third.assert_successful();
let mut notes = records(app).await;
notes.sort();
assert_eq!(notes, (0..40).map(|note| format!("note {note:02}")).collect::<Vec<_>>());
assert!(rows(app).await.is_empty());
}

#[cfg(feature = "database-postgres")]
#[tokio::test]
#[ignore = "requires LARAVEL_TEST_POSTGRES_URL pointing to a disposable database"]
Expand All @@ -622,6 +638,7 @@ async fn database_contracts(url: String) {
let app = application(url.clone()).await;
sizes_contract(&app).await;
worker_contract(&app).await;
racing_workers_contract(&application(url.clone()).await).await;
#[cfg(feature = "database-sqlite")]
crate::queue_batches::concurrent_completion_contract(&crate::queue_batches::application(url, "database").await)
.await;
Expand Down