首页 > 解决方案 > 作业尝试次数过多或运行时间过长

问题描述

我对 Laravel 5.7 中的作业调度有疑问。我需要在工作中提取客户的数据,但是每次我在产品服务器上运行它时,我都会得到

[2020-01-02 13:02:02] production.INFO: Job started  
[2020-01-02 13:02:02] production.INFO: ===================  
[2020-01-02 13:02:02] production.INFO: Contest: name
[2020-01-02 13:02:02] production.INFO: ===================  
[2020-01-02 13:12:08] production.INFO: Job started  
[2020-01-02 13:12:08] production.INFO: ===================  
[2020-01-02 13:12:08] production.INFO: Contest: name
[2020-01-02 13:12:08] production.INFO: ===================  
[2020-01-02 13:22:15] production.INFO: Job started  
[2020-01-02 13:22:15] production.INFO: ===================  
[2020-01-02 13:22:15] production.INFO: Contest: name
[2020-01-02 13:22:15] production.INFO: ===================  
[2020-01-02 13:32:21] production.INFO: Job started  
[2020-01-02 13:32:21] production.INFO: ===================  
[2020-01-02 13:32:21] production.INFO: Contest: name
[2020-01-02 13:32:21] production.INFO: ===================  
[2020-01-02 13:42:28] production.INFO: Job started  
[2020-01-02 13:42:28] production.INFO: ===================  
[2020-01-02 13:42:28] production.INFO: Contest: name 
[2020-01-02 13:42:28] production.INFO: ===================  
[2020-01-02 13:44:33] production.ERROR: App\Jobs\SendContestMailToFans has been attempted too many times or run too long. The job may have previously timed out. {"exception":"[object] (Illuminate\\Queue\\MaxAttemptsExceededException(code: 0): App\\Jobs\\SendContestMailToFans has been attempted too many times or run too long. The job may have previously timed out. at /home/forge/loycals.com/vendor/laravel/framework/src/Illuminate/Queue/Worker.php:401)
[stacktrace]

这是一个工作主体。正如我从日志中看到的那样,工作在拉动收件人时停止。

private function sendContestEmails()
{
    Log::info('Job started');

    $contest = $this->contest->load([
        'prizes',
        'primary_sponsor',
        'secondary_sponsors',
    ]);

    Log::info('===================');
    Log::info('Contest: ' . $contest->title);
    Log::info('===================');

    $recipients = $contest->primary_sponsor->first()->subscribers()->whereDate('email_sent_at', '<=', Carbon::now()->subWeek()->format('Y-m-d H:i:s'))->orWhereNull('email_sent_at')->get(); // list of subscribers (which is a unique list of fans for that sponsor)

    $count = 1;
    foreach($recipients as $recipient) {
        $count++;
    }
    Log::info('===================');
    Log::info('Recipients: ' . $count);
    Log::info('===================');


    $sponsor = $contest->primary_sponsor->first();
    $subject = 'New Contest at ' . $sponsor->title; // venue title

    Log::info('===================');
    Log::info('Sponsor: ' . $sponsor->title);
    Log::info('===================');

    Log::info('Job finished');
}

这是 Forge 上的工人设置: https ://imgur.com/eX6A78V

我还在 config/queue.php 中将“retry_after”设置为 600。我尝试使用超时 0 和默认的“retry_after”运行作业,但遇到了同样的错误。我尝试在登台服务器上使用相同的配置和数据库中的虚拟数据执行此操作,并且该作业已在几秒钟内完成。

标签: phplaravelqueuelaravel-5.7laravel-queue

解决方案


更改 config/queue.php 中的“retry_after”选项。您应该将“retry_after”值设置为您的作业应该合理地完成处理的最大秒数。

'retry_after' => 1080

推荐阅读