From c1e33376ca0f016787f00663a3d7cdec0f96fec2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=9C=E6=B5=81?= Date: Wed, 23 May 2018 11:43:20 +0800 Subject: [PATCH] tcp pid update --- App.php | 34 ++++++-- Http/HttpYafServer.php | 29 ++----- Tcp/SwooleServer.php | 183 +++++++++++++++++++++++++++-------------- 3 files changed, 158 insertions(+), 88 deletions(-) diff --git a/App.php b/App.php index de349d8..95048ad 100644 --- a/App.php +++ b/App.php @@ -27,6 +27,7 @@ class App */ protected $climate; + protected $_service; /** * 入口文件加载 @@ -51,6 +52,7 @@ public function run($params) if ($item['service'] == '--help') { $this->help(); } + $this->_service = $item['service']; if ($item['service'] == '-h') { if (in_array($item['command'], ['start', 'stop', 'reload'])) { call_user_func([$this, $item['command']]); @@ -78,7 +80,12 @@ public function start() $climate = $this->climate; $climate->style->addCommand('rage', ['green','bold']); $climate->br(1)->rage('Server is starting....'); - $app = new \ultraman\Http\HttpYafServer(); + + if ($this->_service == '-h') { + $app = new \ultraman\Http\HttpYafServer(); + } elseif ($this->_service == '-t') { + $app = new \ultraman\Tcp\SwooleServer(); + } $app->run(); } @@ -88,18 +95,28 @@ private function stop() $climate = $this->climate; $climate->style->addCommand('rage', ['green','bold']); $climate->br(1)->rage('Server is stopping....'); - $app = new \ultraman\Http\HttpYafServer(); + + if ($this->_service == '-h') { + $app = new \ultraman\Http\HttpYafServer(); + } elseif ($this->_service == '-t') { + $app = new \ultraman\Tcp\SwooleServer(); + } $app->stop(); $climate->br(1)->rage('Server is stopping....ok'); die; } + //重载服务 public function reload() { $climate = $this->climate; $climate->style->addCommand('rage', ['green','bold']); $climate->br(1)->rage('Server is reloadping....'); - $app = new \ultraman\Http\HttpYafServer(); + if ($this->_service == '-h') { + $app = new \ultraman\Http\HttpYafServer(); + } elseif ($this->_service == '-t') { + $app = new \ultraman\Tcp\SwooleServer(); + } $app->reload(); $climate->br(1)->rage('Server is reloadping....ok'); die; @@ -116,11 +133,12 @@ public function help() $climate->br(2)->draw('ultraman'); $climate->style->addCommand('holler', ['underline', 'green', 'bold']); $climate->br(2)->holler('帮助'); - $climate->br(2)->info('php cli (-h|-t) (start|stop|reload) (env) (port)'); - $climate->br(1)->info('php cli -h start test 启动测试环境'); - $climate->br(1)->info('php cli -h start dev 启动开发环境'); - $climate->br(1)->info('php cli -h start prod 启动正式环境'); - $climate->br(1)->info('php cli -h start pre 启动预发布环境'); + $climate->br(1)->out('基础命令:php cli (-h|-t) (start|stop|reload) (env) (port) 启动'); + $climate->br(1)->out('-h / -t: -h 启动http服务 -t 启动tcp服务'); + $climate->br(1)->out('start / stop / reload: start 启动服务 stop 停止服务 reload重载服务'); + $climate->br(1)->out('env: 环境区分建议为dev test pre prod'); + $climate->br(1)->out('port: 环境端口 可不填写默认读取main.ini配置'); + $climate->br(5); die; } diff --git a/Http/HttpYafServer.php b/Http/HttpYafServer.php index 31226d4..37c773f 100644 --- a/Http/HttpYafServer.php +++ b/Http/HttpYafServer.php @@ -26,13 +26,18 @@ public function run() $this->climate = new CLImate(); $config = @parse_ini_file(APPLICATION_PATH."/conf/main.ini", true); - $this->pid = APPLICATION_PATH.'/'.$config['common']['application.service_name'].'-service.pid'; + $this->pid = APPLICATION_PATH.'/'.$config['common']['application.service_name'].'-http-service.pid'; if ($this->isRunning()) { $this->climate->error('服务已经启动'); die; } - $http_service = $config['http-service']; + $http_service = $config['http-service']??''; + if ($http_service == '') { + $this->climate->error('未配置http服务器'); + die; + } + $app = new \ultraman\Http\SwooleHttpServer($http_service); $http = $app->connent(); $this->http = $http; @@ -44,7 +49,6 @@ public function run() $http->start(); } - public function onRequest($request, $response) { if ($request->server['request_uri'] == '/favicon.ico') { @@ -76,7 +80,6 @@ public function onRequest($request, $response) $response->end($result); } - public function onWorkerStart($serv, $worker_id) { $config = APPLICATION_PATH.'/conf/main.ini'; @@ -99,9 +102,8 @@ public function onStart($serv) { $config = @parse_ini_file(APPLICATION_PATH."/conf/main.ini", true); $name = $config['common']['application.service_name']; - echo $name."服务启动\n"; - @cli_set_process_title($name); file_put_contents($this->pid, $serv->master_pid); + echo $name."服务启动\n"; } @@ -154,10 +156,6 @@ public function onFinish($serv, $task_id, $data) \ultraman\Log\monoLog::write("INFO", $taskdata); } - /** - * 判断服务是否已经启动 - * @return bool - */ public function isRunning() { if (!file_exists($this->pid)) { @@ -168,14 +166,11 @@ public function isRunning() return (bool) posix_getpgid($pid); } - /** - * 获取进程id - */ private function getPid() { $this->climate = new CLImate(); $config = @parse_ini_file(APPLICATION_PATH."/conf/main.ini", true); - $this->pid = APPLICATION_PATH.'/'.$config['common']['application.service_name'].'-service.pid'; + $this->pid = APPLICATION_PATH.'/'.$config['common']['application.service_name'].'-http-service.pid'; if (!file_exists($this->pid)) { $this->climate->error("没有这个进程"); die; @@ -198,12 +193,6 @@ public function stop() unlink($this->pid); return false; } - - - - /** - * 重载服务 - */ public function reload() { posix_kill($this->getPid(), SIGUSR1); diff --git a/Tcp/SwooleServer.php b/Tcp/SwooleServer.php index 65bdf6d..48b9565 100644 --- a/Tcp/SwooleServer.php +++ b/Tcp/SwooleServer.php @@ -6,79 +6,142 @@ namespace ultraman\Tcp; -define('AUTHLOAD',dirname(dirname(dirname(dirname(__FILE__))))); -define('APPLICATION_PATH',dirname(dirname(dirname(dirname(dirname(__FILE__)))))); +define('AUTHLOAD', dirname(dirname(dirname(dirname(__FILE__))))); +define('APPLICATION_PATH', dirname(dirname(dirname(dirname(dirname(__FILE__)))))); require AUTHLOAD.'/autoload.php'; +use League\CLImate\CLImate; +use ultraman\Foundation\DI; class SwooleServer { - public function __construct() - { - new \ultraman\App(); - $this->run(); - } - - public function run() - { - $config = @parse_ini_file(APPLICATION_PATH."/conf/main.ini",true); - $main = $config['tcp-service']; - $name = $config['common']['application.service_name'].'tcp'; - $server = new \Hprose\Swoole\Server("tcp://".$main['host'].':'.$main['port']); + protected $climate; + protected $pid; + public function run() + { + $this->climate = new CLImate(); + + $config = @parse_ini_file(APPLICATION_PATH."/conf/main.ini", true); + $this->pid = APPLICATION_PATH.'/'.$config['common']['application.service_name'].'-tcp-service.pid'; + if ($this->isRunning()) { + $this->climate->error('服务已经启动'); + die; + } + $main = $config['tcp-service']??''; + if ($main == '') { + $this->climate->error('未配置tcp服务器'); + die; + } + if (DI::get('port') != '') { + $main['port'] = DI::get('port'); + } + + $server = new \Hprose\Swoole\Server("tcp://".$main['host'].':'.$main['port']); \Yaf_Registry::set('swoole_tcp', $server); - $server->set( - array( - 'worker_num'=>isset($main['worker_num'])?$main['worker_num']:3, - 'daemonize'=>isset($main['daemonize'])?$main['daemonize']:false, - 'max_request'=>isset($main['max_request'])?$main['max_request']:10000, - 'dispatch_mode'=>isset($main['dispatch_mode'])?$main['dispatch_mode']:0, - 'task_worker_num'=>isset($main['task_worker_num'])?$main['task_worker_num']:4, - 'task_ipc_mode'=>isset($main['task_ipc_mode'])?$main['task_ipc_mode']:1, - 'task_max_request'=>isset($main['task_max_request'])?$main['task_max_request']:5000, - 'heartbeat_check_interval'=>isset($main['heartbeat_check_interval'])?$main['heartbeat_check_interval']:30, - 'heartbeat_idle_time'=>isset($main['heartbeat_idle_time'])?$main['heartbeat_idle_time']:60, - ) - ); - $server->on('task', array($this, 'onTask')); + $server->set( + array( + 'worker_num'=>isset($main['worker_num'])?$main['worker_num']:3, + 'daemonize'=>isset($main['daemonize'])?$main['daemonize']:false, + 'max_request'=>isset($main['max_request'])?$main['max_request']:10000, + 'dispatch_mode'=>isset($main['dispatch_mode'])?$main['dispatch_mode']:0, + 'task_worker_num'=>isset($main['task_worker_num'])?$main['task_worker_num']:4, + 'task_ipc_mode'=>isset($main['task_ipc_mode'])?$main['task_ipc_mode']:1, + 'task_max_request'=>isset($main['task_max_request'])?$main['task_max_request']:5000, + 'heartbeat_check_interval'=>isset($main['heartbeat_check_interval'])?$main['heartbeat_check_interval']:30, + 'heartbeat_idle_time'=>isset($main['heartbeat_idle_time'])?$main['heartbeat_idle_time']:60, + ) + ); + $server->setGetEnabled=true; + $server->on('Start', array($this, 'onStart')); + $server->on('task', array($this, 'onTask')); $server->on('finish', array($this, 'onFinish')); + $server->addInstanceMethods(new $main['func']); + $server->setDebugEnabled(false); + $server->setErrorTypes(E_ALL); + $server->start(); + } - $server->addInstanceMethods(new $main['func']); - $server->setGetEnabled=true; - $server->setDebugEnabled(false); - $server->setErrorTypes(E_ALL); - @cli_set_process_title($name); - $server->start(); - } - - public function onTask($serv, $task_id, $from_id, array $taskdata) - { + public function onStart($serv) + { + $config = @parse_ini_file(APPLICATION_PATH."/conf/main.ini", true); + $name = $config['common']['application.service_name']; + file_put_contents($this->pid, $serv->master_pid); + echo $name."TCP服务启动\n"; + } - if($taskdata['cli'] == ''){ - return false; - } - $cli = $taskdata['cli']; - $params = ''; - if($taskdata['params']!=''){ - foreach ($taskdata['params'] as $key => $value) { - if(trim($value) == ''){ - continue; + public function onTask($serv, $task_id, $from_id, $data) + { + if (isset($data['cli']) && $data['cli'] != '') { + $cli = $data['cli']; + $params = ''; + if (isset($data['params']) && count($data['params'])!=0 && is_array($data['params'])) { + foreach ($data['params'] as $key => $value) { + if (trim($value) == '') { + continue; + } + $params.="--{$key} ".$value." "; } - $params.="--{$key} ".$value." "; - } - } - $path = 'php '.APPLICATION_PATH.'/public/cli '.$cli.' '. $params; - $app = exec($path); - $taskdata['msg'] = "异步任务[来自进程 {$from_id},当前进程 {$task_id}"; - \ultraman\Log\monoLog::write("INFO",$taskdata); - $taskdata = serialize($taskdata); - $serv->finish($taskdata); + } + $path = 'php '.APPLICATION_PATH.'/cli '.$cli.' '. $params; + exec($path); + } else { + call_user_func_array($data['class'], [$data]); + } + + $items['msg'] = "异步任务[来自进程 {$from_id},当前进程 {$task_id}"; + $taskdata = serialize($items); + $serv->finish($taskdata); + } + + public function onFinish($serv, $task_id, $data) + { + $params = unserialize($data); + $taskdata['msg'] = "异步任务当前进程 {$task_id} 执行完成"; + \ultraman\Log\monoLog::write("INFO", $taskdata); + } + + public function isRunning() + { + if (!file_exists($this->pid)) { + return false; + } + + $pid = file_get_contents($this->pid); + return (bool) posix_getpgid($pid); } - public function onFinish($serv, $task_id, $data) + + private function getPid() + { + $this->climate = new CLImate(); + $config = @parse_ini_file(APPLICATION_PATH."/conf/main.ini", true); + $this->pid = APPLICATION_PATH.'/'.$config['common']['application.service_name'].'-tcp-service.pid'; + if (!file_exists($this->pid)) { + $this->climate->error("没有这个进程"); + die; + } + $pid = file_get_contents($this->pid); + if (posix_getpgid($pid)) { + return $pid; + } + unlink($this->pid); + return false; + } + + public function stop() + { + $pid = $this->getPid(); + posix_kill($pid, SIGTERM); + usleep(500); + posix_kill($pid, SIGKILL); + unlink($this->pid); + return false; + } + + public function reload() { - $params = unserialize($data); - $taskdata['msg'] = "异步任务当前进程 {$task_id} 执行完成"; - \ultraman\Log\monoLog::write("INFO",$taskdata); + posix_kill($this->getPid(), SIGUSR1); + return true; } }