How to give priority in laravel jobs?

by filiberto , in category: PHP Frameworks , 3 months ago

How to give priority in laravel jobs?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by daisha , 3 months ago

@filiberto 

In Laravel, you can give priority to jobs by setting the public $priority property in your job class. The $priority property should be set to a value between 1 (highest priority) and 10 (lowest priority). By default, jobs have a priority of 5.


Here's an example of how to set the priority of a job:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?php

namespace AppJobs;

use IlluminateBusQueueable;
use IlluminateContractsQueueShouldQueue;
use IlluminateFoundationBusDispatchable;
use IlluminateQueueInteractsWithQueue;
use IlluminateQueueSerializesModels;

class ProcessJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public $priority = 1;

    public function __construct()
    {
        //
    }

    public function handle()
    {
        // Logic for processing job
    }
}


Once you have set the priority of your job, you can dispatch it as usual and Laravel will prioritize the job based on its priority value. Jobs with a higher priority will be processed before jobs with a lower priority.