title: Codeigniter Daemon using System_Daemon package
body: <p>I am trying to create a daemon using System_Daemon package with CodeIgniter's CLI. This is a new area for me and I'm struggling.</p>
  
  <p>Here's what I have:
  A CI controller that injects messages into an AWS SQS queue (Thanks to [url=<a href="http://codeigniter.com/forums/member/196201/]coccodrillo[/url]" rel="nofollow noreferrer">http://codeigniter.com/forums/member/196201/]coccodrillo[/url]</a> for providing excellent instructions on how to integrated AWS SDK into CI. See here: <a href="https://stackoverflow.com/questions/9541613/integrating-aws-sdk-as-a-library-in-codeigniter">Integrating AWS SDK as a library in Codeigniter</a>).</p>
  
  <p>A CI controller that receives messages in the queue and writes it out to a log file and then deletes the message in the queue.</p>
  
  <p>I would like to have a CI daemon which will listen to this queue, receive messages when they are there and do something useful with the message and then delete the message. So I started with the example in the documentation for System_Daemon and added in the CI code from receiving program. See code below</p>
  
  <p>Is this the right thing to do? Can you guide me into doing this the "right way"?  I've trolled the various knowledgeable forums and have come up short....Help me please! </p>
  
  <p>Mmiz</p>
  
  <pre><code>#!/usr/bin/php -q
  &lt;?php
  
  // Make it possible to test in source directory
  // This is for PEAR developers only
  ini_set('include_path', ini_get('include_path').':..');
  
  // Include Class
  error_reporting(E_ALL);
  require_once "System/Daemon.php";
  
  // Bare minimum setup
  System_Daemon::setOption("appName", "receiveaws");
  System_Daemon::setOption("logLocation","/tmp/log/receiveaws.log");
  System_Daemon::setOption("appPidLocation","/tmp/log/receiveaws/receiveaws.pid");
  System_Daemon::log(System_Daemon::LOG_INFO, "Daemon not yet started so this will be written on-screen");
  
  // Spawn Deamon!
  System_Daemon::start();
  System_Daemon::log(System_Daemon::LOG_INFO, "Daemon: '".
      System_Daemon::getOption("appName").
          "' spawned! This will be written to ".
              System_Daemon::getOption("logLocation"));
  
  System_Daemon::log(System_Daemon::LOG_WARNING, 'My php code starting');
  class Receiveaws extends CI_Controller {
  
      public function index(){
      if ($this-&gt;input-&gt;is_cli_request()) {
          //Load the aws library
          $this-&gt;load-&gt;library('awslib');
          $sqs = new AmazonSQS();
  
          //Get the queue to look at
          $res=$sqs-&gt;get_queue_url('example-queue');
  
          //Get the queue's url
          $qurl=($res-&gt;body-&gt;GetQueueUrlResult-&gt;QueueUrl);
          System_Daemon::log(System_Daemon::LOG_INFO,$qurl);
  
          //Get a message from the queue
          $response = $sqs-&gt;receive_message($qurl);
  
          //If there was a message received, then do something
              if ($res-&gt;isOK()) {
              System_Daemon::log(System_Daemon::LOG_INFO,"Receive message successful");
                              //Now delete message from queue
              $res=$sqs-&gt;delete_message($qurl,$rcpt_hand);
              if ($res-&gt;isOK()) {
                  System_Daemon::log(System_Daemon::LOG_INFO,"Delete message successful");
              }
          } else {
              //go back to check for messages
              //How do you do that?
          }
      } else {
          //Access from URL - so bail out?
          //how do you not bail out of the daemon from here?
      }
      }
  }
  System_Daemon::stop();
  ?&gt;
  </code></pre>