我正在使用 Laravel 8 和 Horizon 来异步跟踪付款。作业实现如下所示:
<?php
namespace App\Jobs;
// use ...
class TrackOrderPayment implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
const RETRY_INTERVAL_SECONDS = 60;
public function handle()
{
// Anticipate errors
try {
// implementation ...
} catch (Throwable $exception) {
// Re-queue the job
$this->reQueueJob();
}
}
/**
* Re-queue the job with a delay to try again later
*/
private function reQueueJob(): void
{
$this->delete();
if (this->order->status === Constants::ORDER_STATUS_AWAITING_PAYMENT) {
dispatch(new TrackOrderPayment($this->order))->delay(self::RETRY_INTERVAL_SECONDS);
}
}
}
如果未检测到付款,我将等待最多 15 分钟来检测付款 - 一个单独的 cron 作业将订单更新为不同的状态,因此该作业在 15 分钟(或 15 次尝试,因为重新-queue 设置为 60 秒)
在我config/queue.php
的 redis 队列连接配置中设置如下:
'redis' => [
'driver' => 'redis',
'connection' => 'default',
'queue' => env('REDIS_QUEUE', 'default'),
'retry_after' => 960,
'block_for' => null,
],
我在这里看不到任何其他配置或config/horizon.php
关于最大尝试/超时。
我试图深入了解这一点,为什么某些工作会因此错误而失败:
App\Jobs\TrackOrderPayment 尝试次数过多或运行时间过长。该作业之前可能已超时。
关于这个主题的所有相关问题都说提出retry_after
并且我已经这样做了,它似乎没有帮助。
每次作业运行时,作业的实际“处理”时间小于 0.5 秒,如果未检测到付款,则会延迟 60 秒返回队列。
我在这里想念什么?