7

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

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['data']['command'] 获取一些参数。有没有一些简单的方法可以做到这一点,或者我需要一些自制的灵魂?

4

3 回答 3

11

$event->job->getRawBody返回一个字符串,所以你不能写$job_details['data']['command'],你最终会得到Illegal string offset error.

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

所以我所做的是:

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

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

    $myJob->getMyProperty();

    //... Just work with $myJob as if it were your job class
于 2017-11-05T15:17:23.327 回答
3

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

$job = unserialize($job_details["data"]["command"]);
dump($job->queue;) // "update_data"
于 2017-10-01T15:35:07.570 回答
0

I was having error with an email that contained some space in it, so I have to decode the payload of the failed jobs and update it.

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.

于 2021-12-04T09:15:12.083 回答