我为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
}
}
一切正常。问题是,有时我预计会超时(例如,我试图获取的网站有一个重定向循环),但队列一直在运行。由于它没有停止,其余的队列都被阻塞了。
我试过了:
添加
-d max_execution_time=30
无效。在队列上设置
max_execution_time
,无效。在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
有什么想法吗?
这似乎更像是脚本没有使用HTTP客户端的情况,该客户端可以在底层库(可能是libCurl)中设置适当的超时,而不是顶层系统。
PHP代码调用一些web客户端,该客户端可能正在调用一个基于C的库,该库打开了一个失败的网络连接。
如果与Beanstalkd的连接仍然打开,则可能是TTR失败,因为这与PHP实例和代码是分开的,但如果脚本被卡住,则对您没有帮助。
您可能需要一个更好的HTTP客户端,它可以为自己生成一个超时,以捕获、删除或隐藏作业。
同时,如果有一个URL失败了-不要对此做任何事情,如果你看到它,就删除它。