Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

laravel deserialize/decode job raw body

i'm experiencing one problem here. Sample will speak for it self.

Queue::after(function (JobProcessed $event) {
$job_details = json_decode($event->job->getRawBody(), true);

)});

This is how $job_details looks like:

'displayName' => 'App\\Jobs\\CommandJob',
  'job' => 'Illuminate\\Queue\\CallQueuedHandler@call',
  'maxTries' => 10,
  'timeout' => NULL,
  'data' => 
  array (
    'commandName' => 'App\\Jobs\\CommandJob',
    'command' => 'O:19:"App\\Jobs\\CommandJob":9:{s:32:"' . "\0" . 'App\\Jobs\\CommandJob' . "\0" . 'commandName";N;s:30:"' . "\0" . 'App\\Jobs\\CommandJob' . "\0" . 'arguments";N;s:28:"' . "\0" . 'App\\Jobs\\CommandJob' . "\0" . 'command";s:20:"google:get-campaigns";s:5:"tries";i:10;s:32:"' . "\0" . 'App\\Jobs\\CommandJob' . "\0" . 'nextCommand";a:1:{i:0;s:19:"google:get-adgroups";}s:6:"' . "\0" . '*' . "\0" . 'job";N;s:10:"connection";N;s:5:"queue";s:11:"update_data";s:5:"delay";N;}',

I would like to get some params from $job_details['data']['command']. Is there some simple way to do this , or i need some home made soultion ?

like image 554
Branko Dragovic Avatar asked Aug 03 '17 21:08

Branko Dragovic


3 Answers

$event->job->getRawBody returns a string so you can't write $job_details['data']['command'] and you will end up with Illegal string offset error.

I am using Laravel 5.4 and i have managed to retrieve my Job instance using $event->job->payload() then applying the unserialize method according to the documentation.

So what i did is :

    $payload = $event->job->payload();

    $myJob = unserialize($payload['data']['command']);

    $myJob->getMyProperty();

    //... Just work with $myJob as if it were your job class
like image 199
Yezan Rafed Avatar answered Nov 05 '22 20:11

Yezan Rafed


The $job_details["data"]["command"] is a string generated from serialize($value). You can unserialize($str) it to create the job object represented by your string. You will then have access to the properties according to the usual visibility rules.

$job = unserialize($job_details["data"]["command"]);
dump($job->queue;) // "update_data"
like image 3
sisve Avatar answered Nov 05 '22 21:11

sisve


I was having error with an email that contained some space in it. To fix the problem and send it, I had to decode the payload of the failed jobs and remove the space in the email.

To do so, in php artisan tinker

// take some specific failed jobs
$failed_job = DB::table('failed_jobs')->where('id', $your_job_id)->first();
$payload = json_decode($failed_job->payload);
$obj = unserialize($payload->data->command);

   
// here I have the user content, I can see the wrong email
$user = $obj->notifiables; 
    
//update email and save
$user->email = "newemail@something"
$user->update()

As the last step, push again the jobs into the queue.

like image 1
Francesco Taioli Avatar answered Nov 05 '22 20:11

Francesco Taioli