<div dir="ltr">Hi,<div><br></div><div style>The code for <span style="white-space:pre-wrap">register_shutdown_function is used to call a function when PHP is shutting down. For example if you want to do some resources clean up (like closing database connections and so on), you could do it there. </span></div>
<div style><span style="white-space:pre-wrap"><br></span></div><div style><span style="white-space:pre-wrap">If inside that shutdown function you need to call methods in the controller, that is, your $this object, then you need to pass it as an extra argument, as explained here: <a href="http://php.net/manual/en/function.register-shutdown-function.php">http://php.net/manual/en/function.register-shutdown-function.php</a></span></div>
<div><br></div><div style>On the other hand if you need to pass more arguments to the RabbitMQ callback, the one that you use in your basic_consume function, the you need to pass them to the callback function there, for example by using the 'use' keyword as explained here: <a href="http://php.net/manual/en/functions.anonymous.php">http://php.net/manual/en/functions.anonymous.php</a></div>
<div><br></div><div style>Besides that I recommend you to refactor that consumer code out of the controller into a consumer class or something similar. Keep in mind to always keep thin controllers.</div><div style><br></div>
<div style>Also if your consumer callbacks need to use a lot of extra arguments, maybe is better to create a class for them, instantiate an object and pass the data inside via the constructor for example.</div><div style>
<br></div><div style>Regarding MySQL going away, you probably need to search in a php-mysql forum to see how to solve that MySQL timeout. You could either increase the timeout value in the server conf, issue a simple query every now and then to keep the connection alive or implement a reconnection strategy, for example by catching an exception.</div>
<div><br></div><div style>Regards,</div><div style><br></div><div style>Alvaro</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Thu, Apr 25, 2013 at 8:30 AM, Seshachalam Malisetti <span dir="ltr"><<a href="mailto:abbiya@gmail.com" target="_blank">abbiya@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><p>I developed an android app where it subscribes to a queue and also
publishes to other queues. At a time it publishes the same message to
two different queues, one of them is a queue named "Queue" and now from a
appfog instance i need to subscribe to the "Queue" and consume messages
and insert them in a mysql db.</p>
<p>I created a php standalone app for the above purpose with
codeigniter. By some reason the worker app loses its connection to
rabbitmq. i would like to know the best way to do this. How can a worker
app on appfog can sustain the application restarts.</p>
<p>what of kind of thing i need to use to solve the above problem.</p>
<p>I figured that the problem is not with rabbitmq connection. it is
with the code related to mysql inserts. i checked the crash logs of my
app and the error is "Mysql gone away". an example of php rabbitmq
consumer has call backs for receive message and register_shutdown. in
receive call back i can not use $this of code igniter because its out of
scope and i was using get_instance(). i am not sure how to call a
method from rabbitmq client receive call back function</p>
<p>The controller is <br></p><p><br></p><pre><code><span><?</span><span>php
</span><span>if</span><span> </span><span>(!</span><span>defined</span><span>(</span><span>'BASEPATH'</span><span>))</span><span>
</span><span>exit</span><span>(</span><span>'No direct script access allowed'</span><span>);</span><span>
include</span><span>(</span><span>__DIR__ </span><span>.</span><span> </span><span>'/php-amqplib/config.php'</span><span>);</span><span>
</span><span>use</span><span> </span><span>PhpAmqpLib</span><span>\Connection\A</span><span>MQPConnection</span><span>;</span><span>
</span><span>class</span><span> </span><span>Welcome</span><span> </span><span>extends</span><span> CI_Controller </span><span>{</span><span>
</span><span>public</span><span> </span><span>function</span><span> __construct</span><span>()</span><span> </span><span>{</span><span>
parent</span><span>::</span><span>__construct</span><span>();</span><span>
</span><span>}</span><span>
</span><span>public</span><span> </span><span>function</span><span> index</span><span>()</span><span> </span><span>{</span><span>
</span><span>//connect to rabbitmq and consume messages</span><span>
</span><span>//insert messages to mysql</span><span>
</span><span>//$this->messages = array();</span><span>
$exchange </span><span>=</span><span> </span><span>"router"</span><span>;</span><span>
$queue </span><span>=</span><span> </span><span>"abbiya"</span><span>;</span><span>
$conn </span><span>=</span><span> </span><span>new</span><span> </span><span>AMQPConnection</span><span>(</span><span>HOST</span><span>,</span><span> PORT</span><span>,</span><span> USER</span><span>,</span><span> PASS</span><span>,</span><span> VHOST</span><span>);</span><span>
$ch </span><span>=</span><span> $conn</span><span>-></span><span>channel</span><span>();</span><span>
</span><span>/*
name: $queue
passive: false
durable: true // the queue will survive server restarts
exclusive: false // the queue can be accessed in other channels
auto_delete: false //the queue won't be deleted once the channel is closed.
*/</span><span>
$ch</span><span>-></span><span>queue_declare</span><span>(</span><span>$queue</span><span>,</span><span> </span><span>false</span><span>,</span><span> </span><span>true</span><span>,</span><span> </span><span>false</span><span>,</span><span> </span><span>false</span><span>);</span><span>
$ch</span><span>-></span><span>queue_bind</span><span>(</span><span>$queue</span><span>,</span><span> $exchange</span><span>,</span><span> $queue</span><span>);</span><span>
</span><span>/*
queue: Queue from where to get the messages
consumer_tag: Consumer identifier
no_local: Don't receive messages published by this consumer.
no_ack: Tells the server if the consumer will acknowledge the messages.
exclusive: Request exclusive consumer access, meaning only this consumer can access the queue
nowait:
callback: A PHP Callback
*/</span><span>
$consumer_tag </span><span>=</span><span> </span><span>"abbiya"</span><span>;</span><span>
$ch</span><span>-></span><span>basic_recover</span><span>(</span><span>true</span><span>);</span><span>
$ch</span><span>-></span><span>basic_consume</span><span>(</span><span>$queue</span><span>,</span><span> $consumer_tag</span><span>,</span><span> </span><span>false</span><span>,</span><span> </span><span>false</span><span>,</span><span> </span><span>false</span><span>,</span><span> </span><span>false</span><span>,</span><span> </span><span>function</span><span>(</span><span>$msg</span><span>)</span><span> </span><span>{</span><span>
$message_body </span><span>=</span><span> json_decode</span><span>(</span><span>$msg</span><span>-></span><span>body</span><span>);</span><span>
$msg</span><span>-></span><span>delivery_info</span><span>[</span><span>'channel'</span><span>]-></span><span>
basic_ack</span><span>(</span><span>$msg</span><span>-></span><span>delivery_info</span><span>[</span><span>'delivery_tag'</span><span>]);</span><span>
</span><span>// Send a message with the string "quit" to cancel the consumer.</span><span>
</span><span>if</span><span> </span><span>(</span><span>$msg</span><span>-></span><span>body </span><span>===</span><span> </span><span>'quit'</span><span>)</span><span> </span><span>{</span><span>
$msg</span><span>-></span><span>delivery_info</span><span>[</span><span>'channel'</span><span>]-></span><span>
basic_cancel</span><span>(</span><span>$msg</span><span>-></span><span>delivery_info</span><span>[</span><span>'consumer_tag'</span><span>]);</span><span>
</span><span>}</span><span>
$data </span><span>=</span><span> array</span><span>(</span><span>
</span><span>'sender_id'</span><span> </span><span>=></span><span> $message_body</span><span>-></span><span>r</span><span>,</span><span>
</span><span>'receiver_id'</span><span> </span><span>=></span><span> $message_body</span><span>-></span><span>s</span><span>,</span><span>
</span><span>'message_content'</span><span> </span><span>=></span><span> $message_body</span><span>-></span><span>m</span><span>,</span><span>
</span><span>// 'sent_time' => $message_body->t,</span><span>
</span><span>'status'</span><span> </span><span>=></span><span> </span><span>0</span><span>
</span><span>);</span><span>
$ci </span><span>=&</span><span> get_instance</span><span>();</span><span>
$ci</span><span>-></span><span>Message_model</span><span>-></span><span>newMessage</span><span>(</span><span>$data</span><span>);</span><span>
</span><span>}</span><span>
</span><span>);</span><span>
</span><span>// Loop as long as the channel has callbacks registered</span><span>
</span><span>while</span><span> </span><span>(</span><span>count</span><span>(</span><span>$ch</span><span>-></span><span>callbacks</span><span>))</span><span> </span><span>{</span><span>
$ch</span><span>-></span><span>wait</span><span>();</span><span>
</span><span>}</span><span>
register_shutdown_function</span><span>(</span><span>function</span><span>()</span><span> </span><span>use</span><span> </span><span>(</span><span>$ch</span><span>,</span><span> $conn</span><span>)</span><span> </span><span>{</span><span>
$ch</span><span>-></span><span>close</span><span>();</span><span>
$conn</span><span>-></span><span>close</span><span>();</span><span>
$this</span><span>-></span><span>index</span><span>();</span><span>
</span><span>}</span><span>
</span><span>);</span><span>
</span><span>}</span><span>
</span><span>}</span><span>
</span><span>/* End of file welcome.php */</span><span>
</span><span>/* Location: ./application/controllers/welcome.php */</span></code></pre><br>_______________________________________________<br>
rabbitmq-discuss mailing list<br>
<a href="mailto:rabbitmq-discuss@lists.rabbitmq.com">rabbitmq-discuss@lists.rabbitmq.com</a><br>
<a href="https://lists.rabbitmq.com/cgi-bin/mailman/listinfo/rabbitmq-discuss" target="_blank">https://lists.rabbitmq.com/cgi-bin/mailman/listinfo/rabbitmq-discuss</a><br>
<br></blockquote></div><br></div>