Re: AMQP 1.0.0: AMQPExchangeException when receiving signal
| From: | jpauli | Date: | Wed, 22 Feb 2012 13:25:48 +0000 |
| Subject: | Re: AMQP 1.0.0: AMQPExchangeException when receiving signal | ||
| References: | 1 2 3 | Groups: | php.pecl.dev |
| Request: | Send a blank email to pecl-dev+get-9168@lists.php.net to get a copy of this message | ||
Yep, unlike Linux signal handlers (mainly), pcntl ones are sync (!= async)
, so they wont break your program dispatch flow unless you kinda poll()
them with pcntl_signal_dispatch(). If you forget it , you can wait forever
:)
This is because of the Zend Engine running in C in the background
Julien.Pauli
On Tue, Feb 21, 2012 at 8:06 PM, Pierre Mariani <pierre.mariani@gmail.com>wrote:
> Your response prompted me to review my test code, and I realized that I
> made a mistake in the way I was using pcntl_signal_dispatch.
> The code below behaves as expected.
>
> Thank you for your response, and sorry about the PIBCAK problem.
>
> <?php
> echo "pid: ".getmypid().PHP_EOL;
> global $run;
> $run = true;
> function sig_handler($sig) {
> global $run;
> echo "SIG".PHP_EOL;
> $run = false;
> }
> pcntl_signal(SIGUSR1, "sig_handler");
> $con = new AMQPConnection();
> $con->connect();
> $channel = new AMQPChannel($con);
> $ex = new AMQPExchange($channel);
> $ex->setName("exchange1");
> $ex->setType(AMQP_EX_TYPE_FANOUT);
> $ex->declare();
> $q = new AMQPQueue($channel);
> $q->setName("queue-test");
> $q->declare();
> $q->bind('exchange1', 'routing.key');
> $msg = "test";
> try {
> while($run) {
> $ex->publish($msg, 'routing.key', 0,
> array('delivery_mode'
> => 2));
> pcntl_signal_dispatch();
> }
> }
> catch (Exception $e) {
> var_dump($e);
> }
> echo "about to disconnect".PHP_EOL;
> $con->disconnect();
> echo "done".PHP_EOL;
> ?>
>
>
> On Tue, Feb 21, 2012 at 9:49 AM, Pieter de Zwart <
> pdezwart@rubiconproject.com> wrote:
>
> > Have you tried this same code with something other than publishing
> > messages to an AMQP broker? I don¹t know either if this is a normal
> > behavior or not, but I would assume that if some other bare PHP code has
> > the same execution chain, then it is normal =)
> >
> > Pieter
> >
> >
> >
> >
> >
> > On 2/17/12 2:26 PM, "Pierre Mariani" <pierre.mariani@gmail.com> wrote:
> >
> > >Hello Everybody,
> > >
> > >I have been playing with the AMQP 1.0.0 extension.
> > >I am seeing the following behavior that I find surprising, but I am
> > >not sure whether it is a bug, and if yes in which project (PHP, AMQP
> > >extension or rabbitmq-c).
> > >
> > >$ uname -a
> > >> Linux ip-XXX 2.6.32-317-ec2 #36-Ubuntu SMP Fri Jul 8 18:12:30 UTC 2011
> > >> x86_64 GNU/Linux
> > >
> > >
> > >$ cat /etc/lsb-release
> > >> DISTRIB_ID=Ubuntu
> > >> DISTRIB_RELEASE=10.04
> > >> DISTRIB_CODENAME=lucid
> > >> DISTRIB_DESCRIPTION="Ubuntu 10.04.3 LTS"
> > >
> > >
> > >$ php -v
> > >> PHP 5.3.10 (cli) (built: Feb 17 2012 11:22:03)
> > >> Copyright (c) 1997-2012 The PHP Group
> > >> Zend Engine v2.3.0, Copyright (c) 1998-2012 Zend Technologies
> > >
> > >
> > >$ php -i | grep -i configure
> > >> Configure Command => './configure'
> > >> '--prefix=/home/pierre/'
> > >> '--enable-pcntl'
> > >
> > >
> > >$ pear list -c pecl
> > >> INSTALLED PACKAGES, CHANNEL PECL.PHP.NET:
> > >> =========================================
> > >> PACKAGE VERSION STATE
> > >> amqp 1.0.0 stable
> > >
> > >
> > >This is the script:
> > >
> > ><?php
> > >> pcntl_signal_dispatch();
> > >> global $run;
> > >> $run = true;
> > >> function sig_handler($sig) {
> > >> global $run;
> > >> switch($sig) {
> > >> case SIGUSR1:
> > >> case SIGTERM:
> > >> case SIGINT:
> > >> echo "SIG".PHP_EOL;
> > >> $run = false;
> > >> break;
> > >> }
> > >> }
> > >> pcntl_signal(SIGUSR1, "sig_handler");
> > >> pcntl_signal(SIGTERM, "sig_handler");
> > >> pcntl_signal(SIGINT, "sig_handler");
> > >> $con = new AMQPConnection();
> > >> $con->connect();
> > >> $channel = new AMQPChannel($con);
> > >> $ex = new AMQPExchange($channel);
> > >> $ex->setName("exchange1");
> > >> $ex->setType(AMQP_EX_TYPE_FANOUT);
> > >> $ex->declare();
> > >> $q = new AMQPQueue($channel);
> > >> $q->setName("queue-test");
> > >> $q->declare();
> > >> $q->bind('exchange1', 'routing.key');
> > >> $msg_template = "test";
> > >> try {
> > >> $i = 0;
> > >> while($run) {
> > >> $msg = $i." - ".$msg_template;
> > >> $ex->publish($msg, 'routing.key', 0 /* default */,
> > >> array('delivery_mode' => 2));
> > >> $i++;
> > >> }
> > >> }
> > >> catch (Exception $e) {
> > >> var_dump($e);
> > >> }
> > >> $con->disconnect();
> > >> ?>
> > >
> > >
> > >After starting it and getting its pid, I run:
> > >
> > >kill -s SIGUSR1 <pid>
> > >
> > >
> > >And I get the following exception:
> > >
> > >object(AMQPExchangeException)#5 (7) {
> > >> ["message":protected]=>
> > >> string(0) ""
> > >> ["string":"Exception":private]=>
> > >> string(0) ""
> > >> ["code":protected]=>
> > >> int(0)
> > >> ["file":protected]=>
> > >> string(28) "/home/pierre/publisher.php"
> > >> ["line":protected]=>
> > >> int(39)
> > >> ["trace":"Exception":private]=>
> > >> array(1) {
> > >> [0]=>
> > >> array(6) {
> > >> ["file"]=>
> > >> string(28) "/home/pierre/publisher.php"
> > >> ["line"]=>
> > >> int(39)
> > >> ["function"]=>
> > >> string(7) "publish"
> > >> ["class"]=>
> > >> string(12) "AMQPExchange"
> > >> ["type"]=>
> > >> string(2) "->"
> > >> ["args"]=>
> > >> array(4) {
> > >> [0]=>
> > >> string(13) "191395 - test"
> > >> [1]=>
> > >> string(11) "routing.key"
> > >> [2]=>
> > >> int(0)
> > >> [3]=>
> > >> array(1) {
> > >> ["delivery_mode"]=>
> > >> int(2)
> > >> }
> > >> }
> > >> }
> > >> }
> > >> ["previous":"Exception":private]=>
> > >> NULL
> > >> }
> > >
> > >
> > >Instead of the exception I would expect the current loop iteration to
> > >finish and break out of it and disconnect from the AMQP server.
> > >
> > >So, is this a bug or am I misunderstanding something?
> > >
> > >Thank you.
> > >Pierre
> >
> >
>