gpt4 book ai didi

PHP AMQP consumer does not respond after a while(PHP AMQP使用者过了一段时间没有响应)

转载 作者:bug小助手 更新时间:2023-10-22 17:34:54 32 4
gpt4 key购买 nike



I got a little problem here with a php amqp consumer, which quits working after a while. Below you can see my silex command. I also tried with heartbeat and keepalive configuration to handle broken network connections, but it does not change. What could be a reason that the consumer does not read the messages from the queue? The script does not quit, it just seems to be sleeping.

我在这里遇到了一个php-amqp消费者的小问题,它过了一段时间就停止工作了。下面你可以看到我的silex命令。我还尝试过使用心跳和保活配置来处理断开的网络连接,但它没有改变。消费者没有从队列中读取消息的原因是什么?剧本并没有停止,只是看起来在睡觉。



<?php

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Knp\Command\Command as BaseCommand;
use PhpAmqpLib\Message\AMQPMessage;

class RequestWorkerCommand extends BaseCommand
{
protected function configure()
{
$this->setName('queue:worker');
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$app = $this->getSilexApplication();
$amqp = $app['amqp.connection']; /* @var $amqp \PhpAmqpLib\Connection\AMQPStreamConnection */
$channel = $amqp->channel();

$callback = function($message) use ($input, $output) {
return call_user_func_array([$this, 'processMessage'], [$message, $input, $output]);
};

$channel->queue_declare('myqueue', false, true, false, false);
$channel->basic_qos(null, 1, null);
$channel->basic_consume('myqueue', '', false, false, false, false, $callback);

while(count($channel->callbacks)) {
$output->writeln('Waiting for incoming price requests');
$channel->wait();
}
}

protected function processMessage(AMQPMessage $message, InputInterface $input, OutputInterface $output)
{
$app = $this->getSilexApplication();

try {
$data = json_decode($message->body, true);
$request = Request::createFromArray($data); /* create object from data */
$message->delivery_info['channel']->basic_ack($message->delivery_info['delivery_tag']);
$app['distributor']->distribute($request); /* process message */
} catch (\Exception $e) { /* handle error */ }
}
}

更多回答
优秀答案推荐

I cannot be sure of PHP but I faced a similar problem with Python/kombu. the pure puython amqplib never did heartbeats, even though I gave it directives to do so. When I switched to using librabbitmq (which wraps around rabbitmq-c) as a replacement, heartbeats stopped being a problem and my consumers stopped hanging up on me.

我不能确定PHP,但我在Python/kombu中遇到了类似的问题。纯puython amqplib从未做过心跳,尽管我给了它这样做的指令。当我改用librabbitmq(它围绕着rabbitmq-c)作为替代时,心跳不再是一个问题,我的消费者也不再挂断我的电话。


更多回答

Sorry for the late answer. I tried to use the heartbeat with phpamqp-lib, but it does not work as expected. Now I try with the connection timeout, but this also has some sideeffects like zombie connections. I use supervisord for the processmanagement and it starts new processes if a worker dies. The connections are still alive on rabbitmq!?

很抱歉答复迟了。我尝试将heartbeat与phpamqp-lib一起使用,但它并没有按预期工作。现在我尝试使用连接超时,但这也有一些副作用,比如僵尸连接。我使用supervisord进行流程管理,如果工人死亡,它会启动新的流程。rabbitmq.上的连接仍然有效!?

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com