新贵ReactPHP脚本 - AWS EC2 ElasticbeanStalk

问题描述:

我使用ReactPHP环路和HttpServer的是,在永无止境的循环运行的PHP脚本新贵ReactPHP脚本 - AWS EC2 ElasticbeanStalk

现在,我使用的是AWS弹性魔豆工人,其读取其中的HttpServer的SQS队列上端口80是必需的。这是我的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服务器来处理。

nohup php /var/app/current/server.php &只是工作正常,但是,我期待创造一个暴发户服务

我新贵脚本是内遵循/etc/init/myscript.conf

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

这不工作。不知道,我哪里出错了。

有什么建议吗?

与这一个

# 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 

更换你/etc/init/myscript.conf然后用

sudo service myscript start 
+0

它说的MyScript启动:无法识别的服务的时候我做须藤服务的MyScript启动 – Guns

+0

也尝试过'启动myscript',它给了我一个PID,但是'status myscript'表示服务已经停止 – Guns

+0

使用你命名的/etc/init/myscript.conf作为服务名。在我的例子中,它是'myscript'。如果你命名你的conf文件'/etc/init/guns.conf',那么你可以用'sudo service guns start'启动它' –