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

Colleen Mirabello colleen at surfmerchants.com
Thu May 23 21:45:17 BST 2013


(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


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.rabbitmq.com/pipermail/rabbitmq-discuss/attachments/20130523/12f9abd0/attachment.htm>


More information about the rabbitmq-discuss mailing list