From b74b0be01819eb22d651541abdb5df962e153ba7 Mon Sep 17 00:00:00 2001 From: "as2252258@163.com" Date: Wed, 7 Apr 2021 02:21:29 +0800 Subject: [PATCH] modify --- Annotation/Annotation.php | 108 +++++++++++++++------------- HttpServer/Events/OnWorkerStart.php | 98 +++++++++++++------------ System/Process/ServerInotify.php | 60 ++++++++++++---- 3 files changed, 157 insertions(+), 109 deletions(-) diff --git a/Annotation/Annotation.php b/Annotation/Annotation.php index 8a19d2bf..e7d4154c 100644 --- a/Annotation/Annotation.php +++ b/Annotation/Annotation.php @@ -18,66 +18,76 @@ class Annotation extends Component { - private Loader $_loader; + private Loader $_loader; - public function init(): void - { - $this->_loader = new Loader(); - } + public function init(): void + { + $this->_loader = new Loader(); + } - /** - * @param string $className - * @param string $method - * @return array 根据类名获取注解 - * 根据类名获取注解 - */ - public function getMethods(string $className, string $method = ''): mixed - { - return $this->_loader->getMethod($className, $method); - } + /** + * @return Loader + */ + public function getLoader(): Loader + { + return $this->_loader; + } - /** - * @param object $class - */ - public function injectProperty(object $class) - { - $this->_loader->injectProperty(get_class($class), $class); - } + /** + * @param string $className + * @param string $method + * @return array 根据类名获取注解 + * 根据类名获取注解 + */ + public function getMethods(string $className, string $method = ''): mixed + { + return $this->_loader->getMethod($className, $method); + } - /** - * @param string $path - * @param string $namespace - * @param string $alias - * @return void - * @throws Exception - */ - public function read(string $path, string $namespace, string $alias = 'root'): void - { - - $this->_loader->_scanDir(new DirectoryIterator($path), $namespace); - } + /** + * @param object $class + */ + public function injectProperty(object $class) + { + $this->_loader->injectProperty(get_class($class), $class); + } - /** - * @param string $dir - */ - public function instanceDirectoryFiles(string $dir) - { - $this->_loader->loadByDirectory($dir); - } + /** + * @param string $path + * @param string $namespace + * @param string $alias + * @return void + * @throws Exception + */ + public function read(string $path, string $namespace, string $alias = 'root'): void + { + + $this->_loader->_scanDir(new DirectoryIterator($path), $namespace); + } - /** - * @param string $filename - * @return mixed - */ - public function getFilename(string $filename): mixed - { - return $this->_loader->getClassByFilepath($filename); - } + /** + * @param string $dir + * @throws Exception + */ + public function instanceDirectoryFiles(string $dir) + { + $this->_loader->loadByDirectory($dir); + } + + + /** + * @param string $filename + * @return mixed + */ + public function getFilename(string $filename): mixed + { + return $this->_loader->getClassByFilepath($filename); + } } diff --git a/HttpServer/Events/OnWorkerStart.php b/HttpServer/Events/OnWorkerStart.php index b7502e12..929e4303 100644 --- a/HttpServer/Events/OnWorkerStart.php +++ b/HttpServer/Events/OnWorkerStart.php @@ -3,6 +3,7 @@ declare(strict_types=1); namespace HttpServer\Events; +use Annotation\Loader; use Annotation\Target; use Exception; use HttpServer\Abstracts\Callback; @@ -10,6 +11,7 @@ use Snowflake\Abstracts\Config; use Snowflake\Event; use Snowflake\Exception\ComponentException; use Snowflake\Exception\ConfigException; +use Snowflake\Process\ServerInotify; use Snowflake\Snowflake; use Swoole\Coroutine; use Swoole\Server; @@ -23,60 +25,66 @@ class OnWorkerStart extends Callback { - /** - * @param Server $server - * @param int $worker_id - * - * @return mixed - * @throws Exception - */ - public function onHandler(Server $server, int $worker_id): void - { - putenv('state=start'); - putenv('worker=' . $worker_id); + /** + * @param Server $server + * @param int $worker_id + * + * @return mixed + * @throws Exception + */ + public function onHandler(Server $server, int $worker_id): void + { + putenv('state=start'); + putenv('worker=' . $worker_id); - name($server->worker_pid, $worker_id >= $server->setting['worker_num'] ? 'task' : 'worker'); + name($server->worker_pid, $worker_id >= $server->setting['worker_num'] ? 'task' : 'worker'); - if ($worker_id >= $server->setting['worker_num']) { - $this->onTask($server, $worker_id); - } else { - $this->onWorker($server, $worker_id); - } - } + $loader = Snowflake::app()->get(ServerInotify::class); + + if ($worker_id >= $server->setting['worker_num']) { + $this->onTask($server, $worker_id, $loader); + } else { + $this->onWorker($server, $worker_id, $loader); + } + } - /** - * @param Server $server - * @param int $worker_id - * @throws Exception - */ - public function onTask(Server $server, int $worker_id) - { - putenv('environmental=' . Snowflake::TASK); + /** + * @param Server $server + * @param int $worker_id + * @throws Exception + */ + public function onTask(Server $server, int $worker_id, Loader $loader) + { + putenv('environmental=' . Snowflake::TASK); - Snowflake::setTaskId($server->worker_pid); + Snowflake::setTaskId($server->worker_pid); - fire(Event::SERVER_TASK_START); - } + $loader->loadByDirectory(MODEL_PATH); + + fire(Event::SERVER_TASK_START); + } - /** - * @param Server $server - * @param int $worker_id - * @throws Exception - */ - public function onWorker(Server $server, int $worker_id) - { - Snowflake::setWorkerId($server->worker_pid); - putenv('environmental=' . Snowflake::WORKER); + /** + * @param Server $server + * @param int $worker_id + * @throws Exception + */ + public function onWorker(Server $server, int $worker_id, Loader $loader) + { + Snowflake::setWorkerId($server->worker_pid); + putenv('environmental=' . Snowflake::WORKER); - try { - fire(Event::SERVER_WORKER_START, [$worker_id]); - } catch (\Throwable $exception) { - $this->addError($exception,'throwable'); - write($exception->getMessage(), 'worker'); - } - } + $loader->loadByDirectory(APP_PATH); + + try { + fire(Event::SERVER_WORKER_START, [$worker_id]); + } catch (\Throwable $exception) { + $this->addError($exception, 'throwable'); + write($exception->getMessage(), 'worker'); + } + } } diff --git a/System/Process/ServerInotify.php b/System/Process/ServerInotify.php index bcc27aca..fc0a75d0 100644 --- a/System/Process/ServerInotify.php +++ b/System/Process/ServerInotify.php @@ -34,6 +34,26 @@ class ServerInotify extends Process private int $int = -1; + + /** + * @throws Exception + */ + private function loadAnnotation() + { + $annotation = Snowflake::app()->getAnnotation(); + $annotation->read(directory('app'), 'App'); + } + + + /** + * @return mixed + */ + public function getLoader() + { + return $this->getLoader(); + } + + /** * @param \Swoole\Process $process * @throws Exception @@ -42,6 +62,9 @@ class ServerInotify extends Process { set_error_handler([$this, 'onErrorHandler']); $this->dirs = Config::get('inotify', [APP_PATH]); + + $this->loadAnnotation(); + if (extension_loaded('inotify')) { $this->inotify = inotify_init(); $this->events = IN_MODIFY | IN_DELETE | IN_CREATE | IN_MOVE; @@ -194,14 +217,17 @@ class ServerInotify extends Process { $this->isReloading = true; $this->trigger_reload(); - $this->clearWatch(); - foreach ($this->dirs as $root) { - $this->watch($root); - } - $this->int = -1; - $this->isReloading = FALSE; - $this->isReloadingOut = FALSE; - $this->md5Map = []; + + $this->exit(0); + +// $this->clearWatch(); +// foreach ($this->dirs as $root) { +// $this->watch($root); +// } +// $this->int = -1; +// $this->isReloading = FALSE; +// $this->isReloadingOut = FALSE; +// $this->md5Map = []; } /** @@ -211,14 +237,18 @@ class ServerInotify extends Process { $this->isReloading = true; $this->trigger_reload(); - $this->int = -1; - $this->loadDirs(); - - $this->isReloading = FALSE; - $this->isReloadingOut = FALSE; - - $this->tick(); + $this->exit(0); +// +// +// $this->int = -1; +// +// $this->loadDirs(); +// +// $this->isReloading = FALSE; +// $this->isReloadingOut = FALSE; +// +// $this->tick(); }