This commit is contained in:
2023-04-21 22:26:43 +08:00
parent ada0f49a43
commit f313860436
12 changed files with 25 additions and 135 deletions
+1 -2
View File
@@ -218,8 +218,7 @@ class AsyncServer implements ServerInterface
*/ */
public function start(): void public function start(): void
{ {
$processManager = Kiri::getDi()->get(EventDispatch::class); event(new OnServerBeforeStart());
$processManager->dispatch(new OnServerBeforeStart());
$this->server->start(); $this->server->start();
} }
+1
View File
@@ -90,6 +90,7 @@ abstract class BaseProcess implements OnProcessInterface
/** /**
* @param $data * @param $data
* @throws \ReflectionException
*/ */
protected function onShutdown($data): void protected function onShutdown($data): void
{ {
-36
View File
@@ -1,36 +0,0 @@
<?php
namespace Kiri\Server\Abstracts;
use Kiri\Di\Context;
class DoWhile
{
private bool $isStop = false;
/**
* @param array|\Closure $handler
* @return void
*/
public static function waite(array|\Closure $handler): void
{
if (Context::exists('stop')) {
return;
}
$handler();
self::waite($handler);
}
/**
* @return void
*/
public static function stop(): void
{
Context::set('stop', 1);
}
}
+2 -2
View File
@@ -164,8 +164,8 @@ trait TraitServer
public function getCoroutineServerClass($type): ?string public function getCoroutineServerClass($type): ?string
{ {
return match ($type) { return match ($type) {
Constant::SERVER_TYPE_BASE, Constant::SERVER_TYPE_TCP, Constant::SERVER_TYPE_UDP => \Swoole\Coroutine\Server::class, Constant::SERVER_TYPE_BASE, Constant::SERVER_TYPE_TCP, Constant::SERVER_TYPE_UDP => Coroutine\Server::class,
Constant::SERVER_TYPE_HTTP, Constant::SERVER_TYPE_WEBSOCKET => \Swoole\Coroutine\Http\Server::class, Constant::SERVER_TYPE_HTTP, Constant::SERVER_TYPE_WEBSOCKET => Coroutine\Http\Server::class,
default => null default => null
}; };
} }
+2
View File
@@ -5,6 +5,7 @@ namespace Kiri\Server\Broadcast;
use Kiri; use Kiri;
use Kiri\Server\ServerInterface; use Kiri\Server\ServerInterface;
use Kiri\Server\Abstracts\ProcessManager; use Kiri\Server\Abstracts\ProcessManager;
use ReflectionException;
class Broadcast class Broadcast
{ {
@@ -13,6 +14,7 @@ class Broadcast
/** /**
* @param $message * @param $message
* @return void * @return void
* @throws ReflectionException
*/ */
public function broadcast($message): void public function broadcast($message): void
{ {
+1 -1
View File
@@ -20,7 +20,7 @@ class OnPipeMessage extends Server
* @param mixed $message * @param mixed $message
* @throws Exception * @throws Exception
*/ */
public function onPipeMessage(\Swoole\Server $server, int $src_worker_id, mixed $message) public function onPipeMessage(\Swoole\Server $server, int $src_worker_id, mixed $message): void
{ {
if (is_string($message)) { if (is_string($message)) {
$message = unserialize($message); $message = unserialize($message);
+6 -39
View File
@@ -32,12 +32,11 @@ class OnServer extends Server
* @throws ContainerExceptionInterface * @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface * @throws NotFoundExceptionInterface
*/ */
public function onStart(SServer $server) public function onStart(SServer $server): void
{ {
\Kiri::setProcessName(sprintf('start[%d].server', $server->master_pid)); \Kiri::setProcessName(sprintf('start[%d].server', $server->master_pid));
$dispatch = \Kiri::getDi()->get(EventDispatch::class); event(new OnStart($server));
$dispatch->dispatch(new OnStart($server));
} }
@@ -47,52 +46,20 @@ class OnServer extends Server
* @throws NotFoundExceptionInterface * @throws NotFoundExceptionInterface
* @throws ReflectionException * @throws ReflectionException
*/ */
public function onBeforeShutdown(SServer $server) public function onBeforeShutdown(SServer $server): void
{ {
$dispatch = \Kiri::getDi()->get(EventDispatch::class); event(new OnBeforeShutdown($server));
$dispatch->dispatch(new OnBeforeShutdown($server));
} }
/** /**
* @param SServer $server * @param SServer $server
* @throws ContainerExceptionInterface * @throws
* @throws NotFoundExceptionInterface
* @throws ReflectionException
*/ */
public function onShutdown(SServer $server): void public function onShutdown(SServer $server): void
{ {
@unlink(storage('.swoole.pid')); @unlink(storage('.swoole.pid'));
$dispatch = \Kiri::getDi()->get(EventDispatch::class); event(new OnShutdown($server));
$dispatch->dispatch(new OnShutdown($server));
} }
/**
* @param SServer $server
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws ReflectionException
*/
public function onBeforeReload(SServer $server)
{
$dispatch = \Kiri::getDi()->get(EventDispatch::class);
$dispatch->dispatch(new OnBeforeReload($server));
}
/**
* @param SServer $server
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws ReflectionException
*/
public function onAfterReload(SServer $server)
{
$dispatch = \Kiri::getDi()->get(EventDispatch::class);
$dispatch->dispatch(new OnAfterReload($server));
}
} }
+3 -6
View File
@@ -24,16 +24,14 @@ class OnServerManager extends Server
/** /**
* @param \Swoole\Server $server * @param \Swoole\Server $server
* @throws ConfigException
* @throws ContainerExceptionInterface * @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface|ReflectionException * @throws NotFoundExceptionInterface|ReflectionException
*/ */
public function onManagerStart(\Swoole\Server $server) public function onManagerStart(\Swoole\Server $server): void
{ {
Kiri::setProcessName(sprintf('manger process[%d]', $server->manager_pid)); Kiri::setProcessName(sprintf('manger process[%d]', $server->manager_pid));
$dispatch = \Kiri::getDi()->get(EventDispatch::class); event(new OnManagerStart($server));
$dispatch->dispatch(new OnManagerStart($server));
} }
@@ -45,8 +43,7 @@ class OnServerManager extends Server
*/ */
public function onManagerStop(\Swoole\Server $server): void public function onManagerStop(\Swoole\Server $server): void
{ {
$dispatch = \Kiri::getDi()->get(EventDispatch::class); event(new OnManagerStop($server));
$dispatch->dispatch(new OnManagerStop($server));
} }
+7 -9
View File
@@ -58,10 +58,9 @@ class OnServerWorker extends \Kiri\Server\Abstracts\Server
* @throws ContainerExceptionInterface * @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface|ReflectionException * @throws NotFoundExceptionInterface|ReflectionException
*/ */
public function onWorkerStop(Server $server, int $workerId) public function onWorkerStop(Server $server, int $workerId): void
{ {
$dispatch = \Kiri::getDi()->get(EventDispatch::class); event(new OnWorkerStop($server, $workerId));
$dispatch->dispatch(new OnWorkerStop($server, $workerId));
} }
@@ -71,10 +70,9 @@ class OnServerWorker extends \Kiri\Server\Abstracts\Server
* @throws ContainerExceptionInterface * @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface|ReflectionException * @throws NotFoundExceptionInterface|ReflectionException
*/ */
public function onWorkerExit(Server $server, int $workerId) public function onWorkerExit(Server $server, int $workerId): void
{ {
$dispatch = \Kiri::getDi()->get(EventDispatch::class); event(new OnWorkerExit($server, $workerId));
$dispatch->dispatch(new OnWorkerExit($server, $workerId));
} }
@@ -88,7 +86,7 @@ class OnServerWorker extends \Kiri\Server\Abstracts\Server
* @throws NotFoundExceptionInterface * @throws NotFoundExceptionInterface
* @throws Exception * @throws Exception
*/ */
public function onWorkerError(Server $server, int $worker_id, int $worker_pid, int $exit_code, int $signal) public function onWorkerError(Server $server, int $worker_id, int $worker_pid, int $exit_code, int $signal): void
{ {
$dispatch = \Kiri::getDi()->get(EventDispatch::class); $dispatch = \Kiri::getDi()->get(EventDispatch::class);
$dispatch->dispatch(new OnWorkerError($server, $worker_id, $worker_pid, $exit_code, $signal)); $dispatch->dispatch(new OnWorkerError($server, $worker_id, $worker_pid, $exit_code, $signal));
@@ -97,7 +95,7 @@ class OnServerWorker extends \Kiri\Server\Abstracts\Server
$worker_id, $worker_pid, $signal, $exit_code, swoole_strerror(swoole_last_error(), 9) $worker_id, $worker_pid, $signal, $exit_code, swoole_strerror(swoole_last_error(), 9)
); );
\Kiri::getLogger()->error($message); error($message);
$this->system_mail($message); $this->system_mail($message);
} }
@@ -107,7 +105,7 @@ class OnServerWorker extends \Kiri\Server\Abstracts\Server
* @param $messageContent * @param $messageContent
* @throws Exception * @throws Exception
*/ */
protected function system_mail($messageContent) protected function system_mail($messageContent): void
{ {
try { try {
$email = Config::get('email', ['enable' => false]); $email = Config::get('email', ['enable' => false]);
+1 -6
View File
@@ -6,7 +6,6 @@ namespace Kiri\Server;
use Exception; use Exception;
use Kiri; use Kiri;
use Kiri\Abstracts\Config;
use Kiri\Exception\ConfigException; use Kiri\Exception\ConfigException;
use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface; use Psr\Container\NotFoundExceptionInterface;
@@ -56,17 +55,13 @@ class ServerCommand extends Command
*/ */
public function execute(InputInterface $input, OutputInterface $output): int public function execute(InputInterface $input, OutputInterface $output): int
{ {
$value = match ($input->getArgument('action')) { return match ($input->getArgument('action')) {
'restart' => $this->restart($input), 'restart' => $this->restart($input),
'stop' => $this->stop(), 'stop' => $this->stop(),
'start' => $this->start($input), 'start' => $this->start($input),
default => default =>
throw new Exception('I don\'t know what I want to do.') throw new Exception('I don\'t know what I want to do.')
}; };
if ($input->getOption('daemon') != 1) {
file_put_contents(storage('.swoole.pid'), 0);
}
return $value;
} }
+1 -1
View File
@@ -24,7 +24,7 @@ class ServerProviders extends Providers
* @throws ContainerExceptionInterface * @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface * @throws NotFoundExceptionInterface
*/ */
public function onImport(LocalService $application) public function onImport(LocalService $application): void
{ {
$server = $this->container->get(ServerCommand::class); $server = $this->container->get(ServerCommand::class);
-33
View File
@@ -1,33 +0,0 @@
<?php
namespace Kiri\Server;
use Kiri\Server\Abstracts\StatusEnum;
class WorkerStatus
{
public StatusEnum $enum = StatusEnum::START;
/**
* @param StatusEnum $enum
*/
public function setEnum(StatusEnum $enum): void
{
$this->enum = $enum;
}
/**
* @param StatusEnum $enum
* @return bool
*/
public function is(StatusEnum $enum): bool
{
return $this->enum == $enum;
}
}