This commit is contained in:
2023-04-22 02:04:31 +08:00
parent 3c0581c2f6
commit ba81504942
9 changed files with 446 additions and 401 deletions
+17 -61
View File
@@ -7,21 +7,18 @@ use Exception;
use Kiri;
use Kiri\Abstracts\Config;
use Kiri\Events\EventDispatch;
use Kiri\Events\EventProvider;
use Kiri\Exception\ConfigException;
use Kiri\Router\Router;
use Kiri\Server\Events\OnShutdown;
use Kiri\Server\Events\OnWorkerStart;
use Kiri\Server\Events\OnTaskerStart;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
use Kiri\Server\Events\OnWorkerStop;
use ReflectionException;
use Swoole\Coroutine;
use Kiri\Server\Abstracts\ProcessManager;
use Kiri\Server\Events\OnShutdown;
use Kiri\Server\Events\OnTaskerStart;
use Kiri\Server\Events\OnWorkerStart;
use Kiri\Server\Events\OnWorkerStop;
use Kiri\Server\Abstracts\AsyncServer;
use Kiri\Di\Inject\Container;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use ReflectionException;
use Swoole\Timer;
defined('PID_PATH') or define('PID_PATH', APP_PATH . 'storage/server.pid');
@@ -49,42 +46,21 @@ class Server
/**
* @return AsyncServer|CoroutineServer
* @throws ReflectionException
*/
private function manager(): AsyncServer|CoroutineServer
private function manager(): AsyncServer
{
return Kiri::getDi()->get($this->class);
}
/**
* @return void
*/
public function init(): void
{
$enable_coroutine = Config::get('server.settings.enable_coroutine', false);
if (!$enable_coroutine) {
return;
}
Coroutine::set([
'hook_flags' => SWOOLE_HOOK_ALL ^ SWOOLE_HOOK_BLOCKING_FUNCTION,
'enable_deadlock_check' => FALSE,
'exit_condition' => function () {
return Coroutine::stats()['coroutine_num'] === 0;
}
]);
}
/**
* @param $process
* @throws Exception
*/
public function addProcess($process): void
{
$manager = Kiri::getDi()->get(ProcessManager::class);
$manager->add($process);
$this->manager()->addProcess($process);
}
@@ -97,29 +73,21 @@ class Server
*/
public function start(): void
{
$this->onHotReload();
on(OnWorkerStop::class, [Timer::class, 'clearAll'], 9999);
on(OnWorkerStart::class, [$this, 'setWorkerName']);
on(OnTaskerStart::class, [$this, 'setTaskerName']);
$manager = Kiri::getDi()->get(Router::class);
$manager->scan_build_route();
$manager = $this->manager();
$manager->initCoreServers(Config::get('server', [], true), $this->daemon);
$manager->start();
}
/**
* @return void
* @throws Exception
*/
protected function onWorkerListener(): void
{
$manager = Kiri::getDi()->get(EventProvider::class);
$manager->on(OnWorkerStop::class, '\Swoole\Timer::clearAll', 9999);
$manager->on(OnWorkerStart::class, [$this, 'setWorkerName']);
$manager->on(OnTaskerStart::class, [$this, 'setTaskerName']);
}
/**
* @param OnWorkerStart $onWorkerStart
* @throws ConfigException
*/
public function setWorkerName(OnWorkerStart $onWorkerStart): void
{
@@ -148,18 +116,6 @@ class Server
}
/**
* @return void
* @throws Exception
*/
public function onHotReload(): void
{
$this->onWorkerListener();
$manager = Kiri::getDi()->get(Router::class);
$manager->scan_build_route();
}
/**
* @return void
* @throws ContainerExceptionInterface