我创建了这样一个操作:
use IlluminateBusQueueable;
use LaravelNovaActionsAction;
use IlluminateSupportCollection;
use LaravelNovaFieldsActionFields;
use IlluminateQueueInteractsWithQueue;
use IlluminateContractsQueueShouldQueue;
use LaravelNovaFieldsBelongsTo;
use IlluminateSupportFacadesQueue;
class ExportToCsv extends Action implements ShouldQueue
{
use InteractsWithQueue, Queueable;
public function handle(ActionFields $fields, Collection $models)
{
var_dump(time(), collect($models)->map(function($item){
return $item->id;
})->first());
sleep(3);
}
}
然后我触发了1128个项目(两次(,并观察到这一结果:
[2020-05-02 20:45:05][181] Processing: AppNovaActionsExportToCsv
int(1588452305)
int(621525)
[2020-05-02 20:45:05][180] Processing: AppNovaActionsExportToCsv
int(1588452305)
int(412186)
[2020-05-02 20:45:05][179] Processing: AppNovaActionsExportToCsv
int(1588452305)
int(621282)
[2020-05-02 20:45:08][181] Processed: AppNovaActionsExportToCsv
[2020-05-02 20:45:08][184] Processing: AppNovaActionsExportToCsv
int(1588452308)
int(623886)
[2020-05-02 20:45:08][180] Processed: AppNovaActionsExportToCsv
[2020-05-02 20:45:08][182] Processing: AppNovaActionsExportToCsv
int(1588452308)
int(622950)
[2020-05-02 20:45:08][179] Processed: AppNovaActionsExportToCsv
[2020-05-02 20:45:08][183] Processing: AppNovaActionsExportToCsv
int(1588452308)
int(623252)
[2020-05-02 20:45:11][184] Processed: AppNovaActionsExportToCsv
[2020-05-02 20:45:11][182] Processed: AppNovaActionsExportToCsv
[2020-05-02 20:45:11][183] Processed: AppNovaActionsExportToCsv
[2020-05-02 20:45:35][185] Processing: AppNovaActionsExportToCsv
int(1588452335)
int(621282)
[2020-05-02 20:45:35][187] Processing: AppNovaActionsExportToCsv
int(1588452335)
int(621525)
[2020-05-02 20:45:35][186] Processing: AppNovaActionsExportToCsv
int(1588452335)
int(412186)
[2020-05-02 20:45:38][185] Processed: AppNovaActionsExportToCsv
[2020-05-02 20:45:38][188] Processing: AppNovaActionsExportToCsv
int(1588452338)
int(622950)
[2020-05-02 20:45:38][187] Processed: AppNovaActionsExportToCsv
[2020-05-02 20:45:38][190] Processing: AppNovaActionsExportToCsv
int(1588452338)
int(623886)
[2020-05-02 20:45:38][186] Processed: AppNovaActionsExportToCsv
[2020-05-02 20:45:38][189] Processing: AppNovaActionsExportToCsv
int(1588452338)
int(623252)
[2020-05-02 20:45:41][188] Processed: AppNovaActionsExportToCsv
[2020-05-02 20:45:41][190] Processed: AppNovaActionsExportToCsv
[2020-05-02 20:45:41][189] Processed: AppNovaActionsExportToCsv
默认情况下,批次按200个项目分组,据我所见,同时产生了3个作业。不幸的是,无法知道将首先处理哪个作业,因此每次批处理的日志中显示的模型都会发生变化,如输出所示:第一次调用操作时为621525, 412186, 621282
,第二次调用时为621282, 621525, 412186
(模型以相同的方式在作业之间划分,但处理的作业顺序会发生变化(。这里的问题是,如果我将视图保存到CSV文件,那么顺序很重要。
我只希望在特定的时间、特定的行动中产生一份工作,这将解决我的问题。这可能吗?
使用地平线作为队列工作者
实现这一点的方法是更改地平线配置中的processes
值:
'environments' => [
'local' => [
'supervisor-1' => [
'connection' => 'redis',
'queue' => ['default'],
'balance' => 'simple',
'processes' => 1,
'tries' => 3,
],
],