【发布时间】:2022-06-10 17:20:16
【问题描述】:
我正在尝试自动化由多个作业组成的流程。每个作业都有一个目标,所以我正在运行一个全局作业,负责创建子作业并将它们添加到批处理实例中。
# Instantiate the batches array
$batches = [];
# Retrieve destinations that have a dataset
$destinations = Destinations::where('dataset','like','%xx%')
->where('dataset','like','%xx%')
->get();
# Checking if there is some results
if(!empty($destinations)){
foreach($destinations as $destination){
# Current DateTime
$dateTime = Carbon::now()->format('Y_m_d_h_i_s');
# Create an array of batches for every location
$batches[] = new AutoImport("auto_job_{$destination->reference}_{$dateTime}",$destination);
}
}
# Creating batch chainning
$batch = Bus::batch($batches)
->then(function (Batch $batch) {
})->catch(function (Batch $batch, Throwable $e) {
})->finally(function (Batch $batch) {
})->dispatch();
return $batch->id;
在某些时候,我遇到了这个错误 "Serialization of 'PDO' is not allowed",我认为是来自这一行 new AutoImport("auto_job_{$destination->reference}_{$dateTime}",$destination); 我将整个对象发送到批次数组的事实
我不确定,所以欢迎任何帮助。
工作抽象类
<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Bus\Batchable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Imtigger\LaravelJobStatus\Trackable;
abstract class Job implements ShouldQueue, ShouldBeUnique
{
/*
|--------------------------------------------------------------------------
| Queueable Jobs
|--------------------------------------------------------------------------
|
| This job base class provides a central location to place any logic that
| is shared across all of your jobs. The trait included with the class
| provides access to the "queueOn" and "delay" queue helper methods.
|
*/
use Batchable,InteractsWithQueue, Queueable, SerializesModels, Trackable;
}
【问题讨论】:
-
没错,资源不能序列化。您必须先从对象中删除任何资源,然后才能对其进行序列化。为此还请查看魔术方法
__sleep和__wakeup... -
我应该把它们放在哪里?控制器、工作、模型?
-
请阅读有关如何实现这些魔术方法的文档...它们属于您要序列化的对象。
-
Laravel 工作有一个
use SerializesModels特征,如果你打算序列化模型,所以如果你的工作没有,那么你应该把它写出来 -
@apokryfos 已经是这样了,我已经用工作抽象类编辑了我的问题