2015-04-29 62 views
0

我使用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 

這不工作。不知道,我哪裏出錯了。

有什麼建議嗎?

回答

1

與這一個

# 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'啓動它' –