From 0faa8f3ec4750a2eac3768efd0664de1ef87d7a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=91=E6=9E=97?= Date: Wed, 8 Jun 2022 14:53:13 +0800 Subject: [PATCH] modify plugin name --- Handler/OnServerWorker.php | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/Handler/OnServerWorker.php b/Handler/OnServerWorker.php index 4e53f5b..3b0df93 100644 --- a/Handler/OnServerWorker.php +++ b/Handler/OnServerWorker.php @@ -17,6 +17,7 @@ use Kiri\Server\Events\OnWorkerStart; use Kiri\Server\Events\OnWorkerStop; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; +use ReflectionException; use Swoole\Server; use Swoole\Timer; @@ -32,11 +33,15 @@ class OnServerWorker extends \Kiri\Server\Abstracts\Server public Router $router; + public EventDispatch $dispatch; + + /** * @return void */ public function init() { + $this->dispatch = Kiri::getDi()->get(EventDispatch::class); $this->router = Kiri::getDi()->get(Router::class); } @@ -52,19 +57,18 @@ class OnServerWorker extends \Kiri\Server\Abstracts\Server */ public function onWorkerStart(Server $server, int $workerId) { - $dispatch = \Kiri::getDi()->get(EventDispatch::class); - $dispatch->dispatch(new OnBeforeWorkerStart($workerId)); + $this->dispatch->dispatch(new OnBeforeWorkerStart($workerId)); set_env('environmental_workerId', $workerId); if ($workerId < $server->setting['worker_num']) { set_env('environmental', Kiri::WORKER); $this->setProcessName(sprintf('Worker Process[%d].%d', $server->worker_pid, $workerId)); - $dispatch->dispatch(new OnWorkerStart($server, $workerId)); + $this->dispatch->dispatch(new OnWorkerStart($server, $workerId)); } else { set_env('environmental', Kiri::TASK); $this->setProcessName(sprintf('Tasker Process[%d].%d', $server->worker_pid, $workerId)); - $dispatch->dispatch(new OnTaskStart($server, $workerId)); + $this->dispatch->dispatch(new OnTaskStart($server, $workerId)); } - $dispatch->dispatch(new OnAfterWorkerStart()); + $this->dispatch->dispatch(new OnAfterWorkerStart()); } @@ -72,12 +76,12 @@ class OnServerWorker extends \Kiri\Server\Abstracts\Server * @param Server $server * @param int $workerId * @throws ContainerExceptionInterface - * @throws NotFoundExceptionInterface + * @throws NotFoundExceptionInterface|ReflectionException */ public function onWorkerStop(Server $server, int $workerId) { Timer::clearAll(); - \Kiri::getDi()->get(EventDispatch::class)->dispatch(new OnWorkerStop($server, $workerId)); + $this->dispatch->dispatch(new OnWorkerStop($server, $workerId)); } @@ -85,11 +89,11 @@ class OnServerWorker extends \Kiri\Server\Abstracts\Server * @param Server $server * @param int $workerId * @throws ContainerExceptionInterface - * @throws NotFoundExceptionInterface + * @throws NotFoundExceptionInterface|ReflectionException */ public function onWorkerExit(Server $server, int $workerId) { - \Kiri::getDi()->get(EventDispatch::class)->dispatch(new OnWorkerExit($server, $workerId)); + $this->dispatch->dispatch(new OnWorkerExit($server, $workerId)); } @@ -105,7 +109,7 @@ class OnServerWorker extends \Kiri\Server\Abstracts\Server */ public function onWorkerError(Server $server, int $worker_id, int $worker_pid, int $exit_code, int $signal) { - \Kiri::getDi()->get(EventDispatch::class)->dispatch(new OnWorkerError($server, $worker_id, $worker_pid, $exit_code, $signal)); + $this->dispatch->dispatch(new OnWorkerError($server, $worker_id, $worker_pid, $exit_code, $signal)); $message = sprintf('Worker#%d::%d error stop. signal %d, exit_code %d, msg %s', $worker_id, $worker_pid, $signal, $exit_code, swoole_strerror(swoole_last_error(), 9)