繁体   English   中英

在Laravel的工匠队列中捕获ProcessTimedOutException:listen

[英]catching ProcessTimedOutException in Laravel's artisan queue:listen

我们有一堆SQS工作,我们使用Laravel的php artisan queue:listen来监视和处理php artisan queue:listen

定期,有几分钟的短暂和SQS超时。 发生这种情况时, queue:listen使用如下消息进行中止:

[Symfony\Component\Process\Exception\ProcessTimedOutException]                                                                                             
  The process "php artisan queue:work
    --queue="https://sqs.us-west-2.amazonaws.com/*******/queue"
    --delay=0 --memory=128 --sleep=3 --tries=0 -env=production"
  exceeded the timeout of 60 seconds.

我已尝试在app/start/global.phpapp/start/artisan.php处理异常:

App::error(function(Symfony\Component\Process\Exception\ProcessTimedOutException $exception) {
    // do nothing
});

不幸的是,异常仍然发生,我的queue:listen仍然死亡。

如何捕获此异常并忽略它以进行重试?

如果它有帮助,我使用SupervisorD来运行crons并且我从队列中解雇:每秒工作一次,我对SQS没有任何问题。

您要么在异常处理程序中重新运行“php artisan queue:work ...”队列作业,

或者将/ vendor / symfony / process / Symfony / Component / Process / Process :: setTimeout()值增加到更高的值。

或者两者都有。

不是答案,但更像是解决方法:

每当我需要确保进程将运行时,即使发生错误,我也会永远使用npms 这个方便的工具一旦崩溃就会重新启动进程。

安装非常简单:

sudo apt-get install nodejs
sudo apt-get install npm
sudo ln -s /usr/bin/nodejs /usr/bin/node
sudo npm -g install forever

使用此命令启动进程: forever start -c php artisan queue:listen
如果你需要阻止它: forever stopall

超时将不时发生。 Symfony Process类有自己的超时设置,导致此特定超时(我相信)。 我不确定如果不编辑与工作者有关的核心Laravel代码就可以捕获这个,这不是一个好主意。

我强烈建议使用Laravel文档中显示的主管:

https://laravel.com/docs/5.1/queues#supervisor-configuration

这将自动重启您的侦听器,即使它失败。

问题似乎与队列作业运行时间有关,据我所知,默认运行时间为30秒,你应该增加这个值,你可以增加两种方式,

php artisan queue:listen --timeout=1200

或者您可以为特定作业设置$timeout

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

我希望这个能帮上忙

<?php

namespace App;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;


abstract class Job implements ShouldQueue, JobStatusInterface
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public $tries = 1; // try only once

    public function handle()
    {

    }
}

使用这个抽象类并将它分发给它解决你的问题的扩展类

<?php

namespace App;

use App\Job;
use Log;
use Exception;
/**
 * Class CheckStockJob
 */
 class CheckStockJob extends Job
 {
     public $tries = 100; // how many you need to retry
     public function handle()
     {
        try {
          // do something here
        }
        catch(Exception $e){
          Log::error($e);
          throw $e; // rethrow to make job fail
        }
     }
 }

你代码中的某个地方

$t = new CheckStockJob();
dispatch($t);

尝试通过执行以下方法调查命令的可用参数:

php artisan queue:listen --help

您可以以秒为单位传递超时值,尝试次数等。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM