0

I have a php script that runs in a never ending loop using ReactPHP Loop and HTTPServer

Now, I am using an AWS Elastic Beanstalk Worker which reads an SQS queue for which HTTPServer on port 80 is required. Here is my PHP Script:

<?php

/* loading the base configuration files and defines */
require_once 'base/bootstrap.php';

$loop   = React\EventLoop\Factory::create();
$socket = new React\Socket\Server($loop);
$http   = new React\Http\Server($socket);

$i = 0;
$j = 0;

$api = BaseClass::initiate(); // the initially loaded class setting up the environment

/**
 * The $loop React Event Loop will make sure that $api BaseClass required to be alive
 * is up-to-date managing the Environment
 */
$loop->addPeriodicTimer(3, function ($timer) use (&$i, &$j, $loop, &$api) {
    try {

        if ($i >= rand(300, 400)) {
            /* Refresh the Environment if any DB changes happen, like new clients added, etc. */
            $api = BaseClass::initiate();
        }

        /**
         * I Do my Work Here
         */

    } catch (Exception $e) {
        /**
         * Error Handling
         */
    }
    $i++;
    $j++;
});

/**
 * The $http React HTTP Server will listen to the SQS Queue and perform actions accordingly
 */
$http->on('request', function (React\Http\Request $request, React\Http\Response $response) use (&$api) {
    $request->on('data', function ($data) use (&$api, $request, $response) {
        try {
            $postData = json_decode($data, TRUE);

            /**
             * I process the Post Data here using $api loaded Class
             */

        } Catch (Exception $e) {
            /* Send an Error Message to the Queue */
            /**
             * Error Handling
             */
        }
    });
});

$socket->listen(80);
$loop->run();

The reason i am looking at this structure is that $api opens a socket connection and needs to be alive for the $http Server to process.

nohup php /var/app/current/server.php & just works fine, however, i am looking at creating an upstart service

My UpStart script is as follow within /etc/init/myscript.conf

#Info
start on startup
stop on shutdown
respawn
php /var/app/current/server.php

which does not work. Not sure, where i am going wrong.

Any Suggestions?

Guns
  • 2,678
  • 2
  • 23
  • 51

1 Answers1

1

Replace your /etc/init/myscript.conf with this one

# Info
description "myscript name"
author      "Yourself"

# Events
start on startup
stop on shutdown

# Automatically respawn
respawn
respawn limit 20 5

# Run the script!
# Note, in this example, if your PHP script returns
# the string "ERROR", the daemon will stop itself.
script
    [ $(exec /usr/bin/php -f /var/app/current/server.php) = 'ERROR' ] && ( stop; exit 1; )
end script

then start it with

sudo service myscript start
greg_diesel
  • 2,955
  • 1
  • 15
  • 24
  • it says myscript: unrecognized service when i do sudo service myscript start – Guns Apr 29 '15 at 20:19
  • also tried `start myscript` and it gave me a PID however, `status myscript` said that the service has stopped – Guns Apr 29 '15 at 20:21
  • Use whatever you named /etc/init/myscript.conf as the service name. In my example it is 'myscript' . If you named your conf file `/etc/init/guns.conf `then you would start it with `sudo service guns start` – greg_diesel Apr 29 '15 at 20:23
  • Make sure you config file saved correctly. go to /etc/init/ and `ls -l` make sure it is there `-rw-r--r-- 1 root root 388 Apr 29 15:24 guns.conf` – greg_diesel Apr 29 '15 at 20:26
  • yes Greg, that is exactly my result... `-rw-r--r-- 1 root root 385 Apr 29 20:28 guns.conf` – Guns Apr 29 '15 at 20:29