I am studying Job dispatching from here
Problem
I read that each connection has queue parameter which tell the queue name. Question is: How can I set the priority to send low or medium or high priority email?
Cmd prompt
I am using command: php artisan queue:listen to process job.
What I tried?
php artisan queue:work --queue=high,default
but this code never works if connection's queue parameter has value is not high
Default Queue Driver
'default' => env('QUEUE_DRIVER', 'database'),
Queue Connections
'connections' => [
'Register' => [
'driver' => 'database',
'table' => 'tbljobs',
'queue' => 'low',
'retry_after' => 5,
],
'ForgotPassword' => [
'driver' => 'database',
'table' => 'tbljobs',
'queue' => 'low',
'retry_after' => 5,
],
],
.env
QUEUE_DRIVER=Register
Controller Code for Register email
$job = (new SendActivationEmail($Data))
->onConnection('Register');
dispatch($job);
Controller Code for Reset Password
$job = (new SendResetPasswordEmail($this->tokens->create($user), $user))
->onConnection('ForgotPassword');
dispatch($job);
Take note of the Connections Vs. Queues note in Laravel's queue documentation, which applies to all queue drivers apart from SQS as far as I'm aware.
Before getting started with Laravel queues, it is important to understand the distinction between "connections" and "queues". In your config/queue.php configuration file, there is a connections configuration option. This option defines a particular connection to a backend service such as Amazon SQS, Beanstalk, or Redis. However, any given queue connection may have multiple "queues" which may be thought of as different stacks or piles of queued jobs.
Note that each connection configuration example in the queue configuration file contains a queue attribute. This is the default queue that jobs will be dispatched to when they are sent to a given connection. In other words, if you dispatch a job without explicitly defining which queue it should be dispatched to, the job will be placed on the queue that is defined in the queue attribute of the connection configuration:
In effect you will register one queue connection in your config/queues.php
file and the default
parameter will simply be the queue that jobs are dispatched to by default, if another queue is not provided.
Vitaly's answer above would be the correct approach to the problem (consolidate to a single connection with a default queue) then adjust your jobs to get sent to different queues if required. This is some important (I think) context to how how queue configuration works.
'connections' => [
'Register' => [ //<this name is connection name
'driver' => 'database',
'table' => 'tbljobs',
'queue' => 'low', //<this name is default queue name then you register a queue using this connection
'retry_after' => 5,
],
],
I suggest you modify your code in the following way:
'connections' => [
'Register' => [
'driver' => 'database',
'table' => 'tbljobs',
'queue' => 'default',
'retry_after' => 5,
],
],
High priority job - Controller Code for Register email
$job = (new SendActivationEmail($Data))
->onConnection('Register')
->onQueue("high");
dispatch($job);
Medium priority job - Controller Code for Reset Password
$job = (new SendResetPasswordEmail($this->tokens->create($user), $user))
->onConnection('Register')
->onQueue("medium");
dispatch($job);
Low priority job
dispatch((new LowPriorityJob())->onQueue("low"));
Default priority job
dispatch((new DefaultPriorityJob()));
->onConnection('Register') //this line is usefull if you specify you default connection is Register in .env QUEUE_DRIVER=Register
Run your jobs
this command run your jobs stored in default connection. In your case Register
php artisan queue:work --queue=high,medium,low,default
this command run your jobs stored in customConnectionName connection
php artisan queue:work customConnectionName --queue=high,medium,low,default
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With