-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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] Accepts BackedEnum for onQueue, onConnection, allOnQueue, and allOnConnection methods in the Queueable trait #52604
[11.x] Accepts BackedEnum for onQueue, onConnection, allOnQueue, and allOnConnection methods in the Queueable trait #52604
Conversation
Why not just pull out the BackedEnum's value directly before passing it to the methods? |
Because it's long and somewhat tedious always to do There are multiple parts in the framework that support |
why backed enums. why not also support non-backed enums? |
It's way more flexible than the |
It's not flexible at all. With a backed enum I am basically write the name twice. |
We do something like this in our application. We actually even went a step further and created a mapping between QueueConnection and Queue. First we have a QueueConnection for connection names. enum QueueConnection: string
{
case SHORT = 'short';
case LONG = 'long';
} enum QueueName: string
{
case ANALYTICS = 'analytics';
case NOTIFICATIONS = 'notifications';
case LONG_JOBS = 'long_jobs';
public function getQueueConnection(): ?QueueConnection
{
return match($this) {
QueueName::NOTIFICATIONS => QueueConnection::SHORT,
QueueName::ANALYTICS, QueueName::LONG_JOBS => QueueConnection::LONG,
default => null // just for illustration
};
}
} And then added a method inside of our jobs like this: /**
* @param string|QueueEnum|null $queue
* @return $this
*/
public function onQueue($queue)
{
if ($queue instanceof QueueEnum) {
if ($queueConnection = $queue->getQueueConnection()) {
$this->onConnection($queueConnection);
}
$queue = $queue->value;
}
$this->queue = $queue;
return $this;
} Now inside of our Jobs, we only have to specify the queue and we get the connection along with it. class SomeJob /* pretend all of the extends and use statements are here */
{
public function __construct($param1, $param2) {
$this->onQueue(QueueName::LONG_JOB);
}
} |
Hey @sethsandaru. Nice additon 👍 Just tried this out and it was not working as I expected it. If start with "dispatch" and then chain "onConnection" it does NOT work to use a BackendEnum, because then the code is referring to a At least this is what my IDE tells me. Am I missing something? |
@christophrumpel Thanks, Yeah I forgot to add the BackendEnum to My usecase: my Off-topicI'd suggest assigning the queue's name & connection inside the Job itself, outer layer shouldn't really determine that (just like the |
Ah yeah, I see that now. I will also use it like that in my video, thanks 👍 |
cc @christophrumpel PendingDispatch is updated (ref #52739), let's wait for the next release ✌️ Edit: looks like it is included in the latest release today 🚀 |
great 🙌 thanks |
@sethsandaru It's also missing from
|
@FrittenKeeZ yeah this PR was mainly focused on normal jobs, batch is not supported. Perhaps something we can add 😉 |
Hi guys,
I believe there are a lot of applications out there that are maintaining the Queue Names & Connections using Enums, which is awesome and less hardcoded string.
This PR enables us to pass an Enum instance to these methods:
onQueue
onConnection
allOnQueue
allOnConnection
This shouldn't bring any breaking changes. Tests are covered everything ✌️
Note: this only enhances those listed methods above, for jobs that use
$connection
&$queue
properties, they still need to use a normal string (orenum->value
)Before
After