This commit is contained in:
xl
2023-06-12 15:34:08 +08:00
parent 30e611a655
commit b5d5826fa9
+101 -100
View File
@@ -31,132 +31,133 @@ class Server
/** /**
* @var string|mixed * @var string|mixed
*/ */
private string $class; private string $class;
/** /**
* @var int * @var int
*/ */
private int $daemon = 0; private int $daemon = 0;
/** /**
* *
*/ */
public function __construct() public function __construct()
{ {
$this->class = \config('server.type', AsyncServer::class); $this->class = \config('server.type', AsyncServer::class);
} }
/** /**
* @throws ReflectionException * @throws ReflectionException
*/ */
private function manager(): AsyncServer private function manager(): AsyncServer
{ {
return Kiri::getDi()->get($this->class); return Kiri::getDi()->get($this->class);
} }
/** /**
* @param $process * @param $process
* @throws Exception * @throws Exception
*/ */
public function addProcess($process): void public function addProcess($process): void
{ {
$this->manager()->addProcess($process); $this->manager()->addProcess($process);
} }
/** /**
* @return void * @return void
* @throws Exception * @throws Exception
*/ */
public function start(): void public function start(): void
{ {
on(OnWorkerStop::class, [Timer::class, 'clearAll'], 9999); on(OnWorkerStop::class, [Timer::class, 'clearAll'], 9999);
on(OnWorkerStart::class, [$this, 'setWorkerName']); on(OnWorkerStart::class, [$this, 'setWorkerName']);
on(OnTaskerStart::class, [$this, 'setTaskerName']); on(OnTaskerStart::class, [$this, 'setTaskerName']);
$manager = Kiri::getDi()->get(Router::class); $manager = Kiri::getDi()->get(Router::class);
$manager->scan_build_route(); $manager->scan_build_route();
$manager = $this->manager(); $manager = $this->manager();
$manager->initCoreServers(\config('server', [], true), $this->daemon); $manager->initCoreServers(\config('server', [], true), $this->daemon);
$manager->start(); $manager->start();
} }
/** /**
* @param OnWorkerStart $onWorkerStart * @param OnWorkerStart $onWorkerStart
*/ */
public function setWorkerName(OnWorkerStart $onWorkerStart): void public function setWorkerName(OnWorkerStart $onWorkerStart): void
{ {
if (!property_exists($onWorkerStart->server, 'worker_pid')) { if (!property_exists($onWorkerStart->server, 'worker_pid')) {
return; return;
} }
$prefix = sprintf('Worker Process[%d].%d', $onWorkerStart->server->worker_pid, $onWorkerStart->workerId); $prefix = sprintf('Worker Process[%d].%d', $onWorkerStart->server->worker_pid, $onWorkerStart->workerId);
set_env('environmental', Kiri::WORKER); set_env('environmental', Kiri::WORKER);
Kiri::setProcessName($prefix); Kiri::setProcessName($prefix);
} }
/** /**
* @param OnTaskerStart $onWorkerStart * @param OnTaskerStart $onWorkerStart
*/ */
public function setTaskerName(OnTaskerStart $onWorkerStart): void public function setTaskerName(OnTaskerStart $onWorkerStart): void
{ {
if (!property_exists($onWorkerStart->server, 'worker_pid')) { if (!property_exists($onWorkerStart->server, 'worker_pid')) {
return; return;
} }
$prefix = sprintf('Tasker Process[%d].%d', $onWorkerStart->server->worker_pid, $onWorkerStart->workerId); $prefix = sprintf('Tasker Process[%d].%d', $onWorkerStart->server->worker_pid, $onWorkerStart->workerId);
set_env('environmental', Kiri::TASK); set_env('environmental', Kiri::TASK);
Kiri::setProcessName($prefix); Kiri::setProcessName($prefix);
} }
/** /**
* @return void * @return void
* @throws ContainerExceptionInterface * @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface * @throws NotFoundExceptionInterface
* @throws Exception * @throws Exception
*/ */
public function shutdown(): void public function shutdown(): void
{ {
$configs = \config('server', [], true); $configs = \config('server', [], true);
$state = Kiri::getDi()->get(State::class); $state = Kiri::getDi()->get(State::class);
foreach ($this->manager()->sortService($configs['ports'] ?? []) as $config) { foreach ($this->manager()->sortService($configs['ports'] ?? []) as $config) {
$state->exit($config['port']); $state->exit($config['port']);
} }
$manager = Kiri::getDi()->get(EventDispatch::class); $manager = Kiri::getDi()->get(EventDispatch::class);
$manager->dispatch(new OnShutdown()); $manager->dispatch(new OnShutdown());
} }
/** /**
* @return bool * @return bool
* @throws Exception * @throws Exception
*/ */
public function isRunner(): bool public function isRunner(): bool
{ {
$state = Kiri::getDi()->get(State::class); $state = Kiri::getDi()->get(State::class);
return $state->isRunner(); return $state->isRunner();
} }
/** /**
* @param $daemon * @param $daemon
* @return Server * @return Server
*/ */
public function setDaemon($daemon): static public function setDaemon($daemon): static
{ {
if (!in_array($daemon, [0, 1])) { if (!in_array($daemon, [0, 1])) {
return $this; return $this;
} }
$this->daemon = $daemon; $this->daemon = $daemon;
return $this; return $this;
} }
} }