Compare commits

...

8 Commits

Author SHA1 Message Date
as2252258 80cf72adc9 Revert "改名"
This reverts commit fdf58326
2022-01-04 16:06:29 +08:00
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
9 changed files with 60 additions and 47 deletions
+5 -5
View File
@@ -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;
} }
+4
View File
@@ -5,6 +5,7 @@ namespace Server\Handler;
use Note\Inject; use Note\Inject;
use Kiri\Events\EventDispatch; use Kiri\Events\EventDispatch;
use Kiri\Exception\ConfigException; use Kiri\Exception\ConfigException;
use ReflectionException;
use Server\Abstracts\Server; use Server\Abstracts\Server;
use Server\Events\OnBeforeShutdown; use Server\Events\OnBeforeShutdown;
use Server\Events\OnShutdown; use Server\Events\OnShutdown;
@@ -28,6 +29,7 @@ class OnServer extends Server
/** /**
* @param \Swoole\Server $server * @param \Swoole\Server $server
* @throws ConfigException * @throws ConfigException
* @throws ReflectionException
*/ */
public function onStart(\Swoole\Server $server) public function onStart(\Swoole\Server $server)
{ {
@@ -39,6 +41,7 @@ class OnServer extends Server
/** /**
* @param \Swoole\Server $server * @param \Swoole\Server $server
* @throws ReflectionException
*/ */
public function onBeforeShutdown(\Swoole\Server $server) public function onBeforeShutdown(\Swoole\Server $server)
{ {
@@ -48,6 +51,7 @@ class OnServer extends Server
/** /**
* @param \Swoole\Server $server * @param \Swoole\Server $server
* @throws ReflectionException
*/ */
public function onShutdown(\Swoole\Server $server) public function onShutdown(\Swoole\Server $server)
{ {
+4 -2
View File
@@ -4,6 +4,7 @@ namespace Server\Handler;
use Note\Inject; use Note\Inject;
use Kiri\Events\EventDispatch; use Kiri\Events\EventDispatch;
use ReflectionException;
use Server\Abstracts\Server; use Server\Abstracts\Server;
use Kiri\Exception\ConfigException; use Kiri\Exception\ConfigException;
use Server\Events\OnManagerStart; use Server\Events\OnManagerStart;
@@ -26,8 +27,8 @@ class OnServerManager extends Server
/** /**
* @param \Swoole\Server $server * @param \Swoole\Server $server
* @throws ConfigException * @throws ConfigException|ReflectionException
*/ */
public function onManagerStart(\Swoole\Server $server) public function onManagerStart(\Swoole\Server $server)
{ {
$this->setProcessName(sprintf('manger[%d].0', $server->manager_pid)); $this->setProcessName(sprintf('manger[%d].0', $server->manager_pid));
@@ -38,6 +39,7 @@ class OnServerManager extends Server
/** /**
* @param \Swoole\Server $server * @param \Swoole\Server $server
* @throws ReflectionException
*/ */
public function onManagerStop(\Swoole\Server $server) public function onManagerStop(\Swoole\Server $server)
{ {
+2
View File
@@ -25,6 +25,7 @@ class OnServerReload
/** /**
* @param Server $server * @param Server $server
* @throws \ReflectionException
*/ */
public function onBeforeReload(Server $server) public function onBeforeReload(Server $server)
{ {
@@ -34,6 +35,7 @@ class OnServerReload
/** /**
* @param Server $server * @param Server $server
* @throws \ReflectionException
*/ */
public function onAfterReload(Server $server) public function onAfterReload(Server $server)
{ {
+7 -7
View File
@@ -2,12 +2,12 @@
namespace Server\Handler; namespace Server\Handler;
use Note\Inject;
use Exception; use Exception;
use Kiri\Abstracts\Config; use Kiri\Abstracts\Config;
use Kiri\Core\Help; use Kiri\Core\Help;
use Kiri\Events\EventDispatch; use Kiri\Events\EventDispatch;
use Kiri\Kiri; use Kiri\Kiri;
use Note\Inject;
use Server\Events\OnAfterWorkerStart; use Server\Events\OnAfterWorkerStart;
use Server\Events\OnBeforeWorkerStart; use Server\Events\OnBeforeWorkerStart;
use Server\Events\OnTaskerStart as OnTaskStart; use Server\Events\OnTaskerStart as OnTaskStart;
@@ -62,8 +62,8 @@ class OnServerWorker extends \Server\Abstracts\Server
*/ */
public function onWorkerStop(Server $server, int $workerId) public function onWorkerStop(Server $server, int $workerId)
{ {
Timer::clearAll(); Timer::clearAll();
$this->eventDispatch->dispatch(new OnWorkerStop($server, $workerId)); $this->eventDispatch->dispatch(new OnWorkerStop($server, $workerId));
} }
@@ -76,8 +76,8 @@ class OnServerWorker extends \Server\Abstracts\Server
{ {
set_env('state', 'exit'); 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->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) protected function system_mail($messageContent)
{ {
try { try {
$email = Config::get('email'); $email = Config::get('email', ['enable' => false]);
if (!empty($email) && ($email['enable'] ?? false) == true) { if (!empty($email) && ($email['enable'] ?? false) == true) {
Help::sendEmail($email, 'Service Error', $messageContent); Help::sendEmail($email, 'Service Error', $messageContent);
} }
+22 -22
View File
@@ -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());
} }
@@ -141,7 +131,17 @@ class Server extends HttpService
*/ */
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);
} }
} }
+6
View File
@@ -10,6 +10,8 @@ use Kiri\Abstracts\Config;
use Kiri\Events\EventDispatch; use Kiri\Events\EventDispatch;
use Kiri\Exception\ConfigException; use Kiri\Exception\ConfigException;
use Kiri\Kiri; use Kiri\Kiri;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Server\Events\OnServerBeforeStart; use Server\Events\OnServerBeforeStart;
use Swoole\Coroutine; use Swoole\Coroutine;
use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Command\Command;
@@ -52,6 +54,10 @@ class ServerCommand extends Command
* @param InputInterface $input * @param InputInterface $input
* @param OutputInterface $output * @param OutputInterface $output
* @return int * @return int
* @throws ConfigException
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws \ReflectionException
* @throws Exception * @throws Exception
*/ */
public function execute(InputInterface $input, OutputInterface $output): int public function execute(InputInterface $input, OutputInterface $output): int
+10 -10
View File
@@ -116,7 +116,6 @@ class ServerManager extends Component
* @throws ConfigException * @throws ConfigException
* @throws ContainerExceptionInterface * @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface * @throws NotFoundExceptionInterface
* @throws ReflectionException
*/ */
public function addListener(string $type, string $host, int $port, int $mode, array $settings = []) public function addListener(string $type, string $host, int $port, int $mode, array $settings = [])
{ {
@@ -159,15 +158,17 @@ class ServerManager extends Component
$customProcess = Kiri::getDi()->get($customProcess); $customProcess = Kiri::getDi()->get($customProcess);
} }
$system = sprintf('[%s].process', Config::get('id', 'system-service')); $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()) { if (Kiri::getPlatform()->isLinux()) {
$process->name($system . '(' . $customProcess->getName() . ')'); $process->name($system . '(' . $customProcess->getName() . ')');
} }
$customProcess->onSigterm()->process($process); $customProcess->onSigterm()->process($process);
}, $customProcess->getRedirectStdinAndStdout(), $customProcess->getPipeType(), $customProcess->isEnableCoroutine()); },
$this->logger->debug($system . ' ' . $customProcess->getName() . ' start.'); $customProcess->getRedirectStdinAndStdout(),
$this->initProcesses[$customProcess->getName()] = $process; $customProcess->getPipeType(),
$this->server->addProcess($process); $customProcess->isEnableCoroutine()
));
} }
@@ -275,7 +276,6 @@ class ServerManager extends Component
* @throws ConfigException * @throws ConfigException
* @throws ContainerExceptionInterface * @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface * @throws NotFoundExceptionInterface
* @throws ReflectionException
* @throws Exception * @throws Exception
*/ */
private function createBaseServer(string $type, string $host, int $port, int $mode, array $settings = []) private function createBaseServer(string $type, string $host, int $port, int $mode, array $settings = [])
@@ -291,7 +291,7 @@ class ServerManager extends Component
$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);
} }
@@ -301,7 +301,6 @@ class ServerManager extends Component
* @param array $settings * @param array $settings
* @throws ContainerExceptionInterface * @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface * @throws NotFoundExceptionInterface
* @throws ReflectionException
*/ */
private function addDefaultListener(array $settings): void private function addDefaultListener(array $settings): void
{ {
@@ -321,7 +320,6 @@ class ServerManager extends Component
* @param Server|Port $server * @param Server|Port $server
* @throws ContainerExceptionInterface * @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface * @throws NotFoundExceptionInterface
* @throws ReflectionException
*/ */
private function addServiceEvents(array $events, Server|Port $server) private function addServiceEvents(array $events, Server|Port $server)
{ {
@@ -356,6 +354,7 @@ class ServerManager extends Component
/** /**
* @param array $events * @param array $events
* @return void
* @throws ContainerExceptionInterface * @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface * @throws NotFoundExceptionInterface
*/ */
@@ -374,6 +373,7 @@ class ServerManager extends Component
/** /**
* @param array|null $settings * @param array|null $settings
* @return void
* @throws ContainerExceptionInterface * @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface * @throws NotFoundExceptionInterface
*/ */
-1
View File
@@ -7,7 +7,6 @@ namespace Server\Tasker;
use Note\Inject; use Note\Inject;
use Kiri\Abstracts\Logger; use Kiri\Abstracts\Logger;
use Kiri\Exception\ConfigException; use Kiri\Exception\ConfigException;
use ReflectionException;
use Server\Contract\OnTaskInterface; use Server\Contract\OnTaskInterface;
use Swoole\Server; use Swoole\Server;