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

故事扮演 提交于 2019-12-21 17:36:19

问题


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.


回答1:


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



回答2:


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.




回答3:


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


来源:https://stackoverflow.com/questions/40329206/how-to-get-the-queued-job-from-job-id-in-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!