Skip to content

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

2 participants