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
-5
View File
@@ -37,11 +37,6 @@ class AsyncServer implements ServerInterface
private Server|null $server = null;
#[Container(LoggerInterface::class)]
public LoggerInterface $logger;
/**
* @param array $service
* @param int $daemon
+3 -11
View File
@@ -26,23 +26,14 @@ class ProcessManager extends Component
private array $_process = [];
/**
* @var ContainerInterface
*/
#[Container(ContainerInterface::class)]
public ContainerInterface $container;
#[Container(EventProvider::class)]
public EventProvider $provider;
/**
* @return void
* @throws Exception
*/
public function init(): void
{
$this->provider->on(OnServerBeforeStart::class, [$this, 'OnServerBeforeStart']);
$provider = Kiri::getDi()->get(EventProvider::class);
$provider->on(OnServerBeforeStart::class, [$this, 'OnServerBeforeStart']);
}
@@ -190,6 +181,7 @@ class ProcessManager extends Component
* @param Process $process
* @return void
* @throws Kiri\Exception\ConfigException
* @throws \ReflectionException
*/
public function extracted(mixed $custom, Process $process): void
{
-8
View File
@@ -17,14 +17,6 @@ use Kiri\Di\Inject\Container;
abstract class Server
{
/**
* @var LoggerInterface
*/
#[Container(LoggerInterface::class)]
public LoggerInterface $logger;
/**
* Server constructor.
* @throws Exception
+15 -15
View File
@@ -84,14 +84,14 @@ class CoroutineServer implements ServerInterface
\Kiri::service()->set('server', $this);
$this->processManager->batch(Config::get('processes', []));
$processManager = \Kiri::getDi()->get(ProcessManager::class);
$processManager->batch(Config::get('processes', []));
}
/**
* @param SConfig $config
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws ReflectionException
*/
public function addListener(SConfig $config): void
{
@@ -99,10 +99,10 @@ class CoroutineServer implements ServerInterface
$events = $config->getEvents()[Constant::REQUEST] ?? null;
if (is_null($events)) {
$events = [\Kiri\Message\Server::class, 'onRequest'];
$events = [\Kiri\Router\Server::class, 'onRequest'];
}
$events[0] = $this->container->get($events[0]);
$events[0] = \Kiri::getDi()->get($events[0]);
$server->handle('/', $events);
$this->servers[] = $server;
@@ -130,7 +130,8 @@ class CoroutineServer implements ServerInterface
$server->shutdown();
}
$this->dispatch->dispatch(new OnShutdown());
$dispatch = \Kiri::getDi()->get(EventDispatch::class);
$dispatch->dispatch(new OnShutdown());
return true;
}
@@ -156,14 +157,13 @@ class CoroutineServer implements ServerInterface
* @param Server\Port|Server $base
* @param array $events
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws ReflectionException
*/
private function onEventListen(Server\Port|Server $base, array $events): void
{
foreach ($events as $name => $event) {
if (is_array($event) && is_string($event[0])) {
$event[0] = $this->container->get($event[0]);
$event[0] = \Kiri::getDi()->get($event[0]);
}
$base->on($name, $event);
}
@@ -176,7 +176,8 @@ class CoroutineServer implements ServerInterface
public function start(): void
{
Coroutine\run(function () {
$this->dispatch->dispatch(new OnServerBeforeStart());
$dispatch = \Kiri::getDi()->get(EventDispatch::class);
$dispatch->dispatch(new OnServerBeforeStart());
$this->onSignal(Config::get('signal', []));
@@ -199,9 +200,7 @@ class CoroutineServer implements ServerInterface
/**
* @return void
* @throws ConfigException
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws ReflectionException
*/
private function onTasker(): void
{
@@ -220,9 +219,10 @@ class CoroutineServer implements ServerInterface
return;
}
$taskEvents[0] = $this->container->get($taskEvents[0]);
$container = \Kiri::getDi();
$taskEvents[0] = $container->get($taskEvents[0]);
if (!is_null($finishEvents)) {
$finishEvents[0] = $this->container->get($finishEvents[0]);
$finishEvents[0] = $container->get($finishEvents[0]);
}
$this->channel = new Coroutine\Channel($config[Constant::OPTION_TASK_WORKER_NUM]);
+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)