laravel deserialize / decode job raw body

时间:2017-08-03 21:16:05

标签: laravel jobs laravel-queue laravel-jobs

我在这里遇到一个问题。样本会自己说话。

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

)});

这就是$ job_details的样子:

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

我想从$ job_details ['数据'] ['命令']中获取一些参数。 有没有一些简单的方法可以做到这一点,或者我需要一些自制的灵魂?

2 个答案:

答案 0 :(得分:4)

$event->job->getRawBody会返回一个字符串,因此您无法撰写$job_details['data']['command'],最终会得到Illegal string offset error

我正在使用Laravel 5.4,我已设法使用Job检索我的$event->job->payload()实例,然后根据documentation应用unserialize方法。

所以我做的是:

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

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

    $myJob->getMyProperty();

    //... Just work with $myJob as if it were your job class

答案 1 :(得分:1)

$job_details["data"]["command"]是从serialize($value)生成的字符串。您可以unserialize($str)创建字符串表示的作业对象。然后,您可以根据通常的可见性规则访问属性。

$job = unserialize($job_details["data"]["command"]);
dump($job->queue;) // "update_data"