Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] Introduce throw() to PendingDispatch #52394

Draft
wants to merge 15 commits into
base: 11.x
Choose a base branch
from

Conversation

tiramisuliu
Copy link

This pull request introduces an optional feature to throw an exception when attempting to dispatch a subsequent unique job that is already in the queue. This enhancement provides clear feedback to the job dispatcher, indicating that the current dispatch attempt has been ignored due to the presence of an existing unique job.

Details:

  • Adds an option to throw JobDispatchedException if a unique job with the same identifier is already dispatched.
  • Allows PendingDispatch to register a callback to be called right before the exception is about to be thrown - similar to the throw() behavior from the HTTP Client/Response and the Process Facade.

Example Usage:

An exception is thrown if the job fails to meet the "unique" requirement.

UpdateSearchIndex::dispatch()
    ->throw();

The closure is executed immediately before the exception is thrown.

UpdateSearchIndex::dispatch()
    ->throw(function () {
    });

The existing behavior of ignoring subsequent dispatches of the same unique job is preserved.

UpdateSearchIndex::dispatch();

Note:

I'm currently having trouble instantiating an instance of Illuminate\Contracts\Cache\Repository in the test - appreciate any pointers!

Copy link

github-actions bot commented Aug 6, 2024

Thanks for submitting a PR!

Note that draft PR's are not reviewed. If you would like a review, please mark your pull request as ready for review in the GitHub user interface.

Pull requests that are abandoned in draft may be closed due to inactivity.

@tiramisuliu tiramisuliu force-pushed the feature/make-pending-dispatch-throwable branch from 8f48575 to 4780bd1 Compare August 6, 2024 05:54
@driesvints driesvints changed the title Introduce throw() to PendingDispatch [11.x] Introduce throw() to PendingDispatch Aug 9, 2024
@hn-seoai
Copy link
Contributor

hn-seoai commented Sep 4, 2024

For consistency with similar functionality in batches/chains, wouldn't catch(callable $callback) be a better name?

@shaedrich
Copy link

I don't like the quite broad naming. Wouldn't it be better to have something like throwOnDuplicates() or the like?

Another possible parameter to this method could be a class-string indicating an alternative exception to throw as syntactic sugar to throwing it oneself within a callback.

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.

3 participants