Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to capture job queue details after completion in Laravel 5.1?

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?

like image 466
Marty Thomas Avatar asked Nov 19 '15 15:11

Marty Thomas


2 Answers

For Laravel 5.2:

Queue::after(function (JobProcessed $event) {
    $command = unserialize($event->data['data']['command']);
});
like image 102
James Avatar answered Sep 26 '22 14:09

James


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

like image 20
Gregorio Hernández Caso Avatar answered Sep 24 '22 14:09

Gregorio Hernández Caso