gpt4 book ai didi

Laravel 在完成 Job 后调度一个 Job 占用内存然后失败

转载 作者:行者123 更新时间:2023-12-05 06:21:10 32 4
gpt4 key购买 nike

我想做的是分派(dispatch)一个 Job,然后在前一个 Job 完成后继续分派(dispatch)同一个 Job,这样就可以连续循环分派(dispatch) Job。如选项一所示,这以前是与数据库队列驱动程序一起工作的。

我现在使用 Redis,因此我可以更轻松地通过 Horizo​​n 监控我的工作。

为了开始第一项工作,我使用自定义 artisan 命令,但这也可以来自 Controller ,这只是第一次调度。

我的 config/horizon ( local 配置与 production 相同):

'production' => [
'supervisor-1' => [
'connection' => 'redis',
'queue' => ['arbitrage'],
'balance' => 'auto',
'processes' => 2,
'tries' => 1,
],
'supervisor-2' => [
'connection' => 'redis',
'queue' => ['trade'],
'balance' => 'auto',
'processes' => 4,
'tries' => 1,
],
'supervisor-3' => [
'connection' => 'redis',
'queue' => ['balance', 'trade_meta'],
'balance' => 'auto',
'processes' => 5,
'tries' => 1,
],
'supervisor-4' => [
'connection' => 'redis',
'queue' => ['notifications'],
'balance' => 'auto',
'processes' => 2,
'tries' => 1,
],
],

选项 1:在作业结束时派发新作业

在这份工作的结尾 handle()我重新调度它,以便它连续运行。

在使用数据库驱动程序时,这实际上运行良好(该过程连续运行了数周)。

工作:

class ArbitrageJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

public $tries = 1;
public $timeout = 30;

// Process things here

ArbitrageJob::dispatch()->onQueue('arbitrage');
}

选项 2:使用 Queue:after 分派(dispatch)新作业

工作:

class ArbitrageJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

public $tries = 1;
public $timeout = 30;
}

然后在AppServiceProvider中:

Queue::after(function (JobProcessed $event) {
ProcessArbitrage::dispatch()->onQueue('arbitrage');
});

我现在看到的情况是,对于这两个选项,每次运行时都会占用内存(我通过 memory_get_usage(true) 记录了这一点)。

第一次运行它是 28MB,下一次是 32MB,它一直在增加直到大约 122MB 然后我得到以下错误:Illuminate\Queue\MaxAttemptsExceededException: App\Jobs\ArbitrageJob has been attempted too many times or run too long. The job may have previously timed out.

可能相关:在config/horizon我设置了memory_limit到 768MB,但看起来它失败的时间比这早很多。还是因为它将内存限制为每个工作人员 128MB?

最佳答案

你把工作放在彼此身上,所以所有的工作都存在于彼此之后,所以很明显你的内存应该积累起来。

在使用 php artisan queue:work 时应该使用开关。我建议测试它:

php artisan queue:work --once try other switches too that can work just last works.

关于Laravel 在完成 Job 后调度一个 Job 占用内存然后失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60005374/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com