【问题标题】:Laravel job has been attempted too many times or run too long. The job may have previously timed outLaravel 作业已尝试太多次或运行时间过长。该作业之前可能已超时
【发布时间】:2021-09-13 21:43:43
【问题描述】:

我正在使用 Laravel 8 和 Horizo​​n 来异步跟踪付款。作业实现如下所示:

<?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 次尝试,因为重新排队之间的延迟设置为 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,我已经这样做了,它似乎没有帮助。

每次作业运行时,作业的实际“处理”时间

我在这里错过了什么?

【问题讨论】:

    标签: php laravel laravel-8 laravel-horizon


    【解决方案1】:

    这是我config/horizon.php 中的一段配置,展示了如何实现超时:

    'my-supervisor-process' => [
        'connection' => 'redis',
        'queue' => ['default'],
        'balance' => 'simple',
        'processes' => 1,
        'tries' => 1,
        'timeout' => 120,
    ],
    

    上面的内容可能没有写在文档here 中,但它确实有效。

    我一直在自己挖掘源代码以自己找到这些参数,无论如何我相信上面的配置与以下命令直接相关,您可以看到所有类似的选项(在Laravel\Horizon\SupervisorOptions类中也可见):

    php artisan horizon:supervisor --help
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-04
      • 1970-01-01
      • 2021-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多