【问题标题】:How to set timeouts for queues properly?如何正确设置队列超时?
【发布时间】:2015-03-03 13:32:59
【问题描述】:

我为 Beanstalkd 队列编写了我的消费者,使用 Supervisord 运行它。

php5.5-sp %appdir%/worker

worker 是一个 PHP 文件,以 1 秒的睡眠时间循环。比如:

#!/usr/bin/env php
<?php

    while(true)
    {
         echo time() . PHP_EOL;
         exec("php5.5-sp -d max_execution_time=30 job");
         sleep(1);
    }

job(文件)使用 Beanstalkd,弹出一个作业,尝试处理它。

 // job
 require __DIR__.'/vendor/autoload.php';
 # Initialize beanstalkd
 $beanstalkd = new Illuminate\Queue\Worker($queue->getQueueManager(), null, null);
 try {
     $beanstalkd ->pop('default', 'default', 2, 8192);
 } catch (Exception $e) {
     Log::critical($e->getMessage());
 }

queue 是这样的:

<?php namespace Aristona\Queue;

class ArticleParserService
{

     public function fire($job, Array $data)
     {
          // Do some time taking stuff
     }
}

一切正常。问题是,有时我希望超时(例如,我尝试获取的站点有一个重定向循环)但队列只是一直运行下去。由于它没有停止,其余的队列都被阻塞了。

我试过了:

  1. 添加-d max_execution_time=30,无效。

  2. 在队列上设置max_execution_time,无效。

  3. 在 Beanstalkd 配置上将 ttr 设置为 30 秒,无效。

我不知道我还应该做什么?

日志是这样的:

sudo supervisord > tail -f queue
Worker is looping on [production] at 1425389013...
Worker is looping on [production] at 1425389016...
Worker is looping on [production] at 1425389017...
Worker is looping on [production] at 1425389019...
Worker is looping on [production] at 1425389023...
Worker is looping on [production] at 1425389027... (stuck here forever)

vi debug.log
DEBUG - 2015-03-03 08:23:59 :: There is no image in the feed. Attempting to guess it.
DEBUG - 2015-03-03 08:23:59 :: Guessing image from URL: https://www.aksent-tercume.com/di%c4%9fer-diller/212-dilde-seni-seviyorum-de/
DEBUG - 2015-03-03 08:24:01 :: Guessed an appropriate image!
DEBUG - 2015-03-03 08:24:01 :: Validating image size...

带有“正在验证图像大小...”的部分,一切都永远停止,并且始终位于同一个 URL 中。 (https://www.aksent-tercume.com/di%c4%9fer-diller/212-dilde-seni-seviyorum-de/)

脚本所做的只是连接到一个 URL,为文章找到最合适的图像,然后检查它的大小以确保它大于 60x60 像素。但由于某种原因,它只是停在那里,永远阻塞了我们的队列。我无法让它超时。只有以下使队列恢复正常。

sudo service beanstalkd restart
sudo supervisorctl > restart all

有什么想法吗?

【问题讨论】:

    标签: php multithreading queue beanstalkd


    【解决方案1】:

    这似乎更像是脚本不使用 HTTP 客户端的情况,该客户端可以在底层库(可能是 libCurl)中设置适当的超时,而不是顶级系统。

    PHP 代码调用了一些 Web 客户端,该客户端可能正在调用基于 C 的库,该库打开了失败的网络连接。

    如果与 Beanstalkd 的连接仍然打开,则可能是 TTR 失败,因为它与 PHP 实例和代码是分开的,但如果脚本被卡住,它对您没有帮助。

    您可能需要一个更好的 HTTP 客户端,它可以为自己生成一个超时,以捕获该超时,然后删除或隐藏该作业。

    与此同时,如果有一个 URL 失败 - 只是不要对它做任何事情,如果你看到它就删除它。

    【讨论】:

      猜你喜欢
      • 2010-12-24
      • 1970-01-01
      • 1970-01-01
      • 2016-09-13
      • 2021-02-09
      • 2020-05-30
      • 1970-01-01
      • 2012-09-23
      • 2014-12-16
      相关资源
      最近更新 更多