[rabbitmq-discuss] Graceful shutdown of blocking consumers in PHP?

Alvaro Videla videlalvaro at gmail.com
Thu May 23 22:41:28 BST 2013


Hi,

Using that library, can you consume from more than one queue? If yes, then
you could send control messages to the script and make it stop.

With php-amqplib since the while loop is part of user land code, you can
control all the consuming parts on your own so this wouldn't be a problem.

Regards,

Alvaro


On Thu, May 23, 2013 at 10:45 PM, Colleen Mirabello <
colleen at surfmerchants.com> wrote:

>  (PHP 5.33, AMQP protocol 0-9-1, librabbitmq 0.0.1, phpamqp 1.0.9)
>
> I'm having trouble figuring out how to gracefully shut down a blocking PHP
> consumer.
>
> The context of this problem is that we would like to create a script that
> forks many consumer children based on a configuration file. That script
> will then monitor its children and restart them should any fail. We would
> also like to be able to kill this parent script and have it stop and kill
> its children and then exit.
>
> Our preference is to use consume() rather than get() if possible.
>
> The problem we're having is how to get a signal to the child processes to
> get them to stop consuming.
>
> A very simple example script (with connection info removed):
>
> <?php
> // so we can catch system calls...
> declare(ticks=1);
>
> pcntl_signal(SIGTERM, array('RunControl', 'sysCalls'));
> pcntl_signal(SIGHUP, array('RunControl', 'sysCalls'));
> pcntl_signal(SIGUSR1, array('RunControl', 'sysCalls'));
>
>
> class RunControl
> {
>     static $alive = true;
>
>     // catch system calls
>     function sysCalls($signal)
>     {
>         print "Got signal to die\n";
>     }
> }
>
> function consume($message, $queue)
> {
>     print "Got a message!\n";
>     $queue->ack($message->getDeliveryTag());
> }
>
>
> $connection = new AMQPConnection($connectionInfo);
> $connection->connect();
>
> $channel = new AMQPChannel($connection);
> $queue = new AMQPQueue($channel);
>
> $queue->setName('my_queue');
>
> $queue->consume('consume');
> ?>
>
> If I run this script, the consumer will happily consume messages. However,
> executing a kill command on its pid does not cause the "Got signal to die"
> message to print.
>
> I have tried using $queue->cancel() in many different configurations
> (within the same script, in another script, with a consumer tag, without),
> but it doesn't seem to do anything.
>
> I tried the workaround described here:
> http://blog.andrewrose.co.uk/2008/02/php-getting-signals-through-to-blocking.html
> and it resulted in a whole bunch of "interrupted system call" exceptions.
>
> Does anyone have any suggestions for how I can get my consumer to exit
> gracefully? Thanks.
>
>     - Colleen
>
>
>
> _______________________________________________
> rabbitmq-discuss mailing list
> rabbitmq-discuss at lists.rabbitmq.com
> https://lists.rabbitmq.com/cgi-bin/mailman/listinfo/rabbitmq-discuss
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.rabbitmq.com/pipermail/rabbitmq-discuss/attachments/20130523/5aad3dd8/attachment.htm>


More information about the rabbitmq-discuss mailing list