<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 &#39;use&#39; 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">&lt;<a href="mailto:abbiya@gmail.com" target="_blank">abbiya@gmail.com</a>&gt;</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 &quot;Queue&quot; and now from a
 appfog instance i need to subscribe to the &quot;Queue&quot; 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 &quot;Mysql gone away&quot;. 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>&lt;?</span><span>php

</span><span>if</span><span> </span><span>(!</span><span>defined</span><span>(</span><span>&#39;BASEPATH&#39;</span><span>))</span><span>
  </span><span>exit</span><span>(</span><span>&#39;No direct script access allowed&#39;</span><span>);</span><span>

include</span><span>(</span><span>__DIR__ </span><span>.</span><span> </span><span>&#39;/php-amqplib/config.php&#39;</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-&gt;messages = array();</span><span>
    $exchange </span><span>=</span><span> </span><span>&quot;router&quot;</span><span>;</span><span>
    $queue </span><span>=</span><span> </span><span>&quot;abbiya&quot;</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>-&gt;</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&#39;t be deleted once the channel is closed.
     */</span><span>
    $ch</span><span>-&gt;</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>-&gt;</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&#39;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>&quot;abbiya&quot;</span><span>;</span><span>

    $ch</span><span>-&gt;</span><span>basic_recover</span><span>(</span><span>true</span><span>);</span><span>
    $ch</span><span>-&gt;</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>-&gt;</span><span>body</span><span>);</span><span>
                $msg</span><span>-&gt;</span><span>delivery_info</span><span>[</span><span>&#39;channel&#39;</span><span>]-&gt;</span><span>
                        basic_ack</span><span>(</span><span>$msg</span><span>-&gt;</span><span>delivery_info</span><span>[</span><span>&#39;delivery_tag&#39;</span><span>]);</span><span>

                </span><span>// Send a message with the string &quot;quit&quot; to cancel the consumer.</span><span>
                </span><span>if</span><span> </span><span>(</span><span>$msg</span><span>-&gt;</span><span>body </span><span>===</span><span> </span><span>&#39;quit&#39;</span><span>)</span><span> </span><span>{</span><span>
                    $msg</span><span>-&gt;</span><span>delivery_info</span><span>[</span><span>&#39;channel&#39;</span><span>]-&gt;</span><span>
                            basic_cancel</span><span>(</span><span>$msg</span><span>-&gt;</span><span>delivery_info</span><span>[</span><span>&#39;consumer_tag&#39;</span><span>]);</span><span>
                </span><span>}</span><span>
                $data </span><span>=</span><span> array</span><span>(</span><span>
                    </span><span>&#39;sender_id&#39;</span><span> </span><span>=&gt;</span><span> $message_body</span><span>-&gt;</span><span>r</span><span>,</span><span>
                    </span><span>&#39;receiver_id&#39;</span><span> </span><span>=&gt;</span><span> $message_body</span><span>-&gt;</span><span>s</span><span>,</span><span>
                    </span><span>&#39;message_content&#39;</span><span> </span><span>=&gt;</span><span> $message_body</span><span>-&gt;</span><span>m</span><span>,</span><span>
                    </span><span>// &#39;sent_time&#39; =&gt; $message_body-&gt;t,</span><span>
                    </span><span>&#39;status&#39;</span><span> </span><span>=&gt;</span><span> </span><span>0</span><span>
                </span><span>);</span><span>
                $ci </span><span>=&amp;</span><span> get_instance</span><span>();</span><span>
                $ci</span><span>-&gt;</span><span>Message_model</span><span>-&gt;</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>-&gt;</span><span>callbacks</span><span>))</span><span> </span><span>{</span><span>
        $ch</span><span>-&gt;</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>-&gt;</span><span>close</span><span>();</span><span>
                $conn</span><span>-&gt;</span><span>close</span><span>();</span><span>
                $this</span><span>-&gt;</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>