![](/img/trans.png)
[英]PHP Upstart on Amazon EC2 Linux (Elasticbeanstalk)
[英]Upstart ReactPHP Script - AWS EC2 ElasticbeanStalk
我有一個使用ReactPHP Loop和HTTPServer在永無休止的循環中運行的php腳本
現在,我正在使用一個AWS Elastic Beanstalk Worker,它讀取一個SQS隊列,為此端口80上需要HTTPServer。 這是我的PHP腳本:
<?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();
我查看此結構的原因是$ api打開套接字連接,並且需要$ http Server才能運行。
nohup php /var/app/current/server.php &
正常工作,但是,我正在尋找創建一個暴發戶服務
我的UpStart腳本在/etc/init/myscript.conf中如下所示
#Info
start on startup
stop on shutdown
respawn
php /var/app/current/server.php
這不起作用。 不知道,我要去哪里錯了。
有什么建議么?
用這個替換您的/etc/init/myscript.conf
# 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
然后開始
sudo service myscript start
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.