Jaimin
Jaimin

Reputation: 872

how to Prioritize queued events?

I am working in laravel 5.0

I am using database as queue driver. I have created 2 events A and B. I want event B to always execute first. Even if Event B is inserted in Jobs table after Event A it should execute first.

Both of my events consists of

implements ShouldBeQueued

and

 use InteractsWithQueue;

My Event handler is like this..

 <?php namespace App\Handlers\Events;

use App\Events\A;

use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldBeQueued;

class A implements ShouldBeQueued { 
use InteractsWithQueue; 

/**
 * Create the event handler.
 *
 * @return void
 */
public function __construct()
{
    //
}

/**
 * Handle the event.
 *
 * @param  A  $event
 * @return void
 */
public function handle(A $event)
{
    $alldeviceID = $event->alldeviceID;
    $gcmMessage  = $event->gcmMessage;


    send_notification($alldeviceID, $gcmMessage);

 }
}

So both events are going into jobs table and i have supervisor(process manager) to handle them..

It is working fine so far, Just that i want to give high priority to Event B over Event A.

Any help would be appreciated..

Upvotes: 2

Views: 2502

Answers (1)

Andrej
Andrej

Reputation: 7504

Solution for laravel > 5.0. According to the manual https://www.laravel.com/docs/5.3/queues#queue-priorities you should define different queues for this purpose e.g names high and low. Message with higher priority should go to high queue like dispatch((new Job)->onQueue('high'));. What is more you should start your workers with following command

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

where --queue=high,low defines the priority in which messages will be processed.

Solution for laravel 5.0

\Queue::pushOn('high', new Job);

You should start your workers with following command

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

Upvotes: 2

Related Questions