From 9b3a2e5296b269de67d099be0e5fcb26b90ba8a5 Mon Sep 17 00:00:00 2001 From: whale Date: Wed, 6 May 2020 17:19:59 +0800 Subject: [PATCH] fix some DataProvider bug. --- composer.json | 2 +- phar-starter.php | 2 +- src/Framework/DataProvider.php | 16 +++++++++++----- src/Framework/FrameworkLoader.php | 2 +- src/Framework/global_functions.php | 11 +++++++---- src/ZM/Event/Swoole/WorkerStartEvent.php | 5 +---- src/ZM/ModBase.php | 6 +++--- src/ZM/Utils/ZMUtil.php | 3 +++ 8 files changed, 28 insertions(+), 19 deletions(-) diff --git a/composer.json b/composer.json index f1cf2e42..82cc2087 100644 --- a/composer.json +++ b/composer.json @@ -3,7 +3,7 @@ "description": "high-performance intelligent assistant", "minimum-stability": "stable", "license": "proprietary", - "version": "1.2.1", + "version": "1.3.0", "authors": [ { "name": "whale", diff --git a/phar-starter.php b/phar-starter.php index ddd6baa6..333ad6d0 100644 --- a/phar-starter.php +++ b/phar-starter.php @@ -54,7 +54,7 @@ function testEnvironment() { if (!is_file($current_dir . '/config/global.php')) { echo "Exporting default global config...\n"; $global = file_get_contents(__DIR__ . '/config/global.php'); - $global = str_replace("WORKING_DIR", 'realpath("../")', $global); + $global = str_replace("WORKING_DIR", 'realpath(__DIR__ . "/../")', $global); file_put_contents($current_dir . '/config/global.php', $global); } if (!is_file($current_dir . '/config/file_header.json')) { diff --git a/src/Framework/DataProvider.php b/src/Framework/DataProvider.php index 002fe310..0c1b03b9 100644 --- a/src/Framework/DataProvider.php +++ b/src/Framework/DataProvider.php @@ -14,9 +14,9 @@ class DataProvider public static function getWorkingDir() { global $is_phar; - if($is_phar === true) { + if ($is_phar === true) { return realpath('.'); - } else { + } else { return WORKING_DIR; } } @@ -28,7 +28,8 @@ class DataProvider public static function addSaveBuffer($buf_name, $sub_folder = null) { $name = ($sub_folder ?? "") . "/" . $buf_name . ".json"; self::$buffer_list[$buf_name] = $name; - Console::debug("Added ".$buf_name . " at $sub_folder"); + Console::debug("Added " . $buf_name . " at $sub_folder"); + var_dump(self::$buffer_list); ZMBuf::set($buf_name, self::getJsonData($name)); } @@ -37,6 +38,7 @@ class DataProvider if (ZMBuf::$atomics["info_level"]->get() >= 3) echo $head; foreach (self::$buffer_list as $k => $v) { + Console::debug("Saving " . $k . " to " . $v); self::setJsonData($v, ZMBuf::get($k)); } if (ZMBuf::$atomics["info_level"]->get() >= 3) @@ -53,8 +55,12 @@ class DataProvider } private static function setJsonData($filename, array $args) { - Console::debug("Saving ".$filename); - file_put_contents(self::getDataConfig() . $filename, json_encode($args, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT | JSON_BIGINT_AS_STRING)); + $pathinfo = pathinfo($filename); + if (!is_dir($pathinfo["dirname"])) mkdir(self::getDataConfig() . $pathinfo["dirname"]); + $r = file_put_contents(self::getDataConfig() . $filename, json_encode($args, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT | JSON_BIGINT_AS_STRING)); + if ($r === false) { + Console::warning("无法保存文件: " . $filename); + } } public static function getDataFolder() { diff --git a/src/Framework/FrameworkLoader.php b/src/Framework/FrameworkLoader.php index bf4eff19..a9eed746 100644 --- a/src/Framework/FrameworkLoader.php +++ b/src/Framework/FrameworkLoader.php @@ -68,7 +68,7 @@ class FrameworkLoader $this->server->set($settings); $this->server->on("WorkerStart", [$this, "onWorkerStart"]); $this->server->on("message", function ($server, Frame $frame) { - Console::debug("Calling Swoole \"message\" event from fd=" . $frame->fd); + Console::debug("Calling Swoole \"message\" from fd=" . $frame->fd); EventHandler::callSwooleEvent("message", $server, $frame); }); $this->server->on("request", function ($request, $response) { diff --git a/src/Framework/global_functions.php b/src/Framework/global_functions.php index 4dace297..3738764a 100644 --- a/src/Framework/global_functions.php +++ b/src/Framework/global_functions.php @@ -5,7 +5,7 @@ use Framework\DataProvider; use Framework\ZMBuf; use ZM\Context\ContextInterface; -function isPharMode(){ +function isPharMode() { return substr(__DIR__, 0, 7) == 'phar://'; } @@ -162,7 +162,8 @@ function matchArgs($pattern, $context) { function set_coroutine_params($array) { $cid = Co::getCid(); if ($cid == -1) die("Cannot set coroutine params at none coroutine mode."); - ZMBuf::$context[$cid] = $array; + if(isset(ZMBuf::$context[$cid])) ZMBuf::$context[$cid] = array_merge(ZMBuf::$context[$cid], $array); + else ZMBuf::$context[$cid] = $array; foreach (ZMBuf::$context as $c => $v) { if (!Co::exists($c)) unset(ZMBuf::$context[$c]); } @@ -175,16 +176,18 @@ function context() { $cid = Co::getCid(); $c_class = ZMBuf::globals("context_class"); if (isset(ZMBuf::$context[$cid])) { - return new $c_class(ZMBuf::$context[$cid], $cid); + return new $c_class($cid); } else { while (($pcid = Co::getPcid($cid)) !== -1) { - if (isset(ZMBuf::$context[$cid])) return new $c_class(ZMBuf::$context[$cid], $cid); + if (isset(ZMBuf::$context[$cid])) return new $c_class($cid); else $cid = $pcid; } return null; } } +function ctx() { return context(); } + function debug($msg) { if (ZMBuf::$atomics["info_level"]->get() >= 4) Console::log(date("[H:i:s] ") . "[D] " . $msg, 'gray'); diff --git a/src/ZM/Event/Swoole/WorkerStartEvent.php b/src/ZM/Event/Swoole/WorkerStartEvent.php index 2f6bfc78..f3755884 100644 --- a/src/ZM/Event/Swoole/WorkerStartEvent.php +++ b/src/ZM/Event/Swoole/WorkerStartEvent.php @@ -85,11 +85,8 @@ class WorkerStartEvent implements SwooleEvent Console::info("监听console输入"); Console::listenConsole(); //这个方法只能在这里调用,且如果worker_num不为1的话,此功能不可用 - - $this->loadAllClass(); //加载composer资源、phar外置包、注解解析注册等 - $this->setAutosaveTimer(ZMBuf::globals("auto_save_interval")); - + $this->loadAllClass(); //加载composer资源、phar外置包、注解解析注册等 return $this; } diff --git a/src/ZM/ModBase.php b/src/ZM/ModBase.php index e14649f6..55d0d271 100644 --- a/src/ZM/ModBase.php +++ b/src/ZM/ModBase.php @@ -67,7 +67,7 @@ abstract class ModBase } public function finalReply($msg, $yield = false) { - $this->block_continue = true; + $this->setBlock(); if ($msg == "") return true; return $this->reply($msg, $yield); } @@ -161,5 +161,5 @@ abstract class ModBase public function getConnection() { return $this->connection; } - public function setBlock($result = true) { $this->block_continue = $result; } -} \ No newline at end of file + public function setBlock($result = true) { context()->setCache("block_continue", $result); } +} diff --git a/src/ZM/Utils/ZMUtil.php b/src/ZM/Utils/ZMUtil.php index e1010c13..e641f346 100644 --- a/src/ZM/Utils/ZMUtil.php +++ b/src/ZM/Utils/ZMUtil.php @@ -40,6 +40,9 @@ class ZMUtil public static function reload() { Console::info(Console::setColor("Reloading server...", "gold")); + foreach (ZMBuf::get("wait_api") as $k => $v) { + if ($v["result"] === null) Co::resume($v["coroutine"]); + } foreach (ZMBuf::$server->connections as $v) { ZMBuf::$server->close($v); }