From ea09639fd721d3e4152b455dc483393285f19c1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=91=E6=9E=97?= Date: Fri, 29 Oct 2021 14:15:40 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/RpcJsonp.php | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/RpcJsonp.php b/src/RpcJsonp.php index 4a9cc80..1edaf5d 100644 --- a/src/RpcJsonp.php +++ b/src/RpcJsonp.php @@ -8,10 +8,11 @@ use Http\Handler\Router; use Kiri\Abstracts\Component; use Kiri\Abstracts\Config; use Kiri\Consul\Agent; -use Kiri\Di\NoteManager; +use Kiri\Di\ContainerInterface; use Kiri\Events\EventProvider; use Kiri\Exception\ConfigException; use Kiri\Kiri; +use Server\Events\OnBeforeShutdown; use Server\Events\OnStart; use Server\SInterface\OnCloseInterface; use Server\SInterface\OnConnectInterface; @@ -39,6 +40,10 @@ class RpcJsonp extends Component implements OnConnectInterface, OnReceiveInterfa #[Inject(EventProvider::class)] public EventProvider $eventProvider; + + #[Inject(ContainerInterface::class)] + public ContainerInterface $container; + /** * * @throws \Exception @@ -46,11 +51,25 @@ class RpcJsonp extends Component implements OnConnectInterface, OnReceiveInterfa public function init(): void { $this->eventProvider->on(OnStart::class, [$this, 'register']); + $this->eventProvider->on(OnBeforeShutdown::class, [$this, 'onBeforeShutdown']); scan_directory(APP_PATH . 'rpc', 'Rpc'); } + /** + * @param OnBeforeShutdown $beforeShutdown + * @throws ConfigException + */ + public function onBeforeShutdown(OnBeforeShutdown $beforeShutdown) + { + $config = Config::get('rpc'); + + $agent = $this->container->get(Agent::class); + $agent->service->deregister($config['registry']['config']['config']); + } + + /** * @throws ConfigException */ @@ -58,7 +77,7 @@ class RpcJsonp extends Component implements OnConnectInterface, OnReceiveInterfa { $config = Config::get('rpc'); - $agent = Kiri::getDi()->get(Agent::class); + $agent = $this->container->get(Agent::class); $data = $agent->service->register($config['registry']['config']); if ($data->getStatusCode() != 200) { $server->server->shutdown();