Daniel
Daniel

Reputation: 539

PHP AMQP consumer does not respond after a while

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

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 */ }
    }
}

Upvotes: 1

Views: 1097

Answers (1)

pbhowmick
pbhowmick

Reputation: 1113

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.

Upvotes: 0

Related Questions