Skip to content

Commit

Permalink
tcp pid update
Browse files Browse the repository at this point in the history
  • Loading branch information
东流 committed May 23, 2018
1 parent c224e40 commit c1e3337
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 88 deletions.
34 changes: 26 additions & 8 deletions App.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class App
*/

protected $climate;
protected $_service;

/**
* 入口文件加载
Expand All @@ -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']]);
Expand Down Expand Up @@ -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();
}

Expand All @@ -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;
Expand All @@ -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('基础命令:<bold><green>php cli (-h|-t) (start|stop|reload) (env) (port)</green></bold> 启动');
$climate->br(1)->out('<bold><green>-h / -t:</green></bold> -h 启动http服务 -t 启动tcp服务');
$climate->br(1)->out('<bold><green>start / stop / reload:</green></bold> start 启动服务 stop 停止服务 reload重载服务');
$climate->br(1)->out('<bold><green>env:</green></bold> 环境区分建议为dev test pre prod');
$climate->br(1)->out('<bold><green>port:</green></bold> 环境端口 可不填写默认读取main.ini配置');
$climate->br(5);
die;
}

Expand Down
29 changes: 9 additions & 20 deletions Http/HttpYafServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -44,7 +49,6 @@ public function run()
$http->start();
}


public function onRequest($request, $response)
{
if ($request->server['request_uri'] == '/favicon.ico') {
Expand Down Expand Up @@ -76,7 +80,6 @@ public function onRequest($request, $response)
$response->end($result);
}


public function onWorkerStart($serv, $worker_id)
{
$config = APPLICATION_PATH.'/conf/main.ini';
Expand All @@ -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";
}


Expand Down Expand Up @@ -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)) {
Expand All @@ -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;
Expand All @@ -198,12 +193,6 @@ public function stop()
unlink($this->pid);
return false;
}



/**
* 重载服务
*/
public function reload()
{
posix_kill($this->getPid(), SIGUSR1);
Expand Down
183 changes: 123 additions & 60 deletions Tcp/SwooleServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit c1e3337

Please sign in to comment.