From 73b6b8045d6ba5bd225e1908b633ea0db3307118 Mon Sep 17 00:00:00 2001 From: crazywhalecc Date: Sun, 13 Mar 2022 22:47:11 +0800 Subject: [PATCH] enhancement for process state --- src/ZM/Event/SwooleEvent/OnMessage.php | 3 - src/ZM/Event/SwooleEvent/OnPipeMessage.php | 4 +- src/ZM/global_functions.php | 137 ++++++++++++++------- 3 files changed, 97 insertions(+), 47 deletions(-) diff --git a/src/ZM/Event/SwooleEvent/OnMessage.php b/src/ZM/Event/SwooleEvent/OnMessage.php index 1238ba9d..9cc8ffe3 100644 --- a/src/ZM/Event/SwooleEvent/OnMessage.php +++ b/src/ZM/Event/SwooleEvent/OnMessage.php @@ -25,9 +25,6 @@ use ZM\Event\SwooleEvent; */ class OnMessage implements SwooleEvent { - /** - * @noinspection PhpUnreachableStatementInspection - */ public function onCall($server, Frame $frame) { Console::debug("Calling Swoole \"message\" from fd=" . $frame->fd . ": " . TermColor::ITALIC . $frame->data . TermColor::RESET); unset(Context::$context[Coroutine::getCid()]); diff --git a/src/ZM/Event/SwooleEvent/OnPipeMessage.php b/src/ZM/Event/SwooleEvent/OnPipeMessage.php index 2a5f846a..0a4f541d 100644 --- a/src/ZM/Event/SwooleEvent/OnPipeMessage.php +++ b/src/ZM/Event/SwooleEvent/OnPipeMessage.php @@ -10,7 +10,7 @@ use Swoole\Server; use ZM\Annotation\Swoole\SwooleHandler; use ZM\Console\Console; use ZM\Event\SwooleEvent; -use ZM\Utils\Manager\ProcessManager; +use ZM\Utils\Manager\WorkerManager; /** * Class OnPipeMessage @@ -22,7 +22,7 @@ class OnPipeMessage implements SwooleEvent public function onCall(Server $server, $src_worker_id, $data) { $data = json_decode($data, true); try { - ProcessManager::workerAction($src_worker_id, $data); + WorkerManager::workerAction($src_worker_id, $data); } catch (Exception $e) { $error_msg = $e->getMessage() . " at " . $e->getFile() . "(" . $e->getLine() . ")"; Console::error(zm_internal_errcode("E00021") . "Uncaught exception " . get_class($e) . " when calling \"pipeMessage\": " . $error_msg); diff --git a/src/ZM/global_functions.php b/src/ZM/global_functions.php index b03bc259..12a62cea 100644 --- a/src/ZM/global_functions.php +++ b/src/ZM/global_functions.php @@ -21,7 +21,8 @@ use Swoole\Coroutine\System; use ZM\Context\ContextInterface; -function getClassPath($class_name) { +function getClassPath($class_name) +{ $dir = str_replace("\\", "/", $class_name); $dir2 = DataProvider::getSourceRootDir() . "/src/" . $dir . ".php"; //echo "@@@".$dir2.PHP_EOL; @@ -29,11 +30,13 @@ function getClassPath($class_name) { if (file_exists($dir2)) return $dir2; else return null; } + /** * 检查炸毛框架运行的环境 * @internal */ -function _zm_env_check() { +function _zm_env_check() +{ if (!extension_loaded("swoole")) die(zm_internal_errcode("E00001") . "Can not find swoole extension.\n"); if (version_compare(SWOOLE_VERSION, "4.5.0") == -1) die(zm_internal_errcode("E00002") . "You must install swoole version >= 4.5.0 !"); if (version_compare(PHP_VERSION, "7.2") == -1) die(zm_internal_errcode("E00003") . "PHP >= 7.2 required."); @@ -49,7 +52,8 @@ function _zm_env_check() { * @param bool $ban_comma * @return array */ -function explodeMsg($msg, $ban_comma = false): array { +function explodeMsg($msg, $ban_comma = false): array +{ $msg = str_replace(" ", "\n", trim($msg)); if (!$ban_comma) { //$msg = str_replace(",", "\n", $msg); @@ -65,13 +69,15 @@ function explodeMsg($msg, $ban_comma = false): array { } /** @noinspection PhpUnused */ -function unicode_decode($str) { +function unicode_decode($str) +{ return preg_replace_callback('/\\\\u([0-9a-f]{4})/i', function ($matches) { return mb_convert_encoding(pack("H*", $matches[1]), "UTF-8", "UCS-2BE"); }, $str); } -function matchPattern($pattern, $context): bool { +function matchPattern($pattern, $context): bool +{ if (mb_substr($pattern, 0, 1) == "" && mb_substr($context, 0, 1) == "") return true; if ('*' == mb_substr($pattern, 0, 1) && "" != mb_substr($pattern, 1, 1) && "" == mb_substr($context, 0, 1)) @@ -83,7 +89,8 @@ function matchPattern($pattern, $context): bool { return false; } -function split_explode($del, $str, $divide_en = false): array { +function split_explode($del, $str, $divide_en = false): array +{ $str = explode($del, $str); for ($i = 0; $i < mb_strlen($str[0]); $i++) { if ( @@ -115,7 +122,8 @@ function split_explode($del, $str, $divide_en = false): array { return $ls == [] ? [""] : $ls; } -function matchArgs($pattern, $context) { +function matchArgs($pattern, $context) +{ $result = []; if (matchPattern($pattern, $context)) { if (mb_strpos($pattern, "*") === false) return []; @@ -145,19 +153,23 @@ function matchArgs($pattern, $context) { } else return false; } -function connectIsQQ(): bool { +function connectIsQQ(): bool +{ return ctx()->getConnection()->getName() == 'qq'; } -function connectIsDefault(): bool { +function connectIsDefault(): bool +{ return ctx()->getConnection()->getName() == 'default'; } -function connectIs($type): bool { +function connectIs($type): bool +{ return ctx()->getConnection()->getName() == $type; } -function getAnnotations(): array { +function getAnnotations(): array +{ $s = debug_backtrace()[1]; //echo json_encode($s, 128|256); $list = []; @@ -172,9 +184,10 @@ function getAnnotations(): array { return $list; } -function set_coroutine_params($array) { +function set_coroutine_params($array) +{ $cid = Co::getCid(); - if ($cid == -1) die(zm_internal_errcode("E00061")."Cannot set coroutine params at none coroutine mode."); + if ($cid == -1) die(zm_internal_errcode("E00061") . "Cannot set coroutine params at none coroutine mode."); if (isset(Context::$context[$cid])) Context::$context[$cid] = array_merge(Context::$context[$cid], $array); else Context::$context[$cid] = $array; foreach (Context::$context as $c => $v) { @@ -185,14 +198,16 @@ function set_coroutine_params($array) { /** * @return ContextInterface|null */ -function context(): ?ContextInterface { +function context(): ?ContextInterface +{ return ctx(); } /** * @return ContextInterface|null */ -function ctx(): ?ContextInterface { +function ctx(): ?ContextInterface +{ $cid = Co::getCid(); $c_class = ZMConfig::get("global", "context_class"); if (isset(Context::$context[$cid])) { @@ -207,39 +222,47 @@ function ctx(): ?ContextInterface { } } -function onebot_target_id_name($message_type): string { +function onebot_target_id_name($message_type): string +{ return ($message_type == "group" ? "group_id" : "user_id"); } -function zm_sleep($s = 1): bool { +function zm_sleep($s = 1): bool +{ if (Coroutine::getCid() != -1) System::sleep($s); else usleep($s * 1000 * 1000); return true; } -function zm_exec($cmd): array { +function zm_exec($cmd): array +{ return System::exec($cmd); } -function zm_cid() { +function zm_cid() +{ return Co::getCid(); } -function zm_yield() { +function zm_yield() +{ Co::yield(); } -function zm_resume(int $cid) { +function zm_resume(int $cid) +{ Co::resume($cid); } -function zm_timer_after($ms, callable $callable) { +function zm_timer_after($ms, callable $callable) +{ Swoole\Timer::after($ms, function () use ($callable) { call_with_catch($callable); }); } -function call_with_catch($callable) { +function call_with_catch($callable) +{ try { $callable(); } catch (Exception $e) { @@ -253,7 +276,8 @@ function call_with_catch($callable) { } } -function zm_timer_tick($ms, callable $callable) { +function zm_timer_tick($ms, callable $callable) +{ if (zm_cid() === -1) { return go(function () use ($ms, $callable) { Console::debug("Adding extra timer tick of " . $ms . " ms"); @@ -268,24 +292,39 @@ function zm_timer_tick($ms, callable $callable) { } } -function zm_go(callable $callable) { +function zm_go(callable $callable) +{ call_with_catch($callable); } -function zm_data_hash($v): string { +function zm_data_hash($v): string +{ return md5($v["user_id"] . "^" . $v["self_id"] . "^" . $v["message_type"] . "^" . ($v[$v["message_type"] . "_id"] ?? $v["user_id"])); } -function server(): ?Server { +function server(): ?Server +{ return Framework::$server; } +/** + * 获取缓存当前框架pid的临时目录 + * @return string + */ +function _zm_pid_dir(): string +{ + global $_ZM_HASH; + if (!isset($_ZM_HASH)) $_ZM_HASH = md5(DataProvider::getWorkingDir()); + return '/tmp/.zm_' . $_ZM_HASH; +} + /** * @return OneBotV11 * @throws RobotNotFoundException * @throws ZMException */ -function bot() { +function bot() +{ if (($conn = LightCacheInside::get("connect", "conn_fd")) == -2) { return OneBotV11::getRandom(); } elseif ($conn != -1) { @@ -302,7 +341,8 @@ function bot() { * @return array * @author 854854321 */ -function getAllFdByConnectType(string $type = 'default'): array { +function getAllFdByConnectType(string $type = 'default'): array +{ $fds = []; foreach (ManagerGM::getAllByName($type) as $obj) { $fds[] = $obj->getFd(); @@ -310,11 +350,13 @@ function getAllFdByConnectType(string $type = 'default'): array { return $fds; } -function zm_atomic($name): ?Atomic { +function zm_atomic($name): ?Atomic +{ return ZMAtomic::get($name); } -function uuidgen($uppercase = false): string { +function uuidgen($uppercase = false): string +{ try { $data = random_bytes(16); } catch (Exception $e) { @@ -326,11 +368,13 @@ function uuidgen($uppercase = false): string { vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4)); } -function working_dir() { +function working_dir() +{ return WORKING_DIR; } -function zm_dump($var, ...$moreVars) { +function zm_dump($var, ...$moreVars) +{ VarDumper::dump($var); foreach ($moreVars as $v) { @@ -344,40 +388,49 @@ function zm_dump($var, ...$moreVars) { return $var; } -function zm_info($obj) { +function zm_info($obj) +{ Console::info($obj); } -function zm_warning($obj) { +function zm_warning($obj) +{ Console::warning($obj); } -function zm_success($obj) { +function zm_success($obj) +{ Console::success($obj); } -function zm_debug($obj) { +function zm_debug($obj) +{ Console::debug($obj); } -function zm_verbose($obj) { +function zm_verbose($obj) +{ Console::verbose($obj); } -function zm_error($obj) { +function zm_error($obj) +{ Console::error($obj); } -function zm_config($name, $key = null) { +function zm_config($name, $key = null) +{ return ZMConfig::get($name, $key); } -function quick_reply_closure($reply) { +function quick_reply_closure($reply) +{ return function () use ($reply) { return $reply; }; } -function zm_internal_errcode($code): string { +function zm_internal_errcode($code): string +{ return "[ErrCode:$code] "; }