From 52f637ea25b71befe6625403ba3f77624d546216 Mon Sep 17 00:00:00 2001 From: "as2252258@163.com" Date: Sun, 2 May 2021 04:12:31 +0800 Subject: [PATCH] modify --- Annotation/Loader.php | 15 ++++++++------- HttpServer/Events/OnWorkerStart.php | 21 +++++++++++++++++---- HttpServer/Server.php | 6 +++++- 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/Annotation/Loader.php b/Annotation/Loader.php index 6224f36e..1d696581 100644 --- a/Annotation/Loader.php +++ b/Annotation/Loader.php @@ -179,7 +179,7 @@ class Loader extends BaseObject } $this->appendFileToDirectory($path->getRealPath(), $replace->getName()); - $_array = ['handler' => $replace->newInstance(), 'target' => [], 'methods' => [], 'property' => []]; + $_array = ['handler' => $replace->getName(), 'target' => [], 'methods' => [], 'property' => []]; foreach ($replace->getAttributes() as $attribute) { if ($attribute->getName() == Attribute::class) { continue; @@ -401,21 +401,22 @@ class Loader extends BaseObject if ($annotations === null) { continue; } + + $target = new $annotations['handler'](); foreach ($annotations['target'] ?? [] as $value) { - $value->execute([$annotations['handler']]); + $value->execute([$target]); } - $_className = $annotations['handler']::class; foreach ($annotations['methods'] as $name => $attribute) { foreach ($attribute as $value) { if ($value instanceof Relation) { - $annotation->addRelate($_className, $value->name, $name); + $annotation->addRelate($target::class, $value->name, $name); } else if ($value instanceof Get) { - $annotation->addGets($_className, $value->name, $name); + $annotation->addGets($target::class, $value->name, $name); } else if ($value instanceof Set) { - $annotation->addSets($_className, $value->name, $name); + $annotation->addSets($target::class, $value->name, $name); } else { - $value->execute([$annotations['handler'], $name]); + $value->execute([$target, $name]); } } } diff --git a/HttpServer/Events/OnWorkerStart.php b/HttpServer/Events/OnWorkerStart.php index 8c47ad1a..7e992c3e 100644 --- a/HttpServer/Events/OnWorkerStart.php +++ b/HttpServer/Events/OnWorkerStart.php @@ -9,6 +9,7 @@ use HttpServer\Abstracts\Callback; use Snowflake\Event; use Snowflake\Exception\ConfigException; use Snowflake\Snowflake; +use Swoole\Coroutine; use Swoole\Coroutine\System; use Swoole\Server; @@ -32,10 +33,7 @@ class OnWorkerStart extends Callback putenv('state=start'); putenv('worker=' . $worker_id); - $content = System::readFile(storage('runtime.php')); - - $annotation = Snowflake::app()->getAnnotation(); - $annotation->setLoader(unserialize($content)); + $annotation = $this->settings(); if ($worker_id < $server->setting['worker_num']) { $this->onWorker($server, $annotation); } else { @@ -44,6 +42,21 @@ class OnWorkerStart extends Callback } + /** + * @return \Annotation\Annotation + * @throws \Exception + */ + private function settings(): Annotation + { + $content = System::readFile(storage('runtime.php')); + + $annotation = Snowflake::app()->getAnnotation(); + $annotation->setLoader(unserialize($content)); + + return $annotation; + } + + /** * @param Server $server * @param int $worker_id diff --git a/HttpServer/Server.php b/HttpServer/Server.php index 0a19bb7d..db90cb5a 100644 --- a/HttpServer/Server.php +++ b/HttpServer/Server.php @@ -115,7 +115,11 @@ class Server extends HttpService Runtime::enableCoroutine(true, SWOOLE_HOOK_ALL ^ SWOOLE_HOOK_BLOCKING_FUNCTION); - Coroutine::set(['enable_deadlock_check' => false]); + $settings['enable_deadlock_check'] = false; + $settings['exit_condition'] = function () { + return Coroutine::stats()['coroutine_num'] === 0; + }; + Coroutine::set($settings); return $this->execute($baseServer); }