9

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 ?

Branko Dragovic
  • 185
  • 4
  • 16
  • I got the very similar problem and tried using the php serialize unserialize methods but seems they are not serialized with this method. – Jose Villalobos Sep 07 '17 at 21:35

3 Answers3

13

$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
Yezan Rafed
  • 532
  • 5
  • 15
  • 1
    Everything else in Laravel seems so clean and elegant and this is just ... not ... but it does work! – Witt Dec 12 '18 at 11:50
  • Is this a secure way to unserialize an object? – Deepak Sharma Dec 19 '19 at 08:16
  • According to the [PHP documentation](https://www.php.net/manual/en/function.unserialize.php), You should not pass untrusted user input to the `unserialize` function. You need to ensure job data was properly encoded, escaped and filtered before being stored into the DB. – Yezan Rafed Jan 07 '20 at 10:29
3

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"
sisve
  • 19,501
  • 3
  • 53
  • 95
1

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.

Francesco Taioli
  • 2,687
  • 1
  • 19
  • 34