From a5378658ee9f646c3d04cecbf889c176f012bdd9 Mon Sep 17 00:00:00 2001 From: xl Date: Mon, 18 Nov 2024 11:24:08 +0800 Subject: [PATCH] eee --- Abstracts/HotReload.php | 129 +++++++++++++++++++++++++++++++++++++ Handler/OnServerWorker.php | 4 ++ ServerCommand.php | 51 ++++++++++++--- 3 files changed, 175 insertions(+), 9 deletions(-) create mode 100644 Abstracts/HotReload.php diff --git a/Abstracts/HotReload.php b/Abstracts/HotReload.php new file mode 100644 index 0000000..c0400c1 --- /dev/null +++ b/Abstracts/HotReload.php @@ -0,0 +1,129 @@ +pipe = inotify_init(); + $this->readDirectory(APP_PATH . '/app/*'); + $this->readDirectory(APP_PATH . '/routes/*'); + + while (!$this->isStop()) { + $read = inotify_read($this->pipe); + + foreach ($read as $item) { + + $this->reWatch($item['name']); + + } + + $this->reload(); + } + } + + + /** + * @return void + */ + public function reload(): void + { + $this->reloading = true; + + di(ServerInterface::class)->reload(); + + $this->reloading = false; + } + + + /** + * @param string $directory + * @return void + */ + public function readFile(string $directory): void + { + if (str_ends_with($directory, '.php') === true) { + inotify_add_watch($this->pipe, $directory, IN_MODIFY | IN_MOVE | IN_CREATE | IN_DELETE); + } + } + + + /** + * @param string $directory + * @return void + */ + public function reWatch(string $directory): void + { + if (str_ends_with($directory, '.php') === true) { + inotify_rm_watch($this->pipe, $directory); + + inotify_add_watch($this->pipe, $directory, IN_MODIFY | IN_MOVE | IN_CREATE | IN_DELETE); + } + } + + + /** + * @param string $directory + * @return void + */ + public function readDirectory(string $directory): void + { + foreach (glob($directory . '/*') as $data) { + if (str_ends_with($data, '.php') === false) { + continue; + } + if (is_dir($data)) { + $this->readFile($data . DIRECTORY_SEPARATOR . '*'); + } else { + $this->readFile($data); + } + } + } +} \ No newline at end of file diff --git a/Handler/OnServerWorker.php b/Handler/OnServerWorker.php index e296533..7521ceb 100644 --- a/Handler/OnServerWorker.php +++ b/Handler/OnServerWorker.php @@ -3,6 +3,8 @@ namespace Kiri\Server\Handler; use Kiri; +use Kiri\Abstracts\CoordinatorManager; +use Kiri\Coordinator; use Kiri\Core\Help; use Kiri\Events\EventDispatch; use Kiri\Server\Events\OnAfterWorkerStart; @@ -63,6 +65,8 @@ class OnServerWorker extends Kiri\Server\Abstracts\Server $dispatch = Kiri::getDi()->get(EventDispatch::class); $dispatch->dispatch(new OnBeforeWorkerStart($server, $workerId)); if ($workerId < $server->setting['worker_num']) { + CoordinatorManager::utility(Coordinator::WORKER_START)->waite(); + $dispatch->dispatch(new OnWorkerStart($server, $workerId)); } else { $dispatch->dispatch(new OnTaskerStart($server, $workerId)); diff --git a/ServerCommand.php b/ServerCommand.php index 7646a69..b54a8a0 100644 --- a/ServerCommand.php +++ b/ServerCommand.php @@ -6,16 +6,21 @@ namespace Kiri\Server; use Exception; use Kiri; +use Kiri\Server\Events\OnWorkerStart; +use Kiri\Events\EventProvider; use Kiri\Events\EventDispatch; use Kiri\Router\Router; use Kiri\Server\Abstracts\AsyncServer; use Kiri\Server\Events\OnShutdown; +use Psr\Container\ContainerInterface; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; use function config; +use Kiri\Server\Abstracts\HotReload; +use Kiri\Di\Inject\Container; defined('ROUTER_TYPE_HTTP') or define('ROUTER_TYPE_HTTP', 'http'); defined('PID_PATH') or define('PID_PATH', APP_PATH . 'storage/server.pid'); @@ -31,6 +36,34 @@ class ServerCommand extends Command public State $state; + /** + * @var ContainerInterface + */ + #[Container(ContainerInterface::class)] + public ContainerInterface $container; + + + /** + * @var AsyncServer + */ + #[Container(AsyncServer::class)] + public AsyncServer $asyncServer; + + + /** + * @var EventDispatch + */ + #[Container(EventDispatch::class)] + public EventDispatch $eventDispatch; + + + /** + * @var EventProvider + */ + #[Container(EventProvider::class)] + public EventProvider $eventProvider; + + /** * @param string|null $name * @throws Exception @@ -92,12 +125,11 @@ class ServerCommand extends Command protected function stop(): int { $configs = config('server', []); - $instances = Kiri::getDi()->get(AsyncServer::class)->sortService($configs['ports'] ?? []); + $instances = $this->asyncServer->sortService($configs['ports'] ?? []); foreach ($instances as $config) { $this->state->exit($config->port); } - $dispatch = Kiri::getDi()->get(EventDispatch::class); - $dispatch->dispatch(new OnShutdown()); + $this->eventDispatch->dispatch(new OnShutdown()); return 1; } @@ -109,12 +141,13 @@ class ServerCommand extends Command */ protected function start(InputInterface $input): int { - $manager = Kiri::getDi()->get(AsyncServer::class); - $router = Kiri::getDi()->get(Router::class); - $router->scan_build_route(); - $manager->addProcess(config('processes', [])); - $manager->initCoreServers(config('server', []), (int)$input->getOption('daemon')); - $manager->start(); + $this->eventProvider->on(OnWorkerStart::class, [di(Router::class), 'scan_build_route']); + $this->asyncServer->addProcess(config('processes', [])); + if (\config('reload.hot', false) === true) { + $this->asyncServer->addProcess([HotReload::class]); + } + $this->asyncServer->initCoreServers(config('server', []), (int)$input->getOption('daemon')); + $this->asyncServer->start(); return 1; }