7

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.

Debiprasad
  • 5,895
  • 16
  • 67
  • 95

3 Answers3

22

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);
Armin
  • 2,397
  • 2
  • 21
  • 31
0

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.

tristanbailey
  • 4,427
  • 1
  • 26
  • 30
-1

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;
    }
}
Karl Hill
  • 12,937
  • 5
  • 58
  • 95
rrCKrr
  • 19
  • 4