【问题标题】:Max Attempts Exceeded Exception queue laravel最大尝试次数超过异常队列 laravel
【发布时间】:2020-11-05 08:35:07
【问题描述】:

我创建了一个应用程序来向多个用户发送电子邮件,但在处理大量收件人时遇到了问题。

错误出现在failed_jobs 表中

Illuminate\Queue\MaxAttemptsExceededException: App\Jobs\ESender has been attempted too many times or run too long. The job may have previously timed out. in D:\EmailSender\vendor\laravel\framework\src\Illuminate\Queue\Worker.php:649

这是payloadfailed_jobs 表中

{"uuid":"ff988083-c1da-4d20-a2e3-c2a10e154c79","timeout":9000,"id":"j2Lz0Ro0bkJpqwxKWTxC3Tiii71iE6Cm","data":{"command":"O:16:\"App\\Jobs\\ESender\":13:{s:7:\"timeout\";i:9000;s:12:\"receiver_obj\";O:45:\"Illuminate\\Contracts\\Database\\ModelIdentifier\":4:{s:5:\"class\";s:12:\"App\\Receiver\";s:2:\"id\";i:6;s:9:\"relations\";a:0:{}s:10:\"connection\";s:5:\"mysql\";}s:16:\"sender_all_hosts\";O:45:\"Illuminate\\Contracts\\Database\\ModelIdentifier\":4:{s:5:\"class\";s:15:\"App\\SenderHosts\";s:2:\"id\";a:4:{i:0;i:1;i:1;i:2;i:2;i:3;i:3;i:4;}s:9:\"relations\";a:0:{}s:10:\"connection\";s:5:\"mysql\";}s:11:\"message_obj\";O:45:\"Illuminate\\Contracts\\Database\\ModelIdentifier\":4:{s:5:\"class\";s:12:\"App\\Messages\";s:2:\"id\";i:36;s:9:\"relations\";a:0:{}s:10:\"connection\";s:5:\"mysql\";}s:7:\"counter\";i:1;s:3:\"job\";N;s:10:\"connection\";N;s:5:\"queue\";N;s:15:\"chainConnection\";N;s:10:\"chainQueue\";N;s:5:\"delay\";N;s:10:\"middleware\";a:0:{}s:7:\"chained\";a:0:{}}","commandName":"App\\Jobs\\ESender"},"displayName":"App\\Jobs\\ESender","timeoutAt":1594841911,"maxExceptions":null,"maxTries":null,"job":"Illuminate\\Queue\\CallQueuedHandler@call","delay":null,"attempts":1}

see the cmd error here.

部分代码:

#1

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


    /**
     * The number of times the job may be attempted.
     *
     * @var int
     */
    public $tries = 100;

    /**
     * The number of seconds the job can run before timing out.
     *
     * @var int
     */
    public $timeout = 9999999;

     ...more code...
}

#2

public function handle(){
    Redis::throttle('key')->allow(1)->every(20)->then(function () {
         //send email
           ..... more code .....

        }, function () {
            // Could not obtain lock...
            return $this->release(10);
        });
    }

这是我的配置:

queue.php:

'redis' => [
            'driver' => 'redis',
            'connection' => 'default',
            'queue' => env('REDIS_QUEUE', 'default'),
            'retry_after' => 9000,
            'block_for' => null,
        ],

.env

BROADCAST_DRIVER=log
CACHE_DRIVER=file
QUEUE_CONNECTION=database
SESSION_DRIVER=file
SESSION_LIFETIME=300
REDIS_CLIENT = predis
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
QUEUE_DRIVER=database

【问题讨论】:

  • 您能否添加一个队列试图调用的命令。您的脚本出现问题,我们需要了解更多信息来帮助您。
  • @VladVladimirHercules 它在队列数量(起初是)的情况下工作正常。但随后出现了这些错误。您可以查看上面的图片。
  • 我更新的是在你的 failed_jobs 表中如何调用命令。您发送给我们的是一个错误,我在实际命令调用之后。此外,该错误是否包含任何其他信息?
  • 超时和尝试是为单个作业设置的,因此当您的作业排队时,根据您的代码,该作业有 100 次尝试执行。
  • @VladVladimirHercules 该问题已在有效负载部分更新,请检查

标签: php laravel redis queue predis


【解决方案1】:

您在作业中设置了timeout,但此超时大于您在此配置中定义的retry_after 中的值。

https://laravel.com/docs/7.x/queues#job-expirations-and-timeouts

有明确的警告:

--timeout 值应始终比您的 retry_after 配置值至少短几秒钟。这将确保处理给定作业的工作人员总是在重试作业之前被杀死。如果您的 --timeout 选项比您的 retry_after 配置值长,您的作业可能会被处理两次。

您可以为长时间运行的作业定义一个新连接,并在作业上设置此连接(调度到特定连接),而不是使用timeout

【讨论】:

  • 不幸的是,我使 retry_after 值大于超时值。但是出现了同样的问题。
  • @FadiSharif 你是如何解决这个问题的?
【解决方案2】:

运行队列工作者的命令需要 --tries= 和 --timeout= 来设置队列工作者允许的输出限制。

这确保您的命令不会超出您定义的工作人员的限制。

您可以使用作业属性来实现超时或尝试,如下所示。 并使用队列配置文件设置默认值。

【讨论】:

  • 不幸的是我做了这个但是没有用,你可以注意到我是在 ESender 类里面做的
  • 可能不是您的解决方案,但您的 .env 文件说连接数据库,但您发布 queue.php 专注于 redis?...您实际使用的是什么..您是否尝试切换?您对这两个驱动程序有同样的问题吗?
  • 您是否知道您的油门/释放也会增加尝试次数...请参阅github.com/laravel/ideas/issues/1381 没有多大意义,但确实会增加尝试次数..
  • 为了测试,我更改了 QUEUE_CONNECTION 和 QUEUE_DRIVER,但我已经在研究 redis。总体来说,双方的手术都没有成功
【解决方案3】:

运行

php artisan config:clear
php artisan optimization:clear

重启管理员

【讨论】:

  • “优化”命名空间中没有定义命令。
  • php 工匠优化:清除
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-22
  • 2018-06-21
  • 2022-08-11
  • 1970-01-01
  • 2021-10-03
  • 1970-01-01
相关资源
最近更新 更多