This commit is contained in:
xl
2024-08-29 17:01:07 +08:00
parent b14b18040b
commit 917e82064a
8 changed files with 34 additions and 75 deletions
+4 -11
View File
@@ -35,13 +35,6 @@ class AsyncServer implements ServerInterface
private ?Server $server = null;
/**
* @var ContainerInterface
*/
#[Container(ContainerInterface::class)]
public ContainerInterface $container;
/**
* @param array $service
* @param int $daemon
@@ -110,7 +103,7 @@ class AsyncServer implements ServerInterface
$config->events[Constant::SHUTDOWN] = [OnServer::class, 'onShutdown'];
}
$this->_listenDump($config);
$this->container->bind(ServerInterface::class, $this->server);
\Kiri::getDi()->bind(ServerInterface::class, $this->server);
}
@@ -123,7 +116,7 @@ class AsyncServer implements ServerInterface
if (!isset($this->server->setting[Constant::OPTION_TASK_WORKER_NUM])) {
return;
}
$this->container->get(Task::class)->initTaskWorker($this->server);
\Kiri::getDi()->get(Task::class)->initTaskWorker($this->server);
}
@@ -170,7 +163,7 @@ class AsyncServer implements ServerInterface
*/
protected function _listenDump(SConfig $config): void
{
$writeln = $this->container->get(OutputInterface::class);
$writeln = \Kiri::getDi()->get(OutputInterface::class);
if ($config->type == Constant::SERVER_TYPE_HTTP) {
$writeln->writeln('Add http port listen ' . $config->host . '::' . $config->port);
} else if ($config->type == Constant::SERVER_TYPE_WEBSOCKET) {
@@ -214,7 +207,7 @@ class AsyncServer implements ServerInterface
{
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);
}
+1 -15
View File
@@ -44,20 +44,6 @@ abstract class BaseProcess implements OnProcessInterface
protected bool $enable_queue = false;
/**
* @var StdoutLogger
*/
#[Container(LoggerInterface::class)]
public StdoutLogger $logger;
/**
* @var \Kiri\Di\Container
*/
#[Container(ContainerInterface::class)]
public \Kiri\Di\Container $container;
/**
* @var string
*/
@@ -141,7 +127,7 @@ abstract class BaseProcess implements OnProcessInterface
{
$this->stop = true;
$value = Context::get('waite:process:message');
$this->logger->alert('Process ' . $this->getName() . ' stop');
\Kiri::getLogger()->alert('Process ' . $this->getName() . ' stop');
if (!is_null($value) && Coroutine::exists((int)$value)) {
Coroutine::cancel((int)$value);
}
+2 -9
View File
@@ -24,13 +24,6 @@ trait TraitServer
private array $_process = [];
/**
* @var StdoutLogger
*/
#[Container(LoggerInterface::class)]
public StdoutLogger $logger;
/**
* @param string|array|BaseProcess $class
* @return void
@@ -64,7 +57,7 @@ trait TraitServer
private function genProcess(BaseProcess $name): Process
{
return new Process(function (Process $process) use ($name) {
$process->name('[' . \config('id','system-service') . ']' . $name->getName());
$process->name('[' . \config('id','system-service') . '].' . $name->getName());
$name->onSigterm()->process($process);
},
$name->getRedirectStdinAndStdout(),
@@ -111,7 +104,7 @@ trait TraitServer
public function onSigint($no, array $signInfo): void
{
try {
$this->logger->alert('Pid ' . getmypid() . ' get signo ' . $no);
Kiri::getLogger()->alert('Pid ' . getmypid() . ' get signo ' . $no);
$this->shutdown();
} catch (\Throwable $exception) {
error($exception);
+3 -4
View File
@@ -63,16 +63,15 @@ class OnRequest implements OnRequestInterface
* @param DataGrip $dataGrip
* @throws
*/
public function __construct(public ResponseInterface $response, public ContainerInterface $container,
public DataGrip $dataGrip)
public function __construct(public ResponseInterface $response, DataGrip $dataGrip)
{
$this->responseEmitter = $this->response->emmit;
$exception = \config('exception.http');
if (!in_array(ExceptionHandlerInterface::class, class_implements($exception))) {
$exception = ExceptionHandlerDispatcher::class;
}
$this->exception = $this->container->get($exception);
$this->router = $this->dataGrip->get(ROUTER_TYPE_HTTP);
$this->exception = \Kiri::getDi()->get($exception);
$this->router = $dataGrip->get(ROUTER_TYPE_HTTP);
}
+5 -12
View File
@@ -26,14 +26,6 @@ use function config;
class OnServerWorker extends Kiri\Server\Abstracts\Server
{
/**
* @var EventDispatch
*/
#[Container(EventDispatch::class)]
public EventDispatch $dispatch;
/**
* @return void
*/
@@ -68,13 +60,14 @@ class OnServerWorker extends Kiri\Server\Abstracts\Server
*/
public function onWorkerStart(Server $server, int $workerId): void
{
$this->dispatch->dispatch(new OnBeforeWorkerStart($server, $workerId));
$dispatch = Kiri::getDi()->get(EventDispatch::class);
$dispatch->dispatch(new OnBeforeWorkerStart($server, $workerId));
if ($workerId < $server->setting['worker_num']) {
$this->dispatch->dispatch(new OnWorkerStart($server, $workerId));
$dispatch->dispatch(new OnWorkerStart($server, $workerId));
} else {
$this->dispatch->dispatch(new OnTaskerStart($server, $workerId));
$dispatch->dispatch(new OnTaskerStart($server, $workerId));
}
$this->dispatch->dispatch(new OnAfterWorkerStart($server, $workerId));
$dispatch->dispatch(new OnAfterWorkerStart($server, $workerId));
}
+7 -7
View File
@@ -37,9 +37,9 @@ class HotReload extends BaseProcess
/**
* @param Router $router
*/
public function __construct(public Router $router)
public function __construct(Router $router)
{
on(OnWorkerStart::class, [$this->router, 'scan_build_route']);
on(OnWorkerStart::class, [$router, 'scan_build_route']);
$this->forceFile = \config('reload.forceFile', false);
}
@@ -61,7 +61,7 @@ class HotReload extends BaseProcess
{
// TODO: Implement onSigterm() method.
if (Context::inCoroutine()) {
Coroutine::create(fn() => $this->onShutdown(Coroutine::waitSignal(SIGTERM | SIGINT)));
Coroutine::create(fn () => $this->onShutdown(Coroutine::waitSignal(SIGTERM | SIGINT)));
} else {
\pcntl_signal(SIGTERM, [$this, 'onStop']);
}
@@ -127,8 +127,8 @@ class HotReload extends BaseProcess
}
$this->watch(rtrim($dir, '/'));
}
Event::add($this->inotify, fn() => $this->check());
Event::cycle(fn() => function () {
Event::add($this->inotify, fn () => $this->check());
Event::cycle(fn () => function () {
Event::dispatch();
}, true);
Event::wait();
@@ -262,7 +262,7 @@ class HotReload extends BaseProcess
if (Context::exists('swoole_timer_after')) {
return;
}
$int = @swoole_timer_after(2000, fn() => $this->reload());
$int = @swoole_timer_after(2000, fn () => $this->reload());
Context::set('swoole_timer_after', $int);
Context::set('isReloading', true);
}
@@ -308,7 +308,7 @@ class HotReload extends BaseProcess
*/
public function trigger_reload(): void
{
$this->logger->failure('Wait trigger server Reload' . PHP_EOL);
\Kiri::getLogger()->failure('Wait trigger server Reload' . PHP_EOL);
di(ServerInterface::class)->reload(false);
}
+3 -5
View File
@@ -25,17 +25,15 @@ class Task
/**
* @param ContainerInterface $container
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*
*/
public function __construct(public ContainerInterface $container)
public function __construct()
{
$exception = \config('exception.task');
if (!in_array(ExceptionHandlerInterface::class, class_implements($exception))) {
$exception = ExceptionHandlerDispatcher::class;
}
$this->exception = $this->container->get($exception);
$this->exception = Kiri::getDi()->get($exception);
}
+9 -12
View File
@@ -8,15 +8,6 @@ use Kiri\Server\ServerInterface;
class TaskExecute implements TaskInterface
{
/**
* @var ServerInterface
*/
#[Container(ServerInterface::class)]
public ServerInterface $server;
/**
* @param mixed $data
* @param float $timeout
@@ -25,7 +16,9 @@ class TaskExecute implements TaskInterface
*/
public function taskWait(mixed $data, float $timeout = 0.5, int $dstWorkerId = -1): mixed
{
return $this->server->taskwait($data, $timeout, $dstWorkerId);
$server = \Kiri::getDi()->get(ServerInterface::class);
return $server->taskwait($data, $timeout, $dstWorkerId);
}
@@ -36,7 +29,9 @@ class TaskExecute implements TaskInterface
*/
public function taskCo(array $tasks, float $timeout = 0.5): false|array
{
return $this->server->taskCo($tasks, $timeout);
$server = \Kiri::getDi()->get(ServerInterface::class);
return $server->taskCo($tasks, $timeout);
}
@@ -47,7 +42,9 @@ class TaskExecute implements TaskInterface
*/
public function taskWaitMulti(array $tasks, float $timeout = 0.5): false|array
{
return $this->server->taskWaitMulti($tasks, $timeout);
$server = \Kiri::getDi()->get(ServerInterface::class);
return $server->taskWaitMulti($tasks, $timeout);
}