Skip to content

Add the WithoutOverlapping job middleware - #388

Open
kdrmlhcn wants to merge 1 commit into
laravel:0.xfrom
kdrmlhcn:queue-without-overlapping
Open

kdrmlhcn wants to merge 1 commit into
laravel:0.xfrom
kdrmlhcn:queue-without-overlapping

Conversation

@kdrmlhcn

@kdrmlhcn kdrmlhcn commented Dec 1, 2026

Copy link
Copy Markdown

This PR adds the WithoutOverlapping job middleware from Laravel's Illuminate\Queue\Middleware. The queues guide currently suggests building it by hand around a cache lock. The lock API is already in place, so the middleware is a thin layer over cache::lock.

use laravel::queue::{IntoMiddleware, WithoutOverlapping, job};

#[job]
impl UpdateCreditScore {
    fn middleware(&self) -> impl IntoMiddleware {
        WithoutOverlapping::new(self.user_id)
            .release_after(10.seconds())
            .expire_after(3.minutes())
    }

    async fn handle(self) -> laravel::Result<()> {
        // ...
        Ok(())
    }
}

Behavior

It follows WithoutOverlapping on Laravel 13.x:

Laravel Here
new WithoutOverlapping($key) WithoutOverlapping::new(key), where the key is anything Display
Lock named laravel-queue-overlap:{job}:{key} Same, using Attempt::name() for the job
Releases immediately when the lock is taken (releaseAfter = 0) Same: attempt.release(Duration::ZERO)
releaseAfter($seconds), dontRelease() release_after(span), dont_release()
expireAfter($seconds) expire_after(span)
withPrefix($prefix), shared() with_prefix(prefix), shared()
Lock released in a finally block Lock released through Lock::get, which also releases it when the attempt is cancelled by its timeout

There is one intentional difference. Laravel's default expiresAfter is 0 (no expiration), but cache::lock rejects a zero duration. The default is therefore 24 hours, exposed as WithoutOverlapping::DEFAULT_EXPIRATION. This matches the default the scheduler's without_overlapping already uses.

A skipped attempt with dont_release() ends without running handle and without releasing, so it succeeds and is removed from the queue. This matches Laravel and the documented rule for middleware that skip next.

Docs

docs/queues.md gains a "Preventing job overlaps" subsection under "Job middleware", modeled on the Laravel guide's section. It covers keys, release_after, dont_release, expire_after, shared, and a note that cross-server protection needs a shared lock store. The intro sentence that suggested building the middleware by hand now links to it.

Tests

tests/queue_overlap.rs adds 6 tests, run through test attempts with the memory cache store:

  • lock naming, including shared() and with_prefix()
  • a free lock runs the job and is released afterward
  • a taken lock releases the attempt (immediately, and with release_after) without running handle, while a different key still runs
  • dont_release() ends the attempt as succeeded without running handle
  • shared() jobs wait on a lock taken under the shared name, while non-shared jobs with the same key don't
  • the lock is released after the job returns an error and after it panics

The queue suite passes, with 256 tests. In the core suite, console::listings_sort_commands_under_their_first_namespace_and_hide_private_commands fails locally without the database feature. It fails the same way on 0.x and is unrelated to this change.

Port Laravel's WithoutOverlapping queue middleware. It takes a cache
lock named after the job and a key, runs the job while holding it, and
releases an attempt that finds the lock taken, with release_after,
dont_release, expire_after, with_prefix, and shared to configure it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant