使用官网推荐的swoole-yaf框架,启动服务后 发现worker进程在不断重启,worker启动后会自动执行默认的controller action。master和manager 是稳定的{{{
class HttpServer
{
public static $instance;
public $http;
public static $get;
public static $post;
public static $header;
public static $server;
private $application;
public $processName = 'HttpServer';
public function __construct() {
$http = new swoole_http_server("172.30.102.41", 9501);
$http->set(
array(
'worker_num' => 4,
'daemonize' => false,
'max_request' => 1000,
'dispatch_mode' => 1
)
);
$http->on('Start', array($this, 'onMasterStart'));
$http->on('ManagerStart', array($this, 'onManagerStart'));
$http->on('WorkerStart' , array( $this , 'onWorkerStart'));
$http->on('request', function ($request, $response) {
if( isset($request->server) ) {
HttpServer::$server = $request->server;
}else{
HttpServer::$server = [];
}
if( isset($request->header) ) {
HttpServer::$header = $request->header;
}else{
HttpServer::$header = [];
}
if( isset($request->get) ) {
HttpServer::$get = $request->get;
}else{
HttpServer::$get = [];
}
if( isset($request->post) ) {
HttpServer::$post = $request->post;
}else{
HttpServer::$post = [];
}
ob_start();
try {
$yaf_request = new Yaf_Request_Http(
HttpServer::$server['request_uri']);
$this->application
->getDispatcher()->dispatch($yaf_request);
} catch ( Yaf_Exception $e ) {
var_dump( $e );
}
$result = ob_get_contents();
ob_end_clean();
$response->end($result);
});
$http->setGlobal(HTTP_GLOBAL_ALL);
$http->start();
}
public function onMasterStart($server) {
swoole_set_process_name($this->processName.' master process');
}
public function onWorkerStart($server, $workerId) {
swoole_set_process_name($this->processName.' worker process');
define('APPLICATION_PATH', dirname(__DIR__));
$this->application = new Yaf_Application( APPLICATION_PATH .
"/conf/application.ini");
ob_start();
try {
$this->application->bootstrap()->run();
} catch(Yaf_Exception $e) {
var_dump($e);
}
ob_end_clean();
}
public function onManagerStart($server) {
swoole_set_process_name($this->processName.' manager process');
}
public static function getInstance() {
if (!self::$instance) {
self::$instance = new HttpServer;
}
return self::$instance;
}
}
HttpServer::getInstance();
}}}
{{{
[vagrant@localhost ~]$ ps uax | grep HttpServer
vagrant 12430 0.2 2.3 445548 23988 pts/2 Sl+ 07:46 0:00 HttpServer master process
vagrant 12431 2.7 0.6 369360 6296 pts/2 S+ 07:46 0:00 HttpServer manager process
vagrant 13378 0.0 0.6 187024 6872 pts/2 R+ 07:46 0:00 HttpServer worker process
vagrant 13379 0.0 0.7 253348 7236 pts/2 R+ 07:46 0:00 HttpServer worker process
vagrant 13380 0.0 0.7 365720 7348 pts/2 D+ 07:46 0:00 HttpServer worker process
vagrant 13382 0.0 0.0 103320 908 pts/1 S+ 07:46 0:00 grep HttpServer
[vagrant@localhost ~]$ ps uax | grep HttpServer
vagrant 12430 0.2 2.3 445548 23988 pts/2 Sl+ 07:46 0:00 HttpServer master process
vagrant 12431 2.6 0.6 369360 6296 pts/2 S+ 07:46 0:00 HttpServer manager process
vagrant 13477 0.0 0.7 356884 7864 pts/2 R+ 07:46 0:00 HttpServer worker process
vagrant 13478 0.0 0.7 365720 7348 pts/2 D+ 07:46 0:00 HttpServer worker process
vagrant 13479 0.0 0.7 365720 7280 pts/2 D+ 07:46 0:00 HttpServer worker process
vagrant 13480 0.0 0.7 365720 7236 pts/2 D+ 07:46 0:00 HttpServer worker process
vagrant 13482 0.0 0.0 103320 908 pts/1 S+ 07:46 0:00 grep HttpServer
}}}