Pankaj
Pankaj

Reputation: 10105

How to set high, low and medium priority email using queue?

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);

Upvotes: 23

Views: 24150

Answers (5)

meow2x
meow2x

Reputation: 2124

Queue priorities is explained here https://laravel.com/docs/5.7/queues#queue-priorities. You simply need to "pass a comma-delimited list of queue names to the work command."

As the documentation says:

php artisan queue:work --queue=high,low

So dispatch((new Job)->onQueue('high')) will be given higher priority than dispatch((new Job)->onQueue('low')).

Or you could use your custom queue names:

php artisan queue:work --queue=first,second.

Upvotes: 1

Dwight
Dwight

Reputation: 12460

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.

Upvotes: 14

Vitaliy Ryaboy
Vitaliy Ryaboy

Reputation: 478

'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

Upvotes: 17

Heartbeat
Heartbeat

Reputation: 142

Try this

'connections' => [

'Register-low' => [
    'driver'        =>  'database',
    'table'         =>  'tbljobs',
    'queue'         =>  'low',
    'retry_after'   =>  5,
],
'Register-high' => [
    'driver'        =>  'database',
    'table'         =>  'tbljobs',
    'queue'         =>  'high',
    'retry_after'   =>  5,
],

'ForgotPassword' => [
    'driver'        =>  'database',
    'table'         =>  'tbljobs',
    'queue'         =>  'low',
    'retry_after'   =>  5,
],

],

AND then

php artisan queue:listen --queue=Register-high,Register-low

Upvotes: 4

avip
avip

Reputation: 1465

You should have two queues defined in your config/queue.php file. Say, one with the name "high" and the other "low".

Then, you can then dispatch jobs to them as needed like this:

$job = (new SendResetPasswordEmail($this->tokens->create($user), $user))
        ->onConnection('ForgotPassword');
dispatch($job)->onQueue('high'));

Note: ->onQueue('high')

Finally, you would run: php artisan queue:work --queue=high,low

This will start a worker that will process all jobs on "high" queue before moving on to jobs on "low".

Upvotes: 7

Related Questions