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?