Compare commits
11 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| bffe375230 | |||
| efb69d02f3 | |||
| 79e5fa0b08 | |||
| de7e005b46 | |||
| a64fa8f493 | |||
| c19e141f68 | |||
| 94a522588d | |||
| 308c992fd3 | |||
| 458c6cbd22 | |||
| b717a7e9f7 | |||
| 6786b313b5 |
@@ -3,7 +3,7 @@
|
|||||||
namespace Server\Abstracts;
|
namespace Server\Abstracts;
|
||||||
|
|
||||||
|
|
||||||
use JetBrains\PhpStorm\Pure;
|
use Kiri\Context;
|
||||||
use Server\Contract\OnProcessInterface;
|
use Server\Contract\OnProcessInterface;
|
||||||
use Swoole\Coroutine;
|
use Swoole\Coroutine;
|
||||||
use Swoole\Process;
|
use Swoole\Process;
|
||||||
@@ -17,7 +17,7 @@ abstract class BaseProcess implements OnProcessInterface
|
|||||||
protected bool $isStop = false;
|
protected bool $isStop = false;
|
||||||
|
|
||||||
|
|
||||||
protected mixed $redirect_stdin_and_stdout = null;
|
protected bool $redirect_stdin_and_stdout = FALSE;
|
||||||
|
|
||||||
|
|
||||||
protected int $pipe_type = SOCK_DGRAM;
|
protected int $pipe_type = SOCK_DGRAM;
|
||||||
@@ -47,10 +47,10 @@ abstract class BaseProcess implements OnProcessInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return mixed
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function getRedirectStdinAndStdout(): mixed
|
public function getRedirectStdinAndStdout(): bool
|
||||||
{
|
{
|
||||||
return $this->redirect_stdin_and_stdout;
|
return $this->redirect_stdin_and_stdout;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -80,4 +80,32 @@ abstract class BaseProcess implements OnProcessInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public function onSigterm(): static
|
||||||
|
{
|
||||||
|
if (!Context::inCoroutine()) {
|
||||||
|
Process::signal(SIGTERM, fn($data) => $this->onShutdown($data));
|
||||||
|
} else {
|
||||||
|
Coroutine::create(function () {
|
||||||
|
$data = Coroutine::waitSignal(SIGTERM, -1);
|
||||||
|
if ($data) {
|
||||||
|
$this->onShutdown($data);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $data
|
||||||
|
*/
|
||||||
|
protected function onShutdown($data): void
|
||||||
|
{
|
||||||
|
$this->isStop = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+24
-23
@@ -5,10 +5,10 @@ namespace Server;
|
|||||||
|
|
||||||
use Exception;
|
use Exception;
|
||||||
use Http\Handler\Abstracts\HttpService;
|
use Http\Handler\Abstracts\HttpService;
|
||||||
use JetBrains\PhpStorm\Pure;
|
|
||||||
use Kiri\Abstracts\Config;
|
use Kiri\Abstracts\Config;
|
||||||
use Kiri\Events\EventDispatch;
|
use Kiri\Events\EventDispatch;
|
||||||
use Kiri\Exception\ConfigException;
|
use Kiri\Exception\ConfigException;
|
||||||
|
use Kiri\Kiri;
|
||||||
use Note\Inject;
|
use Note\Inject;
|
||||||
use Psr\Container\ContainerExceptionInterface;
|
use Psr\Container\ContainerExceptionInterface;
|
||||||
use Psr\Container\NotFoundExceptionInterface;
|
use Psr\Container\NotFoundExceptionInterface;
|
||||||
@@ -28,21 +28,9 @@ class Server extends HttpService
|
|||||||
];
|
];
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @Inject ServerManager
|
|
||||||
* @var null|ServerManager
|
|
||||||
*/
|
|
||||||
#[Inject(ServerManager::class)]
|
|
||||||
public ?ServerManager $manager = null;
|
|
||||||
|
|
||||||
private mixed $daemon = 0;
|
private mixed $daemon = 0;
|
||||||
|
|
||||||
|
|
||||||
/** @var EventDispatch */
|
|
||||||
#[Inject(EventDispatch::class)]
|
|
||||||
public EventDispatch $eventDispatch;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var State
|
* @var State
|
||||||
*/
|
*/
|
||||||
@@ -77,36 +65,38 @@ class Server extends HttpService
|
|||||||
*/
|
*/
|
||||||
public function start(): string
|
public function start(): string
|
||||||
{
|
{
|
||||||
$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'],
|
$this->manager()->addListener($rpcService['type'], $rpcService['host'], $rpcService['port'],
|
||||||
$rpcService['mode'], $rpcService);
|
$rpcService['mode'], $rpcService);
|
||||||
}
|
}
|
||||||
|
|
||||||
$processes = array_merge($this->process, Config::get('processes', []));
|
$processes = array_merge($this->process, Config::get('processes', []));
|
||||||
foreach ($processes as $process) {
|
foreach ($processes as $process) {
|
||||||
$this->manager->addProcess($process);
|
$this->manager()->addProcess($process);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->manager->getServer()->start();
|
return $this->manager()->getServer()->start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return void
|
* @return void
|
||||||
*
|
* @throws ConfigException
|
||||||
* start server
|
* @throws ContainerExceptionInterface
|
||||||
|
* @throws NotFoundExceptionInterface
|
||||||
|
* @throws \ReflectionException
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function shutdown()
|
public function shutdown()
|
||||||
{
|
{
|
||||||
$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->container->get(EventDispatch::class)->dispatch(new OnShutdown());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -137,10 +127,21 @@ class Server extends HttpService
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @return \Swoole\Http\Server|\Swoole\Server|\Swoole\WebSocket\Server|null
|
* @return \Swoole\Http\Server|\Swoole\Server|\Swoole\WebSocket\Server|null
|
||||||
|
* @throws \ReflectionException
|
||||||
*/
|
*/
|
||||||
#[Pure] public function getServer(): \Swoole\Http\Server|\Swoole\Server|\Swoole\WebSocket\Server|null
|
public function getServer(): \Swoole\Http\Server|\Swoole\Server|\Swoole\WebSocket\Server|null
|
||||||
{
|
{
|
||||||
return $this->manager->getServer();
|
return $this->manager()->getServer();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return ServerManager
|
||||||
|
* @throws \ReflectionException
|
||||||
|
*/
|
||||||
|
private function manager(): ServerManager
|
||||||
|
{
|
||||||
|
return Kiri::getDi()->get(ServerManager::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -105,7 +105,7 @@ class ServerCommand extends Command
|
|||||||
$this->configure_set();
|
$this->configure_set();
|
||||||
|
|
||||||
Kiri::app()->getRouter()->read_files();
|
Kiri::app()->getRouter()->read_files();
|
||||||
|
|
||||||
$manager->start();
|
$manager->start();
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
|
|||||||
+18
-19
@@ -3,15 +3,15 @@
|
|||||||
namespace Server;
|
namespace Server;
|
||||||
|
|
||||||
use Exception;
|
use Exception;
|
||||||
|
use Kiri\Abstracts\Component;
|
||||||
use Kiri\Abstracts\Config;
|
use Kiri\Abstracts\Config;
|
||||||
use Kiri\Di\Container;
|
use Kiri\Di\NoteManager;
|
||||||
use Kiri\Error\Logger;
|
use Kiri\Error\Logger;
|
||||||
use Kiri\Events\EventDispatch;
|
use Kiri\Events\EventDispatch;
|
||||||
use Kiri\Exception\ConfigException;
|
use Kiri\Exception\ConfigException;
|
||||||
use Kiri\Kiri;
|
use Kiri\Kiri;
|
||||||
use Note\Inject;
|
use Note\Inject;
|
||||||
use Psr\Container\ContainerExceptionInterface;
|
use Psr\Container\ContainerExceptionInterface;
|
||||||
use Psr\Container\ContainerInterface;
|
|
||||||
use Psr\Container\NotFoundExceptionInterface;
|
use Psr\Container\NotFoundExceptionInterface;
|
||||||
use ReflectionException;
|
use ReflectionException;
|
||||||
use Server\Abstracts\BaseProcess;
|
use Server\Abstracts\BaseProcess;
|
||||||
@@ -41,7 +41,7 @@ use Swoole\WebSocket\Server as WServer;
|
|||||||
* Class OnServerManager
|
* Class OnServerManager
|
||||||
* @package Http\Service
|
* @package Http\Service
|
||||||
*/
|
*/
|
||||||
class ServerManager
|
class ServerManager extends Component
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
@@ -59,14 +59,6 @@ class ServerManager
|
|||||||
#[Inject(Logger::class)]
|
#[Inject(Logger::class)]
|
||||||
public Logger $logger;
|
public Logger $logger;
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var State
|
|
||||||
*/
|
|
||||||
#[Inject(State::class)]
|
|
||||||
public State $state;
|
|
||||||
|
|
||||||
|
|
||||||
/** @var array<string,Port> */
|
/** @var array<string,Port> */
|
||||||
public array $ports = [];
|
public array $ports = [];
|
||||||
|
|
||||||
@@ -76,11 +68,7 @@ class ServerManager
|
|||||||
private Server|null $server = null;
|
private Server|null $server = null;
|
||||||
|
|
||||||
|
|
||||||
/**
|
protected array $initProcesses = [];
|
||||||
* @var Container
|
|
||||||
*/
|
|
||||||
#[Inject(ContainerInterface::class)]
|
|
||||||
public ContainerInterface $container;
|
|
||||||
|
|
||||||
|
|
||||||
const DEFAULT_EVENT = [
|
const DEFAULT_EVENT = [
|
||||||
@@ -176,14 +164,23 @@ class ServerManager
|
|||||||
if (Kiri::getPlatform()->isLinux()) {
|
if (Kiri::getPlatform()->isLinux()) {
|
||||||
$process->name($system . '(' . $customProcess->getName() . ')');
|
$process->name($system . '(' . $customProcess->getName() . ')');
|
||||||
}
|
}
|
||||||
$customProcess->process($process);
|
$customProcess->onSigterm()->process($process);
|
||||||
}, $customProcess->getRedirectStdinAndStdout(), $customProcess->getPipeType(), $customProcess->isEnableCoroutine());
|
}, $customProcess->getRedirectStdinAndStdout(), $customProcess->getPipeType(), $customProcess->isEnableCoroutine());
|
||||||
$this->logger->debug($system . ' ' . $customProcess->getName() . ' start.');
|
$this->logger->debug($system . ' ' . $customProcess->getName() . ' start.');
|
||||||
$this->container->setBindings($customProcess->getName(), $process);
|
$this->initProcesses[$customProcess->getName()] = $process;
|
||||||
$this->server->addProcess($process);
|
$this->server->addProcess($process);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<string,Process>
|
||||||
|
*/
|
||||||
|
public function getProcesses(): array
|
||||||
|
{
|
||||||
|
return $this->initProcesses;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
@@ -295,7 +292,7 @@ class ServerManager
|
|||||||
|
|
||||||
$id = Config::get('id', 'system-service');
|
$id = Config::get('id', 'system-service');
|
||||||
|
|
||||||
$this->logger->debug(sprintf('[%s]' . $type . ' service %s::%d start', $id, $host, $port));
|
$this->logger->debug(sprintf('[%s].' . $type . ' service %s::%d start', $id, $host, $port));
|
||||||
|
|
||||||
$this->addDefaultListener($settings);
|
$this->addDefaultListener($settings);
|
||||||
}
|
}
|
||||||
@@ -360,6 +357,7 @@ class ServerManager
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $events
|
* @param array $events
|
||||||
|
* @return void
|
||||||
* @throws ContainerExceptionInterface
|
* @throws ContainerExceptionInterface
|
||||||
* @throws NotFoundExceptionInterface
|
* @throws NotFoundExceptionInterface
|
||||||
* @throws ReflectionException
|
* @throws ReflectionException
|
||||||
@@ -379,6 +377,7 @@ class ServerManager
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array|null $settings
|
* @param array|null $settings
|
||||||
|
* @return void
|
||||||
* @throws ContainerExceptionInterface
|
* @throws ContainerExceptionInterface
|
||||||
* @throws NotFoundExceptionInterface
|
* @throws NotFoundExceptionInterface
|
||||||
* @throws ReflectionException
|
* @throws ReflectionException
|
||||||
|
|||||||
@@ -3,11 +3,11 @@
|
|||||||
namespace Server;
|
namespace Server;
|
||||||
|
|
||||||
use Exception;
|
use Exception;
|
||||||
use Kiri\Abstracts\BaseObject;
|
use Kiri\Abstracts\Component;
|
||||||
use Kiri\Abstracts\Config;
|
use Kiri\Abstracts\Config;
|
||||||
use Swoole\Process;
|
use Swoole\Process;
|
||||||
|
|
||||||
class State extends BaseObject
|
class State extends Component
|
||||||
{
|
{
|
||||||
|
|
||||||
use TraitServer;
|
use TraitServer;
|
||||||
|
|||||||
@@ -3,12 +3,9 @@
|
|||||||
namespace Server\Tasker;
|
namespace Server\Tasker;
|
||||||
|
|
||||||
use Exception;
|
use Exception;
|
||||||
use Kiri\Abstracts\BaseObject;
|
use Kiri\Abstracts\Component;
|
||||||
use Kiri\Core\HashMap;
|
use Kiri\Core\HashMap;
|
||||||
use Kiri\Di\Container;
|
|
||||||
use Kiri\Kiri;
|
use Kiri\Kiri;
|
||||||
use Note\Inject;
|
|
||||||
use Psr\Container\ContainerInterface;
|
|
||||||
use ReflectionException;
|
use ReflectionException;
|
||||||
use Server\Contract\OnTaskInterface;
|
use Server\Contract\OnTaskInterface;
|
||||||
use Server\SwooleServerInterface;
|
use Server\SwooleServerInterface;
|
||||||
@@ -17,7 +14,7 @@ use Server\SwooleServerInterface;
|
|||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
class AsyncTaskExecute extends BaseObject
|
class AsyncTaskExecute extends Component
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
@@ -27,13 +24,6 @@ class AsyncTaskExecute extends BaseObject
|
|||||||
public ?SwooleServerInterface $server = null;
|
public ?SwooleServerInterface $server = null;
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var Container
|
|
||||||
*/
|
|
||||||
#[Inject(ContainerInterface::class)]
|
|
||||||
public ContainerInterface $container;
|
|
||||||
|
|
||||||
|
|
||||||
private HashMap $hashMap;
|
private HashMap $hashMap;
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user