diff --git a/bin/easyswoole b/bin/easyswoole index 3ab64eb8..36ef7e64 100644 --- a/bin/easyswoole +++ b/bin/easyswoole @@ -1,6 +1,8 @@ #!/usr/bin/env php = 4.2.6 (current: {$version})\n"); -} - -use EasySwoole\EasySwoole\Core; -use EasySwoole\EasySwoole\SysConst; -use EasySwoole\EasySwoole\Config; -use EasySwoole\EasySwoole\ServerManager; -use EasySwoole\EasySwoole\Console\Client; - -class Install -{ - public static function init() - { - Core::getInstance(); - //强制更新easyswoole bin管理文件 - if(is_file(EASYSWOOLE_ROOT . '/easyswoole')){ - unlink(EASYSWOOLE_ROOT . '/easyswoole'); - } - $path = '.'.str_replace(EASYSWOOLE_ROOT,'',__FILE__); - file_put_contents(EASYSWOOLE_ROOT . '/easyswoole',"setIsDev(false); - } - Core::getInstance()->initialize(); - $conf = Config::getInstance(); - if(in_array("d",$commandList) || in_array("daemonize",$commandList)){ - $conf->setConf("MAIN_SERVER.SETTING.daemonize", true); - } - //创建主服务 - Core::getInstance()->createServer(); - $serverType = $conf->getConf('MAIN_SERVER.SERVER_TYPE'); - switch ($serverType){ - case 1:{ - $serverType = 'SWOOLE_SERVER'; - break; - } - case 2:{ - $serverType = 'SWOOLE_WEB'; - break; - } - case 3:{ - $serverType = 'SWOOLE_WEB_SOCKET'; - break; - } - default:{ - $serverType = 'UNKNOWN'; - } - } - Install::showTag('main server', $serverType); - Install::showTag('listen address', $conf->getConf('MAIN_SERVER.LISTEN_ADDRESS')); - Install::showTag('listen port', $conf->getConf('MAIN_SERVER.PORT')); - - $list = ServerManager::getInstance()->getSubServerRegister(); - $index = 1; - foreach ($list as $serverName => $item){ - $type = $item['type'] % 2 > 0 ? 'SWOOLE_TCP' : 'SWOOLE_UDP'; - Install::showTag('sub server'.$index, "{$serverName} => {$type}@{$item['listenAddress']}:{$item['port']}"); - $index++; - } - - $ips = swoole_get_local_ip(); - foreach ($ips as $eth => $val){ - Install::showTag('ip@'.$eth, $val); - } - $data = $conf->getConf('MAIN_SERVER.SETTING'); - foreach ($data as $key => $datum){ - Install::showTag($key, (string)$datum); - } - $user = $conf->getConf('MAIN_SERVER.SETTING.user'); - if(empty($user)){ - $user = get_current_user(); - } - Install::showTag('run at user', $user); - $daemonize = $conf->getConf("MAIN_SERVER.SETTING.daemonize"); - if($daemonize){ - $daemonize = 'true'; - }else{ - $daemonize = 'false'; - } - Install::showTag('daemonize', $daemonize); - Install::showTag('swoole version', phpversion('swoole')); - Install::showTag('php version', phpversion()); - Install::showTag('easy swoole', SysConst::EASYSWOOLE_VERSION); - Install::showTag('develop/produce', $mode); - Install::showTag('temp dir', EASYSWOOLE_TEMP_DIR); - Install::showTag('log dir', EASYSWOOLE_LOG_DIR); - Core::getInstance()->start(); - break; - } - - case 'stop':{ - $force = false; - if(in_array('force',$commandList)){ - $force = true; - } - if(in_array('produce',$commandList)){ - Core::getInstance()->setIsDev(false); - } - Core::getInstance()->initialize(); - $Conf = Config::getInstance(); - $pidFile = $Conf->getConf("MAIN_SERVER.SETTING.pid_file"); - if (file_exists($pidFile)) { - $pid = file_get_contents($pidFile); - if (!swoole_process::kill($pid, 0)) { - echo "PID :{$pid} not exist \n"; - return false; - } - if ($force) { - swoole_process::kill($pid, SIGKILL); - } else { - swoole_process::kill($pid); - } - //等待5秒 - $time = time(); - $flag = false; - while (true) { - usleep(1000); - if (!swoole_process::kill($pid, 0)) { - echo "server stop at " . date("y-m-d h:i:s") . "\n"; - if (is_file($pidFile)) { - unlink($pidFile); - } - $flag = true; - break; - } else { - if (time() - $time > 5) { - echo "stop server fail.try -f again \n"; - break; - } - } - } - return $flag; - } else { - echo "PID file does not exist, please check whether to run in the daemon mode!\n"; - return false; - } - break; - } - - case 'reload':{ - $all = false; - if(in_array('all',$commandList)){ - $all = true; - } - if(in_array('produce',$commandList)){ - Core::getInstance()->setIsDev(false); - } - Core::getInstance()->initialize(); - $Conf =Config::getInstance(); - $pidFile = $Conf->getConf("MAIN_SERVER.SETTING.pid_file"); - if (file_exists($pidFile)) { - if (!$all) { - $sig = SIGUSR2; - Install::showTag('reloadType',"only-task"); - } else { - $sig = SIGUSR1; - Install::showTag('reloadType',"all-worker"); - } - - Install::opCacheClear(); - $pid = file_get_contents($pidFile); - if (!swoole_process::kill($pid, 0)) { - echo "pid :{$pid} not exist \n"; - return; - } - swoole_process::kill($pid, $sig); - echo "send server reload command at " . date("y-m-d h:i:s") . "\n"; - } else { - echo "PID file does not exist, please check whether to run in the daemon mode!\n"; - } - break; - } - - case 'console':{ - Core::getInstance()->initialize(); - if(in_array('produce',$commandList)){ - Core::getInstance()->setIsDev(false); - } - $conf = Config::getInstance()->getConf('CONSOLE'); - $client = new Client($conf['HOST'],$conf['PORT']); - if($client->connect()){ - swoole_event_add(STDIN,function()use($client){ - $ret = trim(fgets(STDIN)); - if(!empty($ret)){ - $client->sendCommand($ret); - } - }); - }else{ - fwrite(STDOUT, "connect to tcp://".$conf['HOST'].":".$conf['PORT']." fail \n"); - } - break; - } - - case 'help': - default:{ - $com = array_shift($commandList); - if($com == 'start'){ - Install::showHelpForStart(); - }else if($com == 'stop'){ - Install::showHelpForStop(); - }else if($com == 'reload'){ - Install::showHelpForReload(); - }else{ - Install::showHelp(); - } - break; - } -} - +$args = $argv; +//trim first command +array_shift($args); +$ret = CommandRunner::getInstance()->run($args); +if(!empty($ret)){ + echo $ret."\n"; +} \ No newline at end of file diff --git a/src/Command/CommandContainer.php b/src/Command/CommandContainer.php new file mode 100644 index 00000000..c205d3e6 --- /dev/null +++ b/src/Command/CommandContainer.php @@ -0,0 +1,48 @@ +container[strtolower($command->commandName())] = $command; + } + + function get($key): ?CommandInterface + { + $key = strtolower($key); + if (isset($this->container[$key])) { + return $this->container[$key]; + } else { + return null; + } + } + + function getCommandList() + { + return array_keys($this->container); + } + + function hook($commandName, array $args):?string + { + $handler = $this->get($commandName); + if($handler){ + return $handler->exec($args); + } + return null; + } +} \ No newline at end of file diff --git a/src/Command/CommandInterface.php b/src/Command/CommandInterface.php new file mode 100644 index 00000000..03b7ec86 --- /dev/null +++ b/src/Command/CommandInterface.php @@ -0,0 +1,17 @@ +set(new Help()); + CommandContainer::getInstance()->set(new Install()); + CommandContainer::getInstance()->set(new Start()); + CommandContainer::getInstance()->set(new Stop()); + CommandContainer::getInstance()->set(new Reload()); + CommandContainer::getInstance()->set(new Console()); + } + + function run(array $args):?string + { + $command = array_shift($args); + if(empty($command)){ + $command = 'help'; + }else if($command != 'install'){ + Core::getInstance()->initialize(); + } + if(!CommandContainer::getInstance()->get($command)){ + $command = 'help'; + } + return CommandContainer::getInstance()->hook($command,$args); + } +} \ No newline at end of file diff --git a/src/Command/DefaultCommand/Console.php b/src/Command/DefaultCommand/Console.php new file mode 100644 index 00000000..f16a5ad8 --- /dev/null +++ b/src/Command/DefaultCommand/Console.php @@ -0,0 +1,62 @@ +initialize(); + if(in_array('produce',$args)){ + Core::getInstance()->setIsDev(false); + } + $conf = Config::getInstance()->getConf('CONSOLE'); + $client = new Client($conf['HOST'],$conf['PORT']); + if($client->connect()){ + swoole_event_add(STDIN,function()use($client){ + $ret = trim(fgets(STDIN)); + if(!empty($ret)){ + $client->sendCommand($ret); + } + }); + }else{ + return "connect to tcp://".$conf['HOST'].":".$conf['PORT']." fail "; + } + return null; + } + + public function help(array $args): ?string + { + // TODO: Implement help() method. + $logo = Utility::easySwooleLog(); + return $logo.<<help($args); + } else { + $actionName = $args[0]; + array_shift($args); + $call = CommandContainer::getInstance()->get($actionName); + if ($call instanceof CommandInterface) { + return $call->help($args); + } else { + return "no help message for command {$actionName} was found"; + } + } + } + + public function help(array $args): ?string + { + // TODO: Implement help() method. + $allCommand = implode(PHP_EOL, CommandContainer::getInstance()->getCommandList()); + $logo = Utility::easySwooleLog(); + return $logo.<<setIsDev(false); + } + Core::getInstance()->initialize(); + $Conf = Config::getInstance(); + $res = ''; + $pidFile = $Conf->getConf("MAIN_SERVER.SETTING.pid_file"); + if (file_exists($pidFile)) { + if (!$all) { + $sig = SIGUSR2; + $res = $res.Utility::displayItem('reloadType',"only-task")."\n"; + } else { + $sig = SIGUSR1; + $res = $res.Utility::displayItem('reloadType',"all-worker")."\n"; + } + Utility::opCacheClear(); + $pid = file_get_contents($pidFile); + if (!\swoole_process::kill($pid, 0)) { + return "pid :{$pid} not exist "; + } + \swoole_process::kill($pid, $sig); + return $res. "send server reload command at " . date("y-m-d h:i:s"); + } else { + return "PID file does not exist, please check whether to run in the daemon mode!"; + } + } + + public function help(array $args): ?string + { + // TODO: Implement help() method. + $logo = Utility::easySwooleLog(); + return $logo.<<setIsDev(false); + } + $conf = Config::getInstance(); + if(in_array("d",$args) || in_array("daemonize",$args)){ + $conf->setConf("MAIN_SERVER.SETTING.daemonize", true); + } + //create main Server + Core::getInstance()->createServer(); + $serverType = $conf->getConf('MAIN_SERVER.SERVER_TYPE'); + switch ($serverType){ + case EASYSWOOLE_SERVER:{ + $serverType = 'SWOOLE_SERVER'; + break; + } + case EASYSWOOLE_WEB_SERVER:{ + $serverType = 'SWOOLE_WEB'; + break; + } + case EASYSWOOLE_WEB_SOCKET_SERVER:{ + $serverType = 'SWOOLE_WEB_SOCKET'; + break; + } + default:{ + $serverType = 'UNKNOWN'; + } + } + $response = $response.Utility::displayItem('main server',$serverType)."\n"; + $response = $response.Utility::displayItem('listen address',$conf->getConf('MAIN_SERVER.LISTEN_ADDRESS'))."\n"; + $response = $response.Utility::displayItem('listen port', $conf->getConf('MAIN_SERVER.PORT'))."\n"; + $list = ServerManager::getInstance()->getSubServerRegister(); + $index = 1; + foreach ($list as $serverName => $item){ + $type = $item['type'] % 2 > 0 ? 'SWOOLE_TCP' : 'SWOOLE_UDP'; + $response = $response.Utility::displayItem('sub server'.$index,"{$serverName} => {$type}@{$item['listenAddress']}:{$item['port']}")."\n"; + $index++; + } + $ips = swoole_get_local_ip(); + foreach ($ips as $eth => $val){ + $response = $response.Utility::displayItem('ip@'.$eth,$val)."\n"; + } + $data = $conf->getConf('MAIN_SERVER.SETTING'); + foreach ($data as $key => $datum){ + $response = $response.Utility::displayItem($key, (string)$datum)."\n"; + } + $user = $conf->getConf('MAIN_SERVER.SETTING.user'); + if(empty($user)){ + $user = get_current_user(); + } + $response = $response.Utility::displayItem('run at user', $user)."\n"; + $daemonize = $conf->getConf("MAIN_SERVER.SETTING.daemonize"); + if($daemonize){ + $daemonize = 'true'; + }else{ + $daemonize = 'false'; + } + $response = $response.Utility::displayItem('daemonize', $daemonize)."\n"; + $response = $response.Utility::displayItem('swoole version', phpversion('swoole'))."\n"; + $response = $response.Utility::displayItem('php version', phpversion())."\n"; + $response = $response.Utility::displayItem('easy swoole', SysConst::EASYSWOOLE_VERSION)."\n"; + $response = $response.Utility::displayItem('develop/produce', $mode)."\n"; + $response = $response.Utility::displayItem('temp dir', EASYSWOOLE_TEMP_DIR)."\n"; + $response = $response.Utility::displayItem('log dir', EASYSWOOLE_LOG_DIR)."\n"; + echo $response; + Core::getInstance()->start(); + return null; + } + + public function help(array $args): ?string + { + // TODO: Implement help() method. + $logo = Utility::easySwooleLog(); + return $logo.<<setIsDev(false); + } + Core::getInstance()->initialize(); + $Conf = Config::getInstance(); + $pidFile = $Conf->getConf("MAIN_SERVER.SETTING.pid_file"); + if (file_exists($pidFile)) { + $pid = file_get_contents($pidFile); + if (!\swoole_process::kill($pid, 0)) { + return "PID :{$pid} not exist "; + } + if ($force) { + \swoole_process::kill($pid, SIGKILL); + } else { + \swoole_process::kill($pid); + } + //等待5秒 + $time = time(); + while (true) { + usleep(1000); + if (!\swoole_process::kill($pid, 0)) { + if (is_file($pidFile)) { + unlink($pidFile); + } + return "server stop at " . date("y-m-d h:i:s") ; + break; + } else { + if (time() - $time > 5) { + return "stop server fail.try -f again "; + break; + } + } + } + return 'stop server fail'; + } else { + return "PID file does not exist, please check whether to run in the daemon mode!"; + } + } + + public function help(array $args): ?string + { + // TODO: Implement help() method. + $logo = Utility::easySwooleLog(); + return $logo.<<set(new Help()); - CommandContainer::getInstance()->set(new Auth()); - CommandContainer::getInstance()->set(new Server()); + ModuleContainer::getInstance()->set(new Help()); + ModuleContainer::getInstance()->set(new Auth()); + ModuleContainer::getInstance()->set(new Server()); } public function __registerTcpServer() diff --git a/src/Console/ConsoleTcpController.php b/src/Console/ConsoleTcpController.php index 0bd55e52..5a567ea5 100644 --- a/src/Console/ConsoleTcpController.php +++ b/src/Console/ConsoleTcpController.php @@ -41,6 +41,6 @@ function onRequest(?string $actionName): bool */ protected function actionNotFound(?string $actionName) { - CommandContainer::getInstance()->hook($actionName, $this->caller(), $this->response()); + ModuleContainer::getInstance()->hook($actionName, $this->caller(), $this->response()); } } \ No newline at end of file diff --git a/src/Console/DefaultCommand/Auth.php b/src/Console/DefaultModule/Auth.php similarity index 92% rename from src/Console/DefaultCommand/Auth.php rename to src/Console/DefaultModule/Auth.php index f127fdfc..0ef22871 100644 --- a/src/Console/DefaultCommand/Auth.php +++ b/src/Console/DefaultModule/Auth.php @@ -6,10 +6,10 @@ * Time: 10:08 PM */ -namespace EasySwoole\EasySwoole\Console\DefaultCommand; +namespace EasySwoole\EasySwoole\Console\DefaultModule; -use EasySwoole\EasySwoole\Console\CommandInterface; +use EasySwoole\EasySwoole\Console\ModuleInterface; use EasySwoole\EasySwoole\Console\ConsoleService; use EasySwoole\Socket\Bean\Caller; use EasySwoole\Socket\Bean\Response; @@ -19,7 +19,7 @@ * Class Auth * @package EasySwoole\EasySwoole\Console\DefaultCommand */ -class Auth implements CommandInterface +class Auth implements ModuleInterface { public function moduleName(): string { diff --git a/src/Console/DefaultCommand/Help.php b/src/Console/DefaultModule/Help.php similarity index 75% rename from src/Console/DefaultCommand/Help.php rename to src/Console/DefaultModule/Help.php index e64238b1..9e8d7958 100644 --- a/src/Console/DefaultCommand/Help.php +++ b/src/Console/DefaultModule/Help.php @@ -6,10 +6,10 @@ * Time: 9:59 PM */ -namespace EasySwoole\EasySwoole\Console\DefaultCommand; +namespace EasySwoole\EasySwoole\Console\DefaultModule; -use EasySwoole\EasySwoole\Console\CommandContainer; -use EasySwoole\EasySwoole\Console\CommandInterface; +use EasySwoole\EasySwoole\Console\ModuleContainer; +use EasySwoole\EasySwoole\Console\ModuleInterface; use EasySwoole\Socket\Bean\Caller; use EasySwoole\Socket\Bean\Response; @@ -18,7 +18,7 @@ * Class Help * @package EasySwoole\EasySwoole\Console\DefaultCommand */ -class Help implements CommandInterface +class Help implements ModuleInterface { function moduleName(): string { @@ -39,8 +39,8 @@ public function exec(Caller $caller, Response $response) $this->help($caller, $response); } else { $actionName = $args[0]; - $call = CommandContainer::getInstance()->get($actionName); - if ($call instanceof CommandInterface) { + $call = ModuleContainer::getInstance()->get($actionName); + if ($call instanceof ModuleInterface) { $call->help($caller, $response); } else { $response->setMessage("no help message for command {$actionName} was found."); @@ -50,7 +50,7 @@ public function exec(Caller $caller, Response $response) public function help(Caller $caller, Response $response) { - $allCommand = implode(PHP_EOL, CommandContainer::getInstance()->getCommandList()); + $allCommand = implode(PHP_EOL, ModuleContainer::getInstance()->getCommandList()); $help = <<container[strtolower($command->moduleName())] = $command; } - function get($key): ?CommandInterface + function get($key): ?ModuleInterface { $key = strtolower($key); if (isset($this->container[$key])) { @@ -52,8 +52,8 @@ function getCommandList() */ function hook($actionName, Caller $caller, Response $response) { - $call = CommandContainer::getInstance()->get($actionName); - if ($call instanceof CommandInterface) { + $call = ModuleContainer::getInstance()->get($actionName); + if ($call instanceof ModuleInterface) { $call->exec($caller, $response); } else { $response->setMessage("action {$actionName} miss"); diff --git a/src/Console/CommandInterface.php b/src/Console/ModuleInterface.php similarity index 93% rename from src/Console/CommandInterface.php rename to src/Console/ModuleInterface.php index 370ef1e7..c2552c52 100644 --- a/src/Console/CommandInterface.php +++ b/src/Console/ModuleInterface.php @@ -12,7 +12,7 @@ use EasySwoole\Socket\Bean\Caller; use EasySwoole\Socket\Bean\Response; -interface CommandInterface +interface ModuleInterface { public function moduleName():string; public function exec(Caller $caller,Response $response); diff --git a/src/Resource/Config.php b/src/Resource/Config.tpl similarity index 100% rename from src/Resource/Config.php rename to src/Resource/Config.tpl