0

我有一个使用 ReactPHP Loop 和 HTTPServer 在永无止境的循环中运行的 php 脚本

现在,我正在使用 AWS Elastic Beanstalk Worker,它读取需要端口 80 上的 HTTPServer 的 SQS 队列。这是我的 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 &工作正常,但是,我正在考虑创建一个新贵服务

我的 UpStart 脚本在/etc/init/myscript.conf中如下所示

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

这是行不通的。不确定,我哪里出错了。

有什么建议么?

4

1 回答 1

1

用这个替换你的/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
于 2015-04-29T20:16:18.400 回答