diff --git a/code/extend/minserver/MicroCore_bak.php b/code/extend/minserver/MicroCore_bak.php new file mode 100644 index 0000000..fac2a94 --- /dev/null +++ b/code/extend/minserver/MicroCore_bak.php @@ -0,0 +1,302 @@ +delete('cache'); + } + + static public function reloadDb() + { + Db::close(); + Container::getInstance()->delete('think\DbManager'); + } + + static public function destructConnectSource() + { + self::reloadDb(); + self::reloadRedis(); + } +} + + + +class QueueManage +{ + public $strQueueName = ''; + + static private $arrObj = []; + + static public function getInstance($intIndex = 1, $strQueueName = '') + { + $strKey = $intIndex . '-' . $strQueueName; + + if (!key_exists($strKey, self::$arrObj)) { + self::$arrObj[$strKey] = new self($intIndex, $strQueueName); + } + return self::$arrObj[$strKey]; + } + + public function __construct($intIndex = 1, $strQueueName = '') + { + if ($strQueueName == '') { + $arrConfig = config('task.queue'); + $strQueueName = $arrConfig[$intIndex]['name']; + } + + $this->strQueueName = $strQueueName; + } + + public function get() + { + return Cache::store('redis')->lpop($this->strQueueName); + } + + public function set($arrData) + { + $strData = json_encode($arrData); + return Cache::store('redis')->rpush($this->strQueueName, $strData); + } +} + + +class Rout +{ + static public function httpDispense(\Swoole\Http\Request $Request, \Swoole\Http\Response $Response) + { + $Response->status(999, 'Hei Guys ~'); + $Response->header("Content-Type", "text/html; charset=utf-8"); + $Response->end("

Hello reptile~. #" . rand(1000, 9999) . "

"); + } + + static public function tcpDispense(\Swoole\Server $server, $fd, $reactor_id, $mixedData) {} +} + + + +class ScheduledTasks +{ + static public function hander($arrProcessTask = []) + { + ProcessSanitizer::destructConnectSource(); + + call_user_func($arrProcessTask['callback'], $arrProcessTask['param']); + + sleep($arrProcessTask['execution_interval']); + } +} + + + +class ServerCore +{ + private $arrConfig; + + private static $obj; + + private $Server; + + static $Process = NULL; + + static function instance($arrConfig) + { + if (self::$obj == null) { + self::$obj = new self($arrConfig); + } + return self::$obj; + } + + public function __construct($arrConfig) + { + $this->arrConfig = $arrConfig; + $this->createServer(); + } + + private function serHttpConfig() + { + $this->Server->set($this->filterConfig()); + $this->Server->on('request', [Rout::class, 'httpDispense']); + } + + private function filterConfig() + { + $arrConfig = $this->arrConfig; + unset($arrConfig['host']); + unset($arrConfig['port']); + unset($arrConfig['mode']); + unset($arrConfig['sockType']); + unset($arrConfig['server_type']); + return $arrConfig; + } + + private function serTcpConfig() + { + $this->Server->set($this->filterConfig()); + $this->Server->on('receive', [Rout::class, 'tcpDispense']); + } + + private function createServer() + { + switch ($this->arrConfig['server_type']) { + case 'HTTP': + $this->Server = new \Swoole\Http\Server($this->arrConfig['host'], $this->arrConfig['port']); + $this->serHttpConfig(); + break; + default: + $this->Server = new \Swoole\Server($this->arrConfig['host'], $this->arrConfig['port'], $this->arrConfig['mode'], $this->arrConfig['sockType']); + $this->serTcpConfig(); + break; + } + + $this->Server->on('task', [$this, 'onTask']); + + $this->Server->on('finish', [$this, 'onFinish']); + } + + public function getServer() + { + return $this->Server; + } + + public function onTask($Http, $task_id, $from_id, $arrData) + { + $mixedResult = call_user_func_array($arrData[0], $arrData[1]); + + $Http->finish($mixedResult); + } + + public function onFinish($Http, $task_id, $mixedData) {} +} + + + +class ServerManage +{ + static $HttpService; + + static $arrDynamicConfig = []; + + public function __init($arrArgvs = []) + { + $this->setConfigDaemonize($arrArgvs[0]['daemonize'] ?? 'false'); + } + + private function setConfigDaemonize($strMode) + { + self::$arrDynamicConfig['daemonize'] = (bool)$strMode; + } + + private function getPid() + { + return file_exists(config('task.service.pid_file')) ? file_get_contents(config('task.service.pid_file')) : NULL; + } + + public function start($arrArgvs = []) + { + $this->__init($arrArgvs); + + $intPid = (int)$this->getPid(); + + if ($intPid > 0 && \Swoole\Process::kill($intPid, SIG_DFL)) { + echo " Service is \e[0;32mRunning\e[0m ! " . PHP_EOL; + exit; + } + echo " Execute the \e[0;32m start\e[0m command ...... " . PHP_EOL; + + $this->createService(); + + $this->addProcessList(); + + $this->addScheduledTasks(); + + self::$HttpService->getServer()->start(); + } + + public function stop($arrArgvs = []) + { + $this->__init($arrArgvs); + + $intPid = (int)$this->getPid(); + + if ($intPid <= 0) { + echo " Service is \e[0;31mStop\e[0m ! " . PHP_EOL; + } else { + echo " Execute the \e[0;32m stop\e[0m command ...... " . PHP_EOL; + \Swoole\Process::kill($intPid, SIGTERM); + echo " Service is \e[0;31mStop\e[0m ! " . PHP_EOL; + } + } + + public function status($arrArgvs = []) + { + $this->__init($arrArgvs); + + $intPid = (int)$this->getPid(); + + if ($intPid <= 0) { + echo " Service is \e[0;31mStop\e[0m ! " . PHP_EOL; + } else if (\Swoole\Process::kill($intPid, SIG_DFL)) { + echo " Service is \e[0;32mRunning\e[0m ! " . PHP_EOL; + } else { + echo " Service is \e[0;31mStop\e[0m ! " . PHP_EOL; + } + } + + public function restart($arrArgvs = []) + { + $this->__init($arrArgvs); + + $intPid = (int)$this->getPid(); + + if ($intPid <= 0) { + echo " \e[0;31m Get Pid Error\e[0m !" . PHP_EOL; + echo " Execute the \e[0;32m start\e[0m command ...... " . PHP_EOL; + $this->start(); + } else { + echo " Execute the \e[0;32m restart\e[0m command ...... " . PHP_EOL; + \Swoole\Process::kill($intPid, SIGUSR1); + } + } + + private function addScheduledTasks() + { + if (config('task.scheduled_tasks_pool')) { + foreach (config('task.scheduled_tasks_pool') as $arrProcessTask) { + if ($arrProcessTask['status']) { + $Process = new \Swoole\Process(function () use ($arrProcessTask) { + call_user_func([ScheduledTasks::class, 'hander'], $arrProcessTask); + }); + self::$HttpService->getServer()->addProcess($Process); + } + } + } + } + + private function addProcessList() + { + if (config('task.process_pool')) { + foreach (config('task.process_pool') as $v) { + for ($i = 0; $i < $v['Num']; $i++) { + $Process = new \Swoole\Process($v['callback']); + + self::$HttpService->getServer()->addProcess($Process); + } + } + } + } + + private function createService() + { + $arrHttpServiceConfig = array_merge(config('task.service'), self::$arrDynamicConfig); + + self::$HttpService = ServerCore::instance($arrHttpServiceConfig); + } +} diff --git a/code/extend/minserver/QueueManage.php b/code/extend/minserver/QueueManage.php index d5c8b0a..8e856d4 100644 --- a/code/extend/minserver/QueueManage.php +++ b/code/extend/minserver/QueueManage.php @@ -10,6 +10,18 @@ class QueueManage { public $strQueueName = ''; + static private $arrObj = []; + + static public function getInstance($intIndex = 1, $strQueueName = '') + { + $strKey = $intIndex . '-' . $strQueueName; + + if (!key_exists($strKey, self::$arrObj)) { + self::$arrObj[$strKey] = new self($intIndex, $strQueueName); + } + return self::$arrObj[$strKey]; + } + public function __construct($intIndex = 1, $strQueueName = '') { if ($strQueueName == '') {