How to capture job queue details after completion

2019-03-30 02:29发布

In Laravel 5.1, I would like to be notified when a job is completed, with details about the job (specifically, the user_id, and customer_id). I'm using the Queue::after method method in the AppServiceProvider as Laravel suggests.

If a dump the $data parameter like so:

Queue::after(function ($connection, $job, $data) {
    var_dump($data);
    exit;
});

I get the following:

array(2) { ["job"]=> string(39) "Illuminate\Queue\CallQueuedHandler@call" ["data"]=> array(1) { ["command"]=> string(263) "O:23:"App\Jobs\ExportContacts":4:{s:7:"*data";a:5:{s:7:"user_id";s:1:"2";s:10:"customer_id";s:1:"1";s:6:"filter";N;s:5:"dates";a:2:{i:0;i:1445352975;i:1;i:1448034975;}s:4:"file";s:31:"/tend_10-20-2015-11-20-2015.csv";}s:5:"queue";N;s:5:"delay";N;s:6:"*job";N;}" } }

The data array contains both of the user_id and the customer_idthat I'm after. Which is great. But how do I parse this response to pull that information out.

My only thought is to use regex. But I thought I'd check to see if there was a better way?

4条回答
放我归山
2楼-- · 2019-03-30 02:43

The data is serialized. You only have to:
$command = unserialize($data['data']['command']);

查看更多
兄弟一词,经得起流年.
3楼-- · 2019-03-30 02:52

Is there a way to pull the variables that you passed to the job into the queue:after? Did you have to use regex? Even after unserializing its a massive string of data.

Still having trouble extracting data from the job that just ran into queue::after

查看更多
乱世女痞
4楼-- · 2019-03-30 03:00

I think that you do not need to get your data from data. I will show you simple use of Queue::later

$yourData = $data_which_you_need;

$date = Carbon::now()->addMinutes(10);

Queue::later($date, function($job) use ($yourData){
    // THIS CODE WILL BE EXECUTE AFTER 10 MINUTES
    $user_id = $yourData['user_id'];
    User::find($user_id)->destroy();
    $job->delete();
});

I hope that i help you

查看更多
Luminary・发光体
5楼-- · 2019-03-30 03:06

For Laravel 5.2:

Queue::after(function (JobProcessed $event) {
    $command = unserialize($event->data['data']['command']);
});
查看更多
登录 后发表回答