Repository navigation
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR adds the
WithoutOverlappingjob middleware from Laravel'sIlluminate\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 overcache::lock.Behavior
It follows
WithoutOverlappingon Laravel13.x:new WithoutOverlapping($key)WithoutOverlapping::new(key), where the key is anythingDisplaylaravel-queue-overlap:{job}:{key}Attempt::name()for the jobreleaseAfter = 0)attempt.release(Duration::ZERO)releaseAfter($seconds),dontRelease()release_after(span),dont_release()expireAfter($seconds)expire_after(span)withPrefix($prefix),shared()with_prefix(prefix),shared()finallyblockLock::get, which also releases it when the attempt is cancelled by its timeoutThere is one intentional difference. Laravel's default
expiresAfteris0(no expiration), butcache::lockrejects a zero duration. The default is therefore 24 hours, exposed asWithoutOverlapping::DEFAULT_EXPIRATION. This matches the default the scheduler'swithout_overlappingalready uses.A skipped attempt with
dont_release()ends without runninghandleand without releasing, so it succeeds and is removed from the queue. This matches Laravel and the documented rule for middleware that skipnext.Docs
docs/queues.mdgains 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.rsadds 6 tests, run through test attempts with thememorycache store:shared()andwith_prefix()release_after) without runninghandle, while a different key still runsdont_release()ends the attempt as succeeded without runninghandleshared()jobs wait on a lock taken under the shared name, while non-shared jobs with the same key don'tThe
queuesuite passes, with 256 tests. In thecoresuite,console::listings_sort_commands_under_their_first_namespace_and_hide_private_commandsfails locally without thedatabasefeature. It fails the same way on0.xand is unrelated to this change.