Nova 队列操作触发多个作业
Nova Queued Actions triggering multiple jobs
我创建了这样一个动作:
use Illuminate\Bus\Queueable;
use Laravel\Nova\Actions\Action;
use Illuminate\Support\Collection;
use Laravel\Nova\Fields\ActionFields;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Laravel\Nova\Fields\BelongsTo;
use Illuminate\Support\Facades\Queue;
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: App\Nova\Actions\ExportToCsv
int(1588452305)
int(621525)
[2020-05-02 20:45:05][180] Processing: App\Nova\Actions\ExportToCsv
int(1588452305)
int(412186)
[2020-05-02 20:45:05][179] Processing: App\Nova\Actions\ExportToCsv
int(1588452305)
int(621282)
[2020-05-02 20:45:08][181] Processed: App\Nova\Actions\ExportToCsv
[2020-05-02 20:45:08][184] Processing: App\Nova\Actions\ExportToCsv
int(1588452308)
int(623886)
[2020-05-02 20:45:08][180] Processed: App\Nova\Actions\ExportToCsv
[2020-05-02 20:45:08][182] Processing: App\Nova\Actions\ExportToCsv
int(1588452308)
int(622950)
[2020-05-02 20:45:08][179] Processed: App\Nova\Actions\ExportToCsv
[2020-05-02 20:45:08][183] Processing: App\Nova\Actions\ExportToCsv
int(1588452308)
int(623252)
[2020-05-02 20:45:11][184] Processed: App\Nova\Actions\ExportToCsv
[2020-05-02 20:45:11][182] Processed: App\Nova\Actions\ExportToCsv
[2020-05-02 20:45:11][183] Processed: App\Nova\Actions\ExportToCsv
[2020-05-02 20:45:35][185] Processing: App\Nova\Actions\ExportToCsv
int(1588452335)
int(621282)
[2020-05-02 20:45:35][187] Processing: App\Nova\Actions\ExportToCsv
int(1588452335)
int(621525)
[2020-05-02 20:45:35][186] Processing: App\Nova\Actions\ExportToCsv
int(1588452335)
int(412186)
[2020-05-02 20:45:38][185] Processed: App\Nova\Actions\ExportToCsv
[2020-05-02 20:45:38][188] Processing: App\Nova\Actions\ExportToCsv
int(1588452338)
int(622950)
[2020-05-02 20:45:38][187] Processed: App\Nova\Actions\ExportToCsv
[2020-05-02 20:45:38][190] Processing: App\Nova\Actions\ExportToCsv
int(1588452338)
int(623886)
[2020-05-02 20:45:38][186] Processed: App\Nova\Actions\ExportToCsv
[2020-05-02 20:45:38][189] Processing: App\Nova\Actions\ExportToCsv
int(1588452338)
int(623252)
[2020-05-02 20:45:41][188] Processed: App\Nova\Actions\ExportToCsv
[2020-05-02 20:45:41][190] Processed: App\Nova\Actions\ExportToCsv
[2020-05-02 20:45:41][189] Processed: App\Nova\Actions\ExportToCsv
默认情况下,批次按 200 个项目分组,据我所见,同时生成了 3 个作业。不幸的是,无法知道将首先处理哪个作业,因此每个批次更改的模型都会显示在日志中,如输出所示:621525, 412186, 621282
表示第一次调用操作时,621282, 621525, 412186
表示调用操作时第二次(模型以相同的方式在作业之间拆分,但作业处理的顺序发生了变化)。这里的问题是,如果我将视图保存到 CSV 文件,顺序很重要。
我希望在给定时间针对给定操作只产生一个作业,这将解决我的问题。这可能吗?
使用 horizon 作为队列工作者
完成此操作的方法是更改 horizon 配置中的 processes
值:
'environments' => [
'local' => [
'supervisor-1' => [
'connection' => 'redis',
'queue' => ['default'],
'balance' => 'simple',
'processes' => 1,
'tries' => 3,
],
],
我创建了这样一个动作:
use Illuminate\Bus\Queueable;
use Laravel\Nova\Actions\Action;
use Illuminate\Support\Collection;
use Laravel\Nova\Fields\ActionFields;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Laravel\Nova\Fields\BelongsTo;
use Illuminate\Support\Facades\Queue;
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: App\Nova\Actions\ExportToCsv
int(1588452305)
int(621525)
[2020-05-02 20:45:05][180] Processing: App\Nova\Actions\ExportToCsv
int(1588452305)
int(412186)
[2020-05-02 20:45:05][179] Processing: App\Nova\Actions\ExportToCsv
int(1588452305)
int(621282)
[2020-05-02 20:45:08][181] Processed: App\Nova\Actions\ExportToCsv
[2020-05-02 20:45:08][184] Processing: App\Nova\Actions\ExportToCsv
int(1588452308)
int(623886)
[2020-05-02 20:45:08][180] Processed: App\Nova\Actions\ExportToCsv
[2020-05-02 20:45:08][182] Processing: App\Nova\Actions\ExportToCsv
int(1588452308)
int(622950)
[2020-05-02 20:45:08][179] Processed: App\Nova\Actions\ExportToCsv
[2020-05-02 20:45:08][183] Processing: App\Nova\Actions\ExportToCsv
int(1588452308)
int(623252)
[2020-05-02 20:45:11][184] Processed: App\Nova\Actions\ExportToCsv
[2020-05-02 20:45:11][182] Processed: App\Nova\Actions\ExportToCsv
[2020-05-02 20:45:11][183] Processed: App\Nova\Actions\ExportToCsv
[2020-05-02 20:45:35][185] Processing: App\Nova\Actions\ExportToCsv
int(1588452335)
int(621282)
[2020-05-02 20:45:35][187] Processing: App\Nova\Actions\ExportToCsv
int(1588452335)
int(621525)
[2020-05-02 20:45:35][186] Processing: App\Nova\Actions\ExportToCsv
int(1588452335)
int(412186)
[2020-05-02 20:45:38][185] Processed: App\Nova\Actions\ExportToCsv
[2020-05-02 20:45:38][188] Processing: App\Nova\Actions\ExportToCsv
int(1588452338)
int(622950)
[2020-05-02 20:45:38][187] Processed: App\Nova\Actions\ExportToCsv
[2020-05-02 20:45:38][190] Processing: App\Nova\Actions\ExportToCsv
int(1588452338)
int(623886)
[2020-05-02 20:45:38][186] Processed: App\Nova\Actions\ExportToCsv
[2020-05-02 20:45:38][189] Processing: App\Nova\Actions\ExportToCsv
int(1588452338)
int(623252)
[2020-05-02 20:45:41][188] Processed: App\Nova\Actions\ExportToCsv
[2020-05-02 20:45:41][190] Processed: App\Nova\Actions\ExportToCsv
[2020-05-02 20:45:41][189] Processed: App\Nova\Actions\ExportToCsv
默认情况下,批次按 200 个项目分组,据我所见,同时生成了 3 个作业。不幸的是,无法知道将首先处理哪个作业,因此每个批次更改的模型都会显示在日志中,如输出所示:621525, 412186, 621282
表示第一次调用操作时,621282, 621525, 412186
表示调用操作时第二次(模型以相同的方式在作业之间拆分,但作业处理的顺序发生了变化)。这里的问题是,如果我将视图保存到 CSV 文件,顺序很重要。
我希望在给定时间针对给定操作只产生一个作业,这将解决我的问题。这可能吗?
使用 horizon 作为队列工作者
完成此操作的方法是更改 horizon 配置中的 processes
值:
'environments' => [
'local' => [
'supervisor-1' => [
'connection' => 'redis',
'queue' => ['default'],
'balance' => 'simple',
'processes' => 1,
'tries' => 3,
],
],