Debiprasad
Debiprasad

Reputation: 6153

How to get the queued job from job ID in Laravel?

Is there any way to get the queued job from the job ID in Laravel? While adding the job to the queue, I store the job ID. Later at some point of time (there is a delay to process the job in the queue), I want to remove the job from the queue. If I can get the job on the queue using the job ID, I can use delete() method to remove it.

Upvotes: 7

Views: 6405

Answers (3)

rrCKrr
rrCKrr

Reputation: 19

You can use DB::table() for searching the particular job by id, and while dispatching the job it returns the job table's id.

use DB;

class ServiceClass
{
    public function deleteJobIfExists($id)
    {
        $jobTable = 'jobs';
        $job = DB::table($jobTable)->find($id);
        
        return $job ? ($job->delete ? 1 : -1) : 0;
    }
}

Upvotes: -1

Armin
Armin

Reputation: 2562

I use this code for laravel 5.5 :

use Illuminate\Contracts\Bus\Dispatcher;

$job = ( new JOB_CLASS() )->onQueue('QUEUE_NAME')->delay('DELAY');
$id  = app(Dispatcher::class)->dispatch($job);

Upvotes: 22

tristanbailey
tristanbailey

Reputation: 4605

It is a queue so you can not select it, but if you are logging the data also outside the queue you could look in the Queue::before(){} added to AppServiceProvider.php to check the stored id or reference to the jobs as they come off the queue and before processed.

I am also working on this area so if I figure out the code for this, and will post it if I do. As you are getting an event back here in the before() so you have to unwrap it and get the Job out to examine.

Upvotes: 0

Related Questions