modify plugin name
This commit is contained in:
@@ -0,0 +1,195 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Server;
|
||||
|
||||
use Exception;
|
||||
use Kiri;
|
||||
use Kiri\Abstracts\Config;
|
||||
use Kiri\Di\ContainerInterface;
|
||||
use Kiri\Exception\ConfigException;
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
use ReflectionException;
|
||||
use Swoole\Server;
|
||||
use Kiri\Events\EventDispatch;
|
||||
use Kiri\Exception\NotFindClassException;
|
||||
use Kiri\Server\Events\OnServerBeforeStart;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
class AsyncServer
|
||||
{
|
||||
|
||||
use TraitServer;
|
||||
|
||||
|
||||
/**
|
||||
* @var Server|null
|
||||
*/
|
||||
private Server|null $server = null;
|
||||
|
||||
|
||||
/**
|
||||
* @param Config $config
|
||||
* @param ContainerInterface $container
|
||||
* @param EventDispatch $dispatch
|
||||
* @param ProcessManager $processManager
|
||||
*/
|
||||
public function __construct(public Config $config,
|
||||
public ContainerInterface $container,
|
||||
public EventDispatch $dispatch,
|
||||
public ProcessManager $processManager)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array $service
|
||||
* @param int $daemon
|
||||
* @return void
|
||||
* @throws ConfigException
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFindClassException
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
public function initCoreServers(array $service, int $daemon = 0): void
|
||||
{
|
||||
$service = $this->genConfigService($service);
|
||||
$this->createBaseServer(array_shift($service), $daemon);
|
||||
foreach ($service as $value) {
|
||||
$this->addListener($value);
|
||||
}
|
||||
$this->processManager->batch(Config::get('processes', []), $this->server);
|
||||
$this->processManager->batch($this->getProcess(), $this->server);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return Server|null
|
||||
*/
|
||||
public function getServer(string $name = ''): Server|null
|
||||
{
|
||||
return $this->server;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param \Kiri\Server\Config $config
|
||||
* @param int $daemon
|
||||
* @return void
|
||||
* @throws ConfigException
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFindClassException
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
private function createBaseServer(\Kiri\Server\Config $config, int $daemon = 0): void
|
||||
{
|
||||
$match = $this->getServerClass($config->type);
|
||||
if (is_null($match)) {
|
||||
throw new NotFindClassException('Unknown server type ' . $config->type);
|
||||
}
|
||||
$this->server = new $match($config->host, $config->port, SWOOLE_PROCESS, $config->mode);
|
||||
|
||||
$this->server->set($this->systemConfig($config, $daemon));
|
||||
|
||||
$this->onEventListen($this->server, Config::get('server.events', []));
|
||||
|
||||
$this->container->setBindings(ServerInterface::class, $this->server);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param \Kiri\Server\Config $config
|
||||
* @param int $daemon
|
||||
* @return array
|
||||
* @throws Exception
|
||||
* @throws ConfigException
|
||||
*/
|
||||
protected function systemConfig(\Kiri\Server\Config $config, int $daemon): array
|
||||
{
|
||||
$settings = array_merge(Config::get('server.settings', []), $config->settings);
|
||||
$settings[Constant::OPTION_DAEMONIZE] = (bool)$daemon;
|
||||
$settings[Constant::OPTION_ENABLE_REUSE_PORT] = true;
|
||||
$settings[Constant::OPTION_PID_FILE] = storage('.swoole.pid');
|
||||
if (!isset($settings[Constant::OPTION_PID_FILE])) {
|
||||
$settings[Constant::OPTION_LOG_FILE] = storage('system.log');
|
||||
}
|
||||
return $settings;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param \Kiri\Server\Config $config
|
||||
* @return void
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
* @throws Exception
|
||||
*/
|
||||
public function addListener(\Kiri\Server\Config $config): void
|
||||
{
|
||||
$port = $this->server->addlistener($config->host, $config->port, $config->mode);
|
||||
if ($port === false) {
|
||||
throw new Exception('Listen port fail.' . swoole_last_error());
|
||||
}
|
||||
|
||||
$port->set($this->resetSettings($config->type, $config->settings));
|
||||
|
||||
$this->onEventListen($port, $config->getEvents());
|
||||
Kiri::app()->set($config->getName(), $port);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $type
|
||||
* @param array $settings
|
||||
* @return array
|
||||
*/
|
||||
private function resetSettings(string $type, array $settings): array
|
||||
{
|
||||
if ($type == Constant::SERVER_TYPE_HTTP && !isset($settings['open_http_protocol'])) {
|
||||
$settings['open_http_protocol'] = true;
|
||||
if (in_array($this->server->setting['dispatch_mode'], [2, 4])) {
|
||||
$settings['open_http2_protocol'] = true;
|
||||
}
|
||||
}
|
||||
if ($type == Constant::SERVER_TYPE_WEBSOCKET && !isset($settings['open_websocket_protocol'])) {
|
||||
$settings['open_websocket_protocol'] = true;
|
||||
}
|
||||
return $settings;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Server\Port|Server $base
|
||||
* @param array $events
|
||||
* @return void
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
private function onEventListen(Server\Port|Server $base, array $events): void
|
||||
{
|
||||
foreach ($events as $name => $event) {
|
||||
if (is_array($event) && is_string($event[0])) {
|
||||
$event[0] = $this->container->get($event[0]);
|
||||
}
|
||||
$base->on($name, $event);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return void
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
public function start(): void
|
||||
{
|
||||
$this->dispatch->dispatch(new OnServerBeforeStart());
|
||||
$this->server->start();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Server\Abstracts;
|
||||
|
||||
use Kiri\Context;
|
||||
|
||||
class DoWhile
|
||||
{
|
||||
|
||||
private bool $isStop = false;
|
||||
|
||||
|
||||
/**
|
||||
* @param array|\Closure $handler
|
||||
* @return void
|
||||
*/
|
||||
public static function waite(array|\Closure $handler): void
|
||||
{
|
||||
if (Context::hasContext('stop')) {
|
||||
return;
|
||||
}
|
||||
$handler();
|
||||
self::waite($handler);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public static function stop(): void
|
||||
{
|
||||
Context::setContext('stop', 1);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,187 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Server;
|
||||
|
||||
use Closure;
|
||||
use Kiri;
|
||||
use Kiri\Abstracts\Config;
|
||||
use Kiri\Annotation\Inject;
|
||||
use Kiri\Context;
|
||||
use Kiri\Events\EventDispatch;
|
||||
use Kiri\Exception\ConfigException;
|
||||
use Kiri\Server\Abstracts\BaseProcess;
|
||||
use Kiri\Server\Broadcast\Message;
|
||||
use Kiri\Server\Contract\OnProcessInterface;
|
||||
use Kiri\Server\Events\OnProcessStart;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Swoole\Coroutine;
|
||||
use Swoole\Process;
|
||||
use Kiri\Server\Events\OnProcessStop;
|
||||
use Kiri\Di\ContainerInterface;
|
||||
use Swoole\Timer;
|
||||
|
||||
class ProcessManager
|
||||
{
|
||||
|
||||
|
||||
/** @var array<string, Process> */
|
||||
private array $_process = [];
|
||||
|
||||
|
||||
/**
|
||||
* @param ContainerInterface $container
|
||||
* @param LoggerInterface $logger
|
||||
*/
|
||||
public function __construct(public ContainerInterface $container, public LoggerInterface $logger)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string|OnProcessInterface|BaseProcess $customProcess
|
||||
* @return array
|
||||
* @throws ConfigException
|
||||
*/
|
||||
public function add(string|OnProcessInterface|BaseProcess $customProcess): array
|
||||
{
|
||||
if (is_string($customProcess)) {
|
||||
$customProcess = Kiri::getDi()->get($customProcess);
|
||||
}
|
||||
|
||||
$system = sprintf('[%s].Custom Process', Config::get('id', 'system-service'));
|
||||
|
||||
$this->logger->debug($system . ' ' . $customProcess->getName() . ' start.');
|
||||
if (Context::inCoroutine()) {
|
||||
return [$customProcess, $this->resolve($customProcess, $system)];
|
||||
}
|
||||
|
||||
$process = new Process($this->resolve($customProcess, $system),
|
||||
$customProcess->getRedirectStdinAndStdout(),
|
||||
$customProcess->getPipeType(),
|
||||
$customProcess->isEnableCoroutine()
|
||||
);
|
||||
|
||||
return [$customProcess, $process];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $customProcess
|
||||
* @param $system
|
||||
* @return Closure
|
||||
*/
|
||||
public function resolve($customProcess, $system): Closure
|
||||
{
|
||||
return static function () use ($customProcess, $system) {
|
||||
$process = func_get_arg(0);
|
||||
if ($process instanceof Process\Pool) {
|
||||
$process = $process->getProcess(func_get_arg(1));
|
||||
}
|
||||
set_env('environmental', Kiri::PROCESS);
|
||||
if (Kiri::getPlatform()->isLinux()) {
|
||||
$process->name($system . '(' . $customProcess->getName() . ')');
|
||||
}
|
||||
$dispatcher = Kiri::getDi()->get(EventDispatch::class);
|
||||
$dispatcher->dispatch(new OnProcessStart());
|
||||
$customProcess->onSigterm()->process($process);
|
||||
$dispatcher->dispatch(new OnProcessStop($process));
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string|null $name
|
||||
* @param string $tag
|
||||
* @return array|Process|null
|
||||
*/
|
||||
public function get(?string $name = null, string $tag = 'default'): array|Process|null
|
||||
{
|
||||
$process = $this->_process[$tag] ?? null;
|
||||
if (empty($process)) {
|
||||
return null;
|
||||
}
|
||||
if (!empty($name)) {
|
||||
if (!isset($process[$name])) {
|
||||
return null;
|
||||
}
|
||||
return $process[$name];
|
||||
}
|
||||
return $process;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function stop(): void
|
||||
{
|
||||
foreach ($this->_process as $process) {
|
||||
$process->exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array $processes
|
||||
* @param \Swoole\Server|null $server
|
||||
* @return void
|
||||
* @throws ConfigException
|
||||
*/
|
||||
public function batch(array $processes, ?\Swoole\Server $server = null): void
|
||||
{
|
||||
$processes = array_merge($processes, Config::get('processes', []));
|
||||
|
||||
if (Context::inCoroutine()) {
|
||||
$this->poolManager($processes);
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($processes as $process) {
|
||||
[$customProcess, $sProcess] = $this->add($process);
|
||||
|
||||
$this->_process[$customProcess->getName()] = $customProcess;
|
||||
|
||||
$server->addProcess($sProcess);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array $processes
|
||||
* @return void
|
||||
* @throws ConfigException
|
||||
*/
|
||||
protected function poolManager(array $processes): void
|
||||
{
|
||||
$manager = new Process\Manager();
|
||||
foreach ($processes as $process) {
|
||||
/** @var BaseProcess $customProcess */
|
||||
[$customProcess, $sProcess] = $this->add($process);
|
||||
|
||||
$this->_process[$customProcess->getName()] = $customProcess;
|
||||
|
||||
$manager->add($sProcess, $customProcess->isEnableCoroutine());
|
||||
}
|
||||
$manager->start();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $message
|
||||
* @param string $name
|
||||
* @param string $tag
|
||||
* @return void
|
||||
*/
|
||||
public function push(string $message, string $name = '', string $tag = 'default'): void
|
||||
{
|
||||
$processes = $this->_process;
|
||||
if (!empty($this->_process[$name])) {
|
||||
$processes = [$this->_process[$name]];
|
||||
}
|
||||
foreach ($processes as $process) {
|
||||
$process->write(serialize(new Message($message)));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
+1
-18
@@ -21,29 +21,12 @@ abstract class Server
|
||||
|
||||
|
||||
/**
|
||||
* @var LoggerInterface
|
||||
* @var LoggerInterface
|
||||
*/
|
||||
#[Inject(LoggerInterface::class)]
|
||||
public LoggerInterface $logger;
|
||||
|
||||
|
||||
/**
|
||||
* @param $prefix
|
||||
* @throws ConfigException
|
||||
*/
|
||||
protected function setProcessName($prefix)
|
||||
{
|
||||
if (Kiri::getPlatform()->isMac()) {
|
||||
return;
|
||||
}
|
||||
$name = '[' . Config::get('id', 'system-service') . ']';
|
||||
if (!empty($prefix)) {
|
||||
$name .= '.' . $prefix;
|
||||
}
|
||||
swoole_set_process_name($name);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Server constructor.
|
||||
* @throws Exception
|
||||
|
||||
Reference in New Issue
Block a user