This commit is contained in:
2023-04-17 01:29:43 +08:00
parent dbfb477023
commit 642ec973cd
7 changed files with 47 additions and 84 deletions
+10 -9
View File
@@ -25,10 +25,6 @@ class OnServer extends Server
{
#[Container(EventDispatch::class)]
public EventDispatch $dispatch;
/**
* @param SServer $server
* @throws ConfigException
@@ -40,7 +36,8 @@ class OnServer extends Server
{
\Kiri::setProcessName(sprintf('start[%d].server', $server->master_pid));
$this->dispatch->dispatch(new OnStart($server));
$dispatch = \Kiri::getDi()->get(EventDispatch::class);
$dispatch->dispatch(new OnStart($server));
}
@@ -52,7 +49,8 @@ class OnServer extends Server
*/
public function onBeforeShutdown(SServer $server)
{
$this->dispatch->dispatch(new OnBeforeShutdown($server));
$dispatch = \Kiri::getDi()->get(EventDispatch::class);
$dispatch->dispatch(new OnBeforeShutdown($server));
}
@@ -64,7 +62,8 @@ class OnServer extends Server
*/
public function onShutdown(SServer $server)
{
$this->dispatch->dispatch(new OnShutdown($server));
$dispatch = \Kiri::getDi()->get(EventDispatch::class);
$dispatch->dispatch(new OnShutdown($server));
}
@@ -76,7 +75,8 @@ class OnServer extends Server
*/
public function onBeforeReload(SServer $server)
{
$this->dispatch->dispatch(new OnBeforeReload($server));
$dispatch = \Kiri::getDi()->get(EventDispatch::class);
$dispatch->dispatch(new OnBeforeReload($server));
}
@@ -88,7 +88,8 @@ class OnServer extends Server
*/
public function onAfterReload(SServer $server)
{
$this->dispatch->dispatch(new OnAfterReload($server));
$dispatch = \Kiri::getDi()->get(EventDispatch::class);
$dispatch->dispatch(new OnAfterReload($server));
}
+6 -8
View File
@@ -21,22 +21,19 @@ use Kiri\Server\Events\OnManagerStop;
class OnServerManager extends Server
{
#[Container(EventDispatch::class)]
public EventDispatch $dispatch;
/**
* @param \Swoole\Server $server
* @throws ConfigException
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws NotFoundExceptionInterface|ReflectionException
*/
public function onManagerStart(\Swoole\Server $server)
{
Kiri::setProcessName(sprintf('manger process[%d]', $server->manager_pid));
$this->dispatch->dispatch(new OnManagerStart($server));
$dispatch = \Kiri::getDi()->get(EventDispatch::class);
$dispatch->dispatch(new OnManagerStart($server));
}
@@ -44,11 +41,12 @@ class OnServerManager extends Server
* @param \Swoole\Server $server
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws NotFoundExceptionInterface|ReflectionException
*/
public function onManagerStop(\Swoole\Server $server): void
{
$this->dispatch->dispatch(new OnManagerStop($server));
$dispatch = \Kiri::getDi()->get(EventDispatch::class);
$dispatch->dispatch(new OnManagerStop($server));
}
+13 -28
View File
@@ -6,7 +6,6 @@ use Exception;
use Kiri;
use Kiri\Abstracts\Config;
use Kiri\Core\Help;
use Kiri\Di\Inject\Container;
use Kiri\Events\EventDispatch;
use Kiri\Server\Events\OnAfterWorkerStart;
use Kiri\Server\Events\OnBeforeWorkerStart;
@@ -19,9 +18,6 @@ use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use ReflectionException;
use Swoole\Server;
use Kiri\Server\Abstracts\StatusEnum;
use Kiri\Server\WorkerStatus;
use Kiri\Router\Router;
/**
@@ -32,17 +28,6 @@ class OnServerWorker extends \Kiri\Server\Abstracts\Server
{
#[Container(EventDispatch::class)]
public EventDispatch $dispatch;
#[Container(WorkerStatus::class)]
public WorkerStatus $status;
#[Container(Router::class)]
public Router $router;
/**
* @param Server $server
* @param int $workerId
@@ -53,15 +38,17 @@ class OnServerWorker extends \Kiri\Server\Abstracts\Server
*/
public function onWorkerStart(Server $server, int $workerId): void
{
$this->dispatch->dispatch(new OnBeforeWorkerStart($workerId));
$dispatch = \Kiri::getDi()->get(EventDispatch::class);
$dispatch->dispatch(new OnBeforeWorkerStart($workerId));
set_env('environmental_workerId', $workerId);
$this->status->setEnum(StatusEnum::START);
if ($workerId < $server->setting['worker_num']) {
$this->dispatch->dispatch(new OnWorkerStart($server, $workerId));
$dispatch->dispatch(new OnWorkerStart($server, $workerId));
} else {
$this->dispatch->dispatch(new OnTaskStart($server, $workerId));
$dispatch->dispatch(new OnTaskStart($server, $workerId));
}
$this->dispatch->dispatch(new OnAfterWorkerStart());
$dispatch->dispatch(new OnAfterWorkerStart());
}
@@ -73,9 +60,8 @@ class OnServerWorker extends \Kiri\Server\Abstracts\Server
*/
public function onWorkerStop(Server $server, int $workerId)
{
$this->status->setEnum(StatusEnum::STOP);
$this->dispatch->dispatch(new OnWorkerStop($server, $workerId));
$dispatch = \Kiri::getDi()->get(EventDispatch::class);
$dispatch->dispatch(new OnWorkerStop($server, $workerId));
}
@@ -87,9 +73,8 @@ class OnServerWorker extends \Kiri\Server\Abstracts\Server
*/
public function onWorkerExit(Server $server, int $workerId)
{
$this->status->setEnum(StatusEnum::EXIT);
$this->dispatch->dispatch(new OnWorkerExit($server, $workerId));
$dispatch = \Kiri::getDi()->get(EventDispatch::class);
$dispatch->dispatch(new OnWorkerExit($server, $workerId));
}
@@ -105,8 +90,8 @@ class OnServerWorker extends \Kiri\Server\Abstracts\Server
*/
public function onWorkerError(Server $server, int $worker_id, int $worker_pid, int $exit_code, int $signal)
{
$this->status->setEnum(StatusEnum::ERROR);
$this->dispatch->dispatch(new OnWorkerError($server, $worker_id, $worker_pid, $exit_code, $signal));
$dispatch = \Kiri::getDi()->get(EventDispatch::class);
$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)