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] Add Dispatchable::newPendingDispatch() #54153

Merged
merged 3 commits into from
Jan 10, 2025

Conversation

cosmastech
Copy link
Contributor

@cosmastech cosmastech commented Jan 10, 2025

This consolidates the creation of a PendingDispatch. It does not however consolidate for the dispatch() helper method.

Why

Ultimately, what I would like to be able to do is call TwSync::dispatch($team) and be able to universally set the queue/connection without having to write it repeatedly. We would just "extend" the Dispatchable trait and modify the newPendingDispatch function so it calls another function on the job before instantiating the PendingDispatch. That would be something like this in userland code:

protected static function newPendingDispatch($job)
{
    return parent::newPendingDispatch($job->prepareForDispatch());
}

We are essentially creating a post-job instantiation, pre-PendingDispatching hook.

We have Horizon set up with 3 connections and 3-4 queues each, so it's easy to mismatch, and there's a lot of duplicative code in our job constructors where we set the queue/connection. Easy to forget to do and then find out you've been dispatching a job to connection that doesn't handle a particular queue (so the job just sits in queued job purgatory).

Alternate solution

Add a if method_exists($job, 'prepareForDispatch') { ... } clause somewhere, not sure if that would be the dispatcher or the PendingDispatch constructor.

@cosmastech cosmastech changed the title [11.x] Add Dispatchable::createPendingDispatch() [11.x] Add Dispatchable::newPendingDispatch() Jan 10, 2025
@taylorotwell taylorotwell merged commit 61492a8 into laravel:11.x Jan 10, 2025
38 checks passed
@cosmastech cosmastech deleted the create-pending-dispatch branch January 10, 2025 21:34
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.

2 participants