Compare commits

...

13 Commits

Author SHA1 Message Date
as2252258 dabcea452a 改名 2021-12-23 18:25:31 +08:00
as2252258 2feda943af 改名 2021-12-23 18:22:26 +08:00
as2252258 bffe375230 1 2021-12-17 04:20:20 +08:00
as2252258 efb69d02f3 改名 2021-12-08 11:46:08 +08:00
as2252258 79e5fa0b08 改名 2021-12-08 11:45:19 +08:00
as2252258 de7e005b46 改名 2021-12-08 11:32:31 +08:00
as2252258 a64fa8f493 改名 2021-12-08 11:16:35 +08:00
as2252258 c19e141f68 改名 2021-12-06 17:58:11 +08:00
as2252258 94a522588d 改名 2021-12-06 13:51:54 +08:00
as2252258 308c992fd3 改名 2021-12-06 11:47:12 +08:00
as2252258 458c6cbd22 改名 2021-12-03 16:05:49 +08:00
as2252258 b717a7e9f7 改名 2021-12-03 15:42:04 +08:00
as2252258 6786b313b5 改名 2021-12-03 14:46:38 +08:00
11 changed files with 109 additions and 77 deletions
+34 -6
View File
@@ -3,7 +3,7 @@
namespace Server\Abstracts;
use JetBrains\PhpStorm\Pure;
use Kiri\Context;
use Server\Contract\OnProcessInterface;
use Swoole\Coroutine;
use Swoole\Process;
@@ -17,7 +17,7 @@ abstract class BaseProcess implements OnProcessInterface
protected bool $isStop = false;
protected mixed $redirect_stdin_and_stdout = null;
protected bool $redirect_stdin_and_stdout = FALSE;
protected int $pipe_type = SOCK_DGRAM;
@@ -47,10 +47,10 @@ abstract class BaseProcess implements OnProcessInterface
}
/**
* @return mixed
*/
public function getRedirectStdinAndStdout(): mixed
{
* @return bool
*/
public function getRedirectStdinAndStdout(): bool
{
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;
}
}
+4
View File
@@ -5,6 +5,7 @@ namespace Server\Handler;
use Note\Inject;
use Kiri\Events\EventDispatch;
use Kiri\Exception\ConfigException;
use ReflectionException;
use Server\Abstracts\Server;
use Server\Events\OnBeforeShutdown;
use Server\Events\OnShutdown;
@@ -28,6 +29,7 @@ class OnServer extends Server
/**
* @param \Swoole\Server $server
* @throws ConfigException
* @throws ReflectionException
*/
public function onStart(\Swoole\Server $server)
{
@@ -39,6 +41,7 @@ class OnServer extends Server
/**
* @param \Swoole\Server $server
* @throws ReflectionException
*/
public function onBeforeShutdown(\Swoole\Server $server)
{
@@ -48,6 +51,7 @@ class OnServer extends Server
/**
* @param \Swoole\Server $server
* @throws ReflectionException
*/
public function onShutdown(\Swoole\Server $server)
{
+4 -2
View File
@@ -4,6 +4,7 @@ namespace Server\Handler;
use Note\Inject;
use Kiri\Events\EventDispatch;
use ReflectionException;
use Server\Abstracts\Server;
use Kiri\Exception\ConfigException;
use Server\Events\OnManagerStart;
@@ -26,8 +27,8 @@ class OnServerManager extends Server
/**
* @param \Swoole\Server $server
* @throws ConfigException
*/
* @throws ConfigException|ReflectionException
*/
public function onManagerStart(\Swoole\Server $server)
{
$this->setProcessName(sprintf('manger[%d].0', $server->manager_pid));
@@ -38,6 +39,7 @@ class OnServerManager extends Server
/**
* @param \Swoole\Server $server
* @throws ReflectionException
*/
public function onManagerStop(\Swoole\Server $server)
{
+2
View File
@@ -25,6 +25,7 @@ class OnServerReload
/**
* @param Server $server
* @throws \ReflectionException
*/
public function onBeforeReload(Server $server)
{
@@ -34,6 +35,7 @@ class OnServerReload
/**
* @param Server $server
* @throws \ReflectionException
*/
public function onAfterReload(Server $server)
{
+7 -7
View File
@@ -2,12 +2,12 @@
namespace Server\Handler;
use Note\Inject;
use Exception;
use Kiri\Abstracts\Config;
use Kiri\Core\Help;
use Kiri\Events\EventDispatch;
use Kiri\Kiri;
use Note\Inject;
use Server\Events\OnAfterWorkerStart;
use Server\Events\OnBeforeWorkerStart;
use Server\Events\OnTaskerStart as OnTaskStart;
@@ -62,8 +62,8 @@ class OnServerWorker extends \Server\Abstracts\Server
*/
public function onWorkerStop(Server $server, int $workerId)
{
Timer::clearAll();
$this->eventDispatch->dispatch(new OnWorkerStop($server, $workerId));
Timer::clearAll();
$this->eventDispatch->dispatch(new OnWorkerStop($server, $workerId));
}
@@ -76,8 +76,8 @@ class OnServerWorker extends \Server\Abstracts\Server
{
set_env('state', 'exit');
$this->eventDispatch->dispatch(new OnWorkerExit($server, $workerId));
}
$this->eventDispatch->dispatch(new OnWorkerExit($server, $workerId));
}
/**
@@ -98,7 +98,7 @@ class OnServerWorker extends \Server\Abstracts\Server
$this->logger->error($message);
// $this->system_mail($message);
$this->system_mail($message);
}
@@ -109,7 +109,7 @@ class OnServerWorker extends \Server\Abstracts\Server
protected function system_mail($messageContent)
{
try {
$email = Config::get('email');
$email = Config::get('email', ['enable' => false]);
if (!empty($email) && ($email['enable'] ?? false) == true) {
Help::sendEmail($email, 'Service Error', $messageContent);
}
+24 -23
View File
@@ -5,10 +5,10 @@ namespace Server;
use Exception;
use Http\Handler\Abstracts\HttpService;
use JetBrains\PhpStorm\Pure;
use Kiri\Abstracts\Config;
use Kiri\Events\EventDispatch;
use Kiri\Exception\ConfigException;
use Kiri\Kiri;
use Note\Inject;
use Psr\Container\ContainerExceptionInterface;
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;
/** @var EventDispatch */
#[Inject(EventDispatch::class)]
public EventDispatch $eventDispatch;
/**
* @var State
*/
@@ -77,36 +65,38 @@ class Server extends HttpService
*/
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', []);
if (!empty($rpcService)) {
$this->manager->addListener($rpcService['type'], $rpcService['host'], $rpcService['port'],
$this->manager()->addListener($rpcService['type'], $rpcService['host'], $rpcService['port'],
$rpcService['mode'], $rpcService);
}
$processes = array_merge($this->process, Config::get('processes', []));
foreach ($processes as $process) {
$this->manager->addProcess($process);
$this->manager()->addProcess($process);
}
return $this->manager->getServer()->start();
return $this->manager()->getServer()->start();
}
/**
* @return void
*
* start server
* @throws ConfigException
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws \ReflectionException
* @throws Exception
*/
public function shutdown()
{
$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->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
* @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);
}
}
+7 -1
View File
@@ -10,6 +10,8 @@ use Kiri\Abstracts\Config;
use Kiri\Events\EventDispatch;
use Kiri\Exception\ConfigException;
use Kiri\Kiri;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Server\Events\OnServerBeforeStart;
use Swoole\Coroutine;
use Symfony\Component\Console\Command\Command;
@@ -52,6 +54,10 @@ class ServerCommand extends Command
* @param InputInterface $input
* @param OutputInterface $output
* @return int
* @throws ConfigException
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws \ReflectionException
* @throws Exception
*/
public function execute(InputInterface $input, OutputInterface $output): int
@@ -105,7 +111,7 @@ class ServerCommand extends Command
$this->configure_set();
Kiri::app()->getRouter()->read_files();
$manager->start();
return 1;
+23 -23
View File
@@ -3,15 +3,14 @@
namespace Server;
use Exception;
use Kiri\Abstracts\Component;
use Kiri\Abstracts\Config;
use Kiri\Di\Container;
use Kiri\Error\Logger;
use Kiri\Events\EventDispatch;
use Kiri\Exception\ConfigException;
use Kiri\Kiri;
use Note\Inject;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
use ReflectionException;
use Server\Abstracts\BaseProcess;
@@ -41,7 +40,7 @@ use Swoole\WebSocket\Server as WServer;
* Class OnServerManager
* @package Http\Service
*/
class ServerManager
class ServerManager extends Component
{
@@ -59,14 +58,6 @@ class ServerManager
#[Inject(Logger::class)]
public Logger $logger;
/**
* @var State
*/
#[Inject(State::class)]
public State $state;
/** @var array<string,Port> */
public array $ports = [];
@@ -76,11 +67,7 @@ class ServerManager
private Server|null $server = null;
/**
* @var Container
*/
#[Inject(ContainerInterface::class)]
public ContainerInterface $container;
protected array $initProcesses = [];
const DEFAULT_EVENT = [
@@ -172,15 +159,26 @@ class ServerManager
$customProcess = Kiri::getDi()->get($customProcess);
}
$system = sprintf('[%s].process', Config::get('id', 'system-service'));
$process = new Process(function (Process $process) use ($customProcess, $system) {
$this->logger->debug($system . ' ' . $customProcess->getName() . ' start.');
$this->server->addProcess(new Process(function (Process $process) use ($customProcess, $system) {
if (Kiri::getPlatform()->isLinux()) {
$process->name($system . '(' . $customProcess->getName() . ')');
}
$customProcess->process($process);
}, $customProcess->getRedirectStdinAndStdout(), $customProcess->getPipeType(), $customProcess->isEnableCoroutine());
$this->logger->debug($system . ' ' . $customProcess->getName() . ' start.');
$this->container->setBindings($customProcess->getName(), $process);
$this->server->addProcess($process);
$customProcess->onSigterm()->process($process);
},
$customProcess->getRedirectStdinAndStdout(),
$customProcess->getPipeType(),
$customProcess->isEnableCoroutine()
));
}
/**
* @return array<string,Process>
*/
public function getProcesses(): array
{
return $this->initProcesses;
}
@@ -295,7 +293,7 @@ class ServerManager
$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);
}
@@ -360,6 +358,7 @@ class ServerManager
/**
* @param array $events
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws ReflectionException
@@ -379,6 +378,7 @@ class ServerManager
/**
* @param array|null $settings
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws ReflectionException
+2 -2
View File
@@ -3,11 +3,11 @@
namespace Server;
use Exception;
use Kiri\Abstracts\BaseObject;
use Kiri\Abstracts\Component;
use Kiri\Abstracts\Config;
use Swoole\Process;
class State extends BaseObject
class State extends Component
{
use TraitServer;
+2 -12
View File
@@ -3,12 +3,9 @@
namespace Server\Tasker;
use Exception;
use Kiri\Abstracts\BaseObject;
use Kiri\Abstracts\Component;
use Kiri\Core\HashMap;
use Kiri\Di\Container;
use Kiri\Kiri;
use Note\Inject;
use Psr\Container\ContainerInterface;
use ReflectionException;
use Server\Contract\OnTaskInterface;
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;
/**
* @var Container
*/
#[Inject(ContainerInterface::class)]
public ContainerInterface $container;
private HashMap $hashMap;
-1
View File
@@ -7,7 +7,6 @@ namespace Server\Tasker;
use Note\Inject;
use Kiri\Abstracts\Logger;
use Kiri\Exception\ConfigException;
use ReflectionException;
use Server\Contract\OnTaskInterface;
use Swoole\Server;