modify plugin name

This commit is contained in:
2022-06-08 15:31:17 +08:00
parent e47c7f0813
commit 44b2cf8ee4
+164 -146
View File
@@ -8,6 +8,7 @@ use JetBrains\PhpStorm\Pure;
use Kiri; use Kiri;
use Kiri\Abstracts\Config; use Kiri\Abstracts\Config;
use Kiri\Events\EventDispatch; use Kiri\Events\EventDispatch;
use Kiri\Events\EventProvider;
use Kiri\Exception\ConfigException; use Kiri\Exception\ConfigException;
use Kiri\Message\Constrict\Request; use Kiri\Message\Constrict\Request;
use Kiri\Message\Constrict\RequestInterface; use Kiri\Message\Constrict\RequestInterface;
@@ -15,16 +16,18 @@ use Kiri\Message\Constrict\Response;
use Kiri\Message\Constrict\ResponseInterface; use Kiri\Message\Constrict\ResponseInterface;
use Kiri\Message\Handler\Abstracts\HttpService; use Kiri\Message\Handler\Abstracts\HttpService;
use Kiri\Message\Handler\Router; use Kiri\Message\Handler\Router;
use Kiri\Server\Events\OnBeforeShutdown;
use Kiri\Server\Events\OnServerBeforeStart; use Kiri\Server\Events\OnServerBeforeStart;
use Kiri\Server\Events\OnShutdown; use Kiri\Server\Events\OnShutdown;
use Kiri\Server\Events\OnWorkerStart;
use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface; use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface; use Psr\Container\NotFoundExceptionInterface;
use ReflectionException; use ReflectionException;
use Swoole\WebSocket\Server as WsServer;
use Swoole\Server as SServer;
use Swoole\Http\Server as HServer;
use Swoole\Coroutine; use Swoole\Coroutine;
use Kiri\Server\Events\OnBeforeShutdown;
use Kiri\Events\EventProvider;
use Kiri\Server\Events\OnWorkerStart;
defined('PID_PATH') or define('PID_PATH', APP_PATH . 'storage/server.pid'); defined('PID_PATH') or define('PID_PATH', APP_PATH . 'storage/server.pid');
@@ -36,175 +39,190 @@ defined('PID_PATH') or define('PID_PATH', APP_PATH . 'storage/server.pid');
class Server extends HttpService class Server extends HttpService
{ {
private array $process = []; private array $process = [];
private mixed $daemon = 0; private mixed $daemon = 0;
/** /**
* @param State $state * @param State $state
* @param ServerManager $manager * @param ServerManager $manager
* @param ContainerInterface $container * @param ContainerInterface $container
* @param ProcessManager $processManager * @param ProcessManager $processManager
* @param EventDispatch $eventDispatch * @param EventDispatch $dispatch
* @param EventProvider $eventProvider * @param EventProvider $provider
* @param Router $router * @param Router $router
* @param array $config * @param array $config
* @throws Exception * @throws Exception
*/ */
public function __construct(public State $state, public function __construct(public State $state,
public ServerManager $manager, public ServerManager $manager,
public ContainerInterface $container, public ContainerInterface $container,
public ProcessManager $processManager, public ProcessManager $processManager,
public EventDispatch $eventDispatch, public EventDispatch $dispatch,
public EventProvider $eventProvider, public EventProvider $provider,
public Router $router, public Router $router,
array $config = []) array $config = [])
{ {
parent::__construct($config); parent::__construct($config);
} }
/** /**
* @return void * @return void
* @throws ConfigException * @throws ConfigException
* @throws ContainerExceptionInterface * @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface * @throws NotFoundExceptionInterface
*/ */
public function init(): void public function init(): void
{ {
$this->container->mapping(ResponseInterface::class, Response::class); $this->container->mapping(ResponseInterface::class, Response::class);
$this->container->mapping(RequestInterface::class, Request::class); $this->container->mapping(RequestInterface::class, Request::class);
$enable_coroutine = Config::get('servers.settings.enable_coroutine', false); $enable_coroutine = Config::get('servers.settings.enable_coroutine', false);
if (!$enable_coroutine) { if (!$enable_coroutine) {
return; return;
} }
Coroutine::set([ Coroutine::set([
'hook_flags' => SWOOLE_HOOK_ALL ^ SWOOLE_HOOK_BLOCKING_FUNCTION, 'hook_flags' => SWOOLE_HOOK_ALL ^ SWOOLE_HOOK_BLOCKING_FUNCTION,
'enable_deadlock_check' => FALSE, 'enable_deadlock_check' => FALSE,
'exit_condition' => function () { 'exit_condition' => function () {
return Coroutine::stats()['coroutine_num'] === 0; return Coroutine::stats()['coroutine_num'] === 0;
} }
]); ]);
} }
/** /**
* @param $process * @param $process
*/ */
public function addProcess($process) public function addProcess($process)
{ {
$this->process[] = $process; $this->process[] = $process;
} }
/** /**
* @return void * @return void
* @throws ConfigException * @throws ConfigException
* @throws ContainerExceptionInterface * @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface * @throws NotFoundExceptionInterface
* @throws Exception * @throws Exception
*/ */
public function start(): void public function start(): void
{ {
$this->manager->initBaseServer(Config::get('server', [], true), $this->daemon); $this->manager->initBaseServer(Config::get('server', [], true), $this->daemon);
$rpcService = Config::get('rpc', []); $rpcService = Config::get('rpc', []);
if (!empty($rpcService)) { if (!empty($rpcService)) {
$this->manager->addListener($rpcService['type'], $rpcService['host'], $rpcService['port'], $rpcService['mode'], $rpcService); $this->manager->addListener($rpcService['type'], $rpcService['host'], $rpcService['port'], $rpcService['mode'], $rpcService);
} }
$reload = Config::get('reload.hot', false); $this->onHotReload();
if ($reload !== false) {
$this->eventProvider->on(OnWorkerStart::class, [$this, 'onWorkerStart']);
$this->process[] = Scaner::class; pcntl_signal(SIGINT, [$this, 'onSigint']);
} else { $processes = array_merge($this->process, Config::get('processes', []));
$this->onWorkerStart(); $this->processManager->batch($processes);
} $this->dispatch->dispatch(new OnServerBeforeStart());
$this->manager->start();
}
pcntl_signal(SIGINT, function () {
try { /**
$this->eventDispatch->dispatch(new OnBeforeShutdown()); * @return void
}catch (\Throwable $exception) { * @throws ConfigException
$this->logger->error($exception->getMessage()); * @throws ReflectionException
} */
public function onHotReload(): void
{
$reload = Config::get('reload.hot', false);
if ($reload !== false) {
$this->provider->on(OnWorkerStart::class, [$this, 'onWorkerStart']);
$this->process[] = Scaner::class;
} else {
$this->onWorkerStart();
}
}
/**
* @return void
*/
public function onSigint(): void
{
try {
$this->dispatch->dispatch(new OnBeforeShutdown());
} catch (\Throwable $exception) {
$this->logger->error($exception->getMessage());
} finally {
$this->manager->getServer()->shutdown(); $this->manager->getServer()->shutdown();
}); }
}
$processes = array_merge($this->process, Config::get('processes', []));
$this->processManager->batch($processes);
$this->eventDispatch->dispatch(new OnServerBeforeStart());
$this->manager->start();
}
/** /**
* @return void * @return void
* @throws ReflectionException * @throws ReflectionException
* @throws Exception * @throws Exception
*/ */
public function onWorkerStart(): void public function onWorkerStart(): void
{ {
scan_directory(MODEL_PATH, 'app\Model'); scan_directory(MODEL_PATH, 'app\Model');
$this->router->scan_build_route(); $this->router->scan_build_route();
} }
/** /**
* @return void * @return void
* @throws ConfigException * @throws ConfigException
* @throws ContainerExceptionInterface * @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface * @throws NotFoundExceptionInterface
* @throws Exception * @throws Exception
*/ */
public function shutdown(): void public function shutdown(): void
{ {
$configs = Config::get('server', [], true); $configs = Config::get('server', [], true);
foreach ($this->manager->sortService($configs['ports'] ?? []) as $config) { foreach ($this->manager->sortService($configs['ports'] ?? []) as $config) {
$this->state->exit($config['port']); $this->state->exit($config['port']);
} }
$this->eventDispatch->dispatch(new OnShutdown()); $this->dispatch->dispatch(new OnShutdown());
} }
/** /**
* @return bool * @return bool
* @throws ConfigException * @throws ConfigException
* @throws Exception * @throws Exception
*/ */
public function isRunner(): bool public function isRunner(): bool
{ {
return $this->state->isRunner(); return $this->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;
} }
/** /**
* @return \Swoole\Http\Server|\Swoole\Server|\Swoole\WebSocket\Server|null * @return HServer|SServer|WsServer|null
*/ */
#[Pure] public function getServer(): \Swoole\Http\Server|\Swoole\Server|\Swoole\WebSocket\Server|null #[Pure] public function getServer(): HServer|SServer|WsServer|null
{ {
return $this->manager->getServer(); return $this->manager->getServer();
} }
} }