function StartServSock($RunServer) { cli_set_process_title(SuperProcessName); //这边其实也是也是demon进程 $serv = new swoole_server(uniSockPath, 0, SWOOLE_BASE, SWOOLE_UNIX_STREAM); //维持一个动态数组 实现动态监控server 包含了php的启动路径和停止路径 array('php'=>,'name'=) $serv->runServer = $RunServer; $serv->set(array('worker_num' => 1, 'daemonize' => true)); $serv->on('WorkerStart', function ($serv, $workerId) { //监控周期 $serv->addtimer(1000); }); error_log(PHP_EOL . __LINE__ . PHP_EOL, 3, '/tmp/SuperMaster.log'); //定时器中操作 主要为轮巡 启动服务 $serv->on('Timer', function ($serv, $interval) { StartLogTimer(__LINE__ . 'timer start ' . time()); if (empty($serv->runServer)) { StartLogTimer(__LINE__ . ' ' . 'no server is running ' . PHP_EOL); return; } foreach ($serv->runServer as $serverName) { $ret = system("ps aux | grep " . $serverName['name'] . " | grep master | grep -v grep "); StartLogTimer(__LINE__ . ' cmd is ' . "ps aux | grep " . $serverName['name'] . " | grep master | grep -v grep " . print_r($ret, true)); if (empty($ret)) { //挂了 什么都没有 之后可能要通过数量来获取 //todo StartServ($serverName['php'], 'start', $serverName['name']); StartLogTimer(__LINE__ . date('Y-m-d H:i:s') . ' ' . print_r($serverName, true) . ' server is dead , start to restart'); } else { StartLogTimer(__LINE__ . date('Y-m-d H:i:s') . ' ' . print_r($serverName, true) . ' server is running success'); } } StartLogTimer(__LINE__ . date('Y-m-d H:i:s') . ' ' . print_r($serv->runServer, true) . ' server is dead , start to restart'); }); error_log(PHP_EOL . __LINE__ . PHP_EOL, 3, '/tmp/SuperMaster.log'); $serv->on('connect', function ($serv, $fd, $from_id) { echo "[#" . posix_getpid() . "]\tClient@[{$fd}:{$from_id}]: Connect.\n"; }); error_log(PHP_EOL . __LINE__ . PHP_EOL, 3, '/tmp/SuperMaster.log'); $serv->on('receive', function ($serv, $fd, $from_id, $data) { StartLog(__LINE__ . 'receive data is' . print_r($data, true)); $opData = json_decode($data, true); if ($opData['cmd'] == 'start') { //添加到runserver 还是需要获取路径 存入数组中 if (isset($serv->runServer[$opData['server']])) { //如果已经有了,说明服务已经启动 $serv->send($fd, json_encode(array('r' => 1, "msg" => $opData['server'] . ' is already running'))); StartLog(__LINE__ . 'receive data is' . json_encode(array('r' => 1, "msg" => $opData['server'] . ' is already running'))); return; } //如果没有,则读取配置 todo 回调中是否可以执行 getServerIni? 验证了可以 $retConf = getServerIni($opData['server']); if ($retConf['r'] != 0) { // $serv->send($fd, json_encode($retConf)); return; } else { //正常启动 $phpStart = $retConf['conf']['server']['php']; StartServ($phpStart, 'start', $opData['server']); StartLog(__LINE__ . " {$phpStart} " . STARTBASEPATH . "/lib/Swoole/shell/start.php " . $opData['cmd'] . ' ' . $opData['server']); $serv->runServer[$opData['server']] = array('php' => $phpStart, 'name' => $opData['server']); //添加到runServer中 $serv->send($fd, json_encode(array('r' => 0, 'msg' => 'server start success'))); return; } } elseif ($opData['cmd'] == 'stop') { //从runserver中干掉 $phpStart = $serv->runServer[$opData['server']]['php']; //获取php启动路径 unset($serv->runServer[$opData['server']]); StartLog(__LINE__ . 'THIS RUNSERVER IS' . print_r($serv->runServer, true)); StartServ($phpStart, 'stop', $opData['server']); StartLog(__LINE__ . "{$phpStart} " . STARTBASEPATH . "/lib/Swoole/shell/start.php " . $opData['cmd'] . ' ' . $opData['server']); $serv->send($fd, json_encode(array('r' => 0, 'msg' => 'server stop success'))); return; } elseif ($opData['cmd'] == 'status') { //获取所有服务的状态 StartLog(__LINE__ . json_encode(array('r' => 0, 'msg' => 'server running success ' . print_r($serv->runServer, true)))); $serv->send($fd, json_encode(array('r' => 0, 'data' => $serv->runServer))); return; } elseif ($opData['cmd'] == 'shutdown') { //获取所有服务的状态 StartLog(__LINE__ . json_encode(array('r' => 0, 'msg' => 'server running success ' . print_r($serv->runServer, true)))); $serv->send($fd, json_encode(array('r' => 0, 'data' => $serv->runServer))); //清除所有的runServer序列 unset($serv->runServer); return; } elseif ($opData['cmd'] == 'reload') { //重载所有服务 $phpStart = $serv->runServer[$opData['server']]['php']; //获取php启动路径 StartLog(__LINE__ . "{$phpStart} " . STARTBASEPATH . "/lib/Swoole/shell/start.php " . $opData['cmd'] . ' ' . $opData['server']); StartServ($phpStart, 'reload', $opData['server']); $serv->send($fd, json_encode(array('r' => 0, 'msg' => 'server start success'))); return; } elseif ($opData['cmd'] == 'restart') { //重启所有服务 $phpStart = $serv->runServer[$opData['server']]['php']; //获取php启动路径 //首先unset 防止被自动拉起,然后停止,然后sleep 然后start unset($serv->runServer[$opData['server']]); //从runserver中干掉 StartServ($phpStart, 'stop', $opData['server']); StartLog(__LINE__ . "{$phpStart} " . STARTBASEPATH . "/lib/Swoole/shell/start.php " . ' stop ' . $opData['server']); sleep(2); // exec("$phpStart ".STARTBASEPATH . "/lib/Swoole/shell/start.php ".' start '.$opData['server']);// StartServ($phpStart, 'start', $opData['server']); StartLog(__LINE__ . "{$phpStart} " . STARTBASEPATH . "/lib/Swoole/shell/start.php " . ' start ' . $opData['server']); $serv->runServer[$opData['server']] = array('php' => $phpStart, 'name' => $opData['server']); //添加到runServer中 $serv->send($fd, json_encode(array('r' => 0, 'msg' => 'server restart success'))); return; } }); $serv->on('close', function ($serv, $fd, $from_id) { echo "[#" . posix_getpid() . "]\tClient@[{$fd}:{$from_id}]: Close.\n"; }); $serv->start(); //之后不会再执行任何代码 }
function StartServSock($RunServer) { cli_set_process_title(SuperProcessName); //这边其实也是也是demon进程 $serv = new swoole_server(uniSockPath, 0, SWOOLE_BASE, SWOOLE_UNIX_STREAM); //维持一个动态数组 实现动态监控server 包含了php的启动路径和停止路径 array('php'=>,'name'=) $serv->runServer = $RunServer; $serv->set(array('worker_num' => 1, 'daemonize' => true)); $serv->on('WorkerStart', function ($serv, $workerId) { // 只有当worker_id为0时才添加定时器,避免重复添加 if ($workerId == 0) { // 定时任务, 100ms检测一次任务队列 // 从redis导入队列 /*$serv->tick(100, function($id, $server) { $server->task( json_encode(array( )); ); queueLogTimer(__LINE__ . date('Y-m-d H:i:s') . ' ' . print_r($serv, true) . ' queue'); });*/ // 监控周期 $serv->addtimer(1000); } }); //定时器中操作 主要为轮巡 启动服务 $serv->on('Timer', function ($serv, $interval) { StartLogTimer(__LINE__ . 'timer start ' . time()); if (empty($serv->runServer)) { StartLogTimer(__LINE__ . ' ' . 'no server is running ' . PHP_EOL); return; } switch ($interval) { // for 100ms case 100: queueLogTimer(__LINE__ . date('Y-m-d H:i:s') . ' ' . print_r($serverName, true) . ' queue'); break; // for 1000ms // for 1000ms default: foreach ($serv->runServer as $serverName) { $ret = system('ps aux | grep ' . $serverName['name'] . ' | grep master | grep -v grep '); StartLogTimer(__LINE__ . ' cmd is ' . 'ps aux | grep ' . $serverName['name'] . ' | grep master | grep -v grep ' . print_r($ret, true)); if (empty($ret)) { //挂了 什么都没有 之后可能要通过数量来获取 //todo StartServ($serverName['php'], 'start', $serverName['name']); StartLogTimer(__LINE__ . date('Y-m-d H:i:s') . ' ' . print_r($serverName, true) . ' server is dead , start to restart' . PHP_EOL); } else { StartLogTimer(__LINE__ . date('Y-m-d H:i:s') . ' ' . print_r($serverName, true) . ' server is running success' . PHP_EOL); } } break; } }); $serv->on('connect', function ($serv, $fd, $from_id) { echo '[#' . posix_getpid() . "]\tClient@[{$fd}:{$from_id}]: Connect.\n"; }); $serv->on('receive', function ($serv, $fd, $from_id, $data) { StartLog(__LINE__ . 'receive data is' . print_r($data, true)); $opData = json_decode($data, true); if ($opData['cmd'] == 'start') { //添加到runserver 还是需要获取路径 存入数组中 if (isset($serv->runServer[$opData['server']])) { //如果已经有了,说明服务已经启动 $serv->send($fd, json_encode(array('r' => 1, 'msg' => $opData['server'] . ' is already running'))); StartLog(__LINE__ . 'receive data is' . json_encode(array('r' => 1, 'msg' => $opData['server'] . ' is already running'))); return; } //如果没有,则读取配置 $retConf = getServerIni($opData['server']); if ($retConf['r'] != 0) { // $serv->send($fd, json_encode($retConf)); return; } else { //正常启动 $phpStart = $retConf['conf']['server']['php']; StartServ($phpStart, 'start', $opData['server']); StartLog(__LINE__ . " {$phpStart} " . STARTBASEPATH . '/lib/Swoole/shell/start.php ' . $opData['cmd'] . ' ' . $opData['server']); $serv->runServer[$opData['server']] = array('php' => $phpStart, 'name' => $opData['server']); //添加到runServer中 $serv->send($fd, json_encode(array('r' => 0, 'msg' => "server {$opData['server']} start" . ' [32;40m [SUCCESS] [0m'))); return; } } elseif ($opData['cmd'] == 'stop') { //从runserver中干掉 $phpStart = $serv->runServer[$opData['server']]['php']; //获取php启动路径 unset($serv->runServer[$opData['server']]); StartLog(__LINE__ . 'THIS RUNSERVER IS' . print_r($serv->runServer, true)); StartServ($phpStart, 'stop', $opData['server']); StartLog(__LINE__ . " {$phpStart} " . STARTBASEPATH . '/lib/Swoole/shell/start.php ' . $opData['cmd'] . ' ' . $opData['server']); $serv->send($fd, json_encode(array('r' => 0, 'msg' => "server {$opData['server']} stop " . ' [32;40m [SUCCESS] [0m'))); return; } elseif ($opData['cmd'] == 'status') { //获取所有服务的状态 StartLog(__LINE__ . ' cmd is status ' . print_r($serv->runServer, true)); $serv->send($fd, json_encode(array('r' => 0, 'data' => $serv->runServer))); return; } elseif ($opData['cmd'] == 'shutdown') { //获取所有服务的状态 StartLog(__LINE__ . ' cmd is shutdown ' . print_r($serv->runServer, true)); $serv->send($fd, json_encode(array('r' => 0, 'data' => $serv->runServer))); //清除所有的runServer序列 unset($serv->runServer); return; } elseif ($opData['cmd'] == 'reload') { //重载所有服务 $phpStart = $serv->runServer[$opData['server']]['php']; //获取php启动路径 StartLog(__LINE__ . "{$phpStart} " . STARTBASEPATH . '/lib/Swoole/shell/start.php ' . $opData['cmd'] . ' ' . $opData['server']); StartServ($phpStart, 'reload', $opData['server']); $serv->send($fd, json_encode(array('r' => 0, 'msg' => "server {$opData['server']} reload " . ' [32;40m [SUCCESS] [0m'))); return; } elseif ($opData['cmd'] == 'restart') { //重启所有服务 $phpStart = $serv->runServer[$opData['server']]['php']; //获取php启动路径 //首先unset 防止被自动拉起,然后停止,然后sleep 然后start unset($serv->runServer[$opData['server']]); //从runserver中干掉 StartServ($phpStart, 'stop', $opData['server']); StartLog(__LINE__ . "{$phpStart} " . STARTBASEPATH . '/lib/Swoole/shell/start.php ' . ' stop ' . $opData['server']); sleep(2); // exec("$phpStart ".STARTBASEPATH . "/lib/Swoole/shell/start.php ".' start '.$opData['server']);// StartServ($phpStart, 'start', $opData['server']); StartLog(__LINE__ . "{$phpStart} " . STARTBASEPATH . '/lib/Swoole/shell/start.php ' . ' start ' . $opData['server']); $serv->runServer[$opData['server']] = array('php' => $phpStart, 'name' => $opData['server']); //添加到runServer中 $serv->send($fd, json_encode(array('r' => 0, 'msg' => "server {$opData['server']} restart [32;40m [SUCCESS] [0m"))); return; } }); $serv->on('close', function ($serv, $fd, $from_id) { echo '[#' . posix_getpid() . "]\tClient@[{$fd}:{$from_id}]: Close.\n"; StartLog(__LINE__ . SuperProcessName . ' begin to close '); }); $serv->start(); }