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

耗尽温柔 提交于 2019-12-04 10:04:52

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

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.

You can simply use DB::table() for searching the particular job by it id, as 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;
}
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!