Skip to content

[7.x] Allow setting the event handler queue via a getQueue() method#32770

Merged
taylorotwell merged 3 commits into
laravel:7.xfrom
lupinitylabs:7.x
Jun 8, 2020
Merged

[7.x] Allow setting the event handler queue via a getQueue() method#32770
taylorotwell merged 3 commits into
laravel:7.xfrom
lupinitylabs:7.x

Conversation

@lupinitylabs

Copy link
Copy Markdown
Contributor

Using queued event listeners, it is possible to set the queue the resulting job should be executed on using the $queue attribute. This attribute is then adopted by the dispatcher.

However, it is not possible to set the queue name from the config, the environment or otherwise programatically, because the listener instance is created without invoking the constructor in Dispatcher::createListenerAndJob(). Thus, trying to set the queue name in the constructor or anywhere else in the Listener has no effect.

This pull request proposes a non-breaking change to the Dispatcher class that allows a user to set the queue name by defining a getQueue() method within the Listener, as it is already done with retryAfter and retryUntil methods.

If no getQueue method is defined, the code falls back to use the $queue attribute or null, as it was before.

The benefit of this change is to provide developers with a flexible way to influence the target queues instead of having to hard code them.

@GrahamCampbell GrahamCampbell changed the title [7.x] allow setting the event handler queue via a getQueue() method. [7.x] Allow setting the event handler queue via a getQueue() method May 12, 2020
@taylorotwell

Copy link
Copy Markdown
Member
public function __construct()
{
    $this->queue = 'whatever-you-want';
}

@lupinitylabs

lupinitylabs commented May 13, 2020

Copy link
Copy Markdown
Contributor Author
public function __construct()
{
    $this->queue = 'whatever-you-want';
}

Sure, as I described above, that was my first attempt, but it doesn't work because the listener is instantiated using $listener = (new ReflectionClass($class))->newInstanceWithoutConstructor(); hence the pull request.

Please check out a quick project I hacked together at https://github.com/lupinitylabs/queuename in the hopes of making myself clearer.

Please note: the same issue exists with setting the tries property, so the pull request should probably even be extended by a method to set tries, too.

@lupinitylabs

Copy link
Copy Markdown
Contributor Author

@taylorotwell Any chance to have this re-opened?

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