This commit is contained in:
as2252258@163.com
2021-09-04 00:08:34 +08:00
parent 991095ae50
commit abd043ee15
10 changed files with 1503 additions and 1511 deletions
+2 -2
View File
@@ -66,13 +66,13 @@ class Redis implements StopHeartbeatCheck
*/ */
public function heartbeat_check(): void public function heartbeat_check(): void
{ {
if (env('state') == 'exit') { if (env('state','start') == 'exit') {
return; return;
} }
if ($this->_timer === -1 && Context::inCoroutine()) { if ($this->_timer === -1 && Context::inCoroutine()) {
$this->_timer = Timer::tick(1000, function () { $this->_timer = Timer::tick(1000, function () {
try { try {
if (env('state') == 'exit') { if (env('state','start') == 'exit') {
echo 'timer end.' . PHP_EOL; echo 'timer end.' . PHP_EOL;
} }
if (time() - $this->_last > 10 * 60) { if (time() - $this->_last > 10 * 60) {
-272
View File
@@ -1,272 +0,0 @@
<?php
namespace Kiri\Di;
use JetBrains\PhpStorm\Pure;
use ReflectionAttribute;
use ReflectionClass;
use ReflectionProperty;
trait Attributes
{
private array $_classTarget = [];
private array $_classMethodNote = [];
private array $_classMethod = [];
private array $_classPropertyNote = [];
private array $_classProperty = [];
private array $_mapping = [];
/**
* @param ReflectionClass $class
*/
protected function setTargetNote(ReflectionClass $class)
{
$className = $class->getName();
if (!isset($this->_classTarget[$className])) {
$this->_classTarget[$className] = [];
}
foreach ($class->getAttributes() as $attribute) {
if (!class_exists($attribute->getName())) {
continue;
}
$instance = $attribute->newInstance();
$this->_classTarget[$className][] = $instance;
$this->setMappingClass($attribute, $className);
}
}
/**
* @param ReflectionAttribute $attribute
* @param string $class
*/
private function setMappingClass(ReflectionAttribute $attribute, string $class)
{
if (!isset($this->_mapping[$attribute->getName()])) {
$this->_mapping[$attribute->getName()] = [];
}
if (!isset($this->_mapping[$attribute->getName()][$class])) {
$this->_mapping[$attribute->getName()][$class] = [];
}
}
/**
* @param ReflectionAttribute $attribute
* @param string $class
* @param string $method
* @param mixed $instance
*/
private function setMappingMethod(ReflectionAttribute $attribute, string $class, string $method, mixed $instance)
{
$this->setMappingClass($attribute, $class);
if (!isset($this->_mapping[$attribute->getName()][$class]['method'])) {
$this->_mapping[$attribute->getName()][$class]['method'] = [];
}
$this->_mapping[$attribute->getName()][$class]['method'][] = [$method => $instance];
}
/**
* @param ReflectionAttribute $attribute
* @param string $class
* @param string $property
* @param $instance
*/
private function setMappingProperty(ReflectionAttribute $attribute, string $class, string $property, $instance)
{
$this->setMappingClass($attribute, $class);
$mapping = $this->_mapping[$attribute->getName()][$class];
if (!isset($mapping['property'])) {
$mapping['property'] = [];
}
$mapping['property'][] = [$property => $instance];
$this->_mapping[$attribute->getName()][$class] = $mapping;
}
/**
* @param mixed $class
* @return array
*/
public function getTargetNote(mixed $class): array
{
if (!is_string($class)) {
$class = $class::class;
}
return $this->_classTarget[$class] ?? [];
}
/**
* @param ReflectionClass $class
*/
protected function setMethodNote(ReflectionClass $class)
{
$className = $class->getName();
$this->_classMethodNote[$className] = $this->_classMethod[$className] = [];
foreach ($class->getMethods() as $ReflectionMethod) {
$this->_classMethod[$className][$ReflectionMethod->getName()] = $ReflectionMethod;
$this->_classMethodNote[$className][$ReflectionMethod->getName()] = [];
foreach ($ReflectionMethod->getAttributes() as $attribute) {
if (!class_exists($attribute->getName())) {
continue;
}
$instance = $attribute->newInstance();
$this->_classMethodNote[$className][$ReflectionMethod->getName()][] = $instance;
$this->setMappingMethod($attribute, $className, $ReflectionMethod->getName(), $instance);
}
}
}
/**
* @param string $class
* @param string $method
* @return bool
*/
public function hasMethod(string $class, string $method): bool
{
return isset($this->_classMethod[$class]) && isset($this->_classMethod[$class][$method]);
}
/**
* @param ReflectionClass $class
* @return array
*/
#[Pure] public function getMethodNote(ReflectionClass $class): array
{
return $this->_classMethodNote[$class->getName()] ?? [];
}
/**
* @param ReflectionClass $class
*/
protected function setPropertyNote(ReflectionClass $class)
{
$className = $class->getName();
$this->_classProperty[$className] = $this->_classPropertyNote[$className] = [];
foreach ($class->getProperties(ReflectionProperty::IS_PRIVATE | ReflectionProperty::IS_PUBLIC |
ReflectionProperty::IS_PROTECTED) as $ReflectionMethod) {
$this->_classProperty[$className][$ReflectionMethod->getName()] = $ReflectionMethod;
foreach ($ReflectionMethod->getAttributes() as $attribute) {
if (!class_exists($attribute->getName())) {
continue;
}
$instance = $attribute->newInstance();
$this->_classPropertyNote[$className][$ReflectionMethod->getName()] = $instance;
$this->setMappingProperty($attribute, $className, $ReflectionMethod->getName(), $instance);
}
}
}
/**
* @param string $attribute
* @param string|null $class
* @return array[]
*/
public function getAttributeTrees(string $attribute, string $class = null): array
{
$mapping = $this->_mapping[$attribute] ?? [];
if (empty($mapping) || empty($class)) {
return $mapping;
}
return $mapping[$class] ?? [];
}
/**
* @param string $attribute
* @param string $class
* @param string|null $method
* @return array
*/
public function getMethodByAnnotation(string $attribute, string $class, string $method = null): mixed
{
$class = $this->getAttributeTrees($attribute, $class);
if (empty($class) || !isset($class['method']) || empty($method)) {
return $class['method'] ?? [];
}
foreach ($class['method'] as $value) {
$key = key($value);
if ($method == $key) {
return $value[$key];
}
}
return null;
}
/**
* @param string $attribute
* @param string $class
* @param string $method
* @return mixed
*/
public function getPropertyByAnnotation(string $attribute, string $class, string $method): mixed
{
$class = $this->getAttributeTrees($attribute, $class);
if (empty($class) || !isset($class['property'])) {
return [];
}
foreach ($class['property'] as $value) {
$key = key($value);
if ($method == $key) {
return $value[$key];
}
}
return null;
}
/**
* @param ReflectionClass|string $class
* @return array
* @throws \ReflectionException
*/
public function getMethods(ReflectionClass|string $class): array
{
if (is_string($class)) {
$class = $this->getReflect($class);
}
return $this->_classMethod[$class->getName()] ?? [];
}
/**
* @param ReflectionClass $class
* @return ReflectionProperty[]
*/
#[Pure] public function getProperty(ReflectionClass $class): array
{
return $this->_classProperty[$class->getName()] ?? [];
}
/**
* @param ReflectionClass $class
* @return array
*/
#[Pure] public function getPropertyNote(ReflectionClass $class): array
{
return $this->_classPropertyNote[$class->getName()] ?? [];
}
}
+7 -9
View File
@@ -34,8 +34,6 @@ use Server\ResponseInterface;
class Container extends BaseObject implements ContainerInterface class Container extends BaseObject implements ContainerInterface
{ {
use Attributes;
/** /**
* @var array * @var array
* *
@@ -200,7 +198,7 @@ class Container extends BaseObject implements ContainerInterface
*/ */
public function propertyInject(ReflectionClass $reflect, $object): mixed public function propertyInject(ReflectionClass $reflect, $object): mixed
{ {
foreach ($this->getPropertyNote($reflect) as $property => $inject) { foreach (NoteManager::getPropertyNote($reflect) as $property => $inject) {
/** @var Inject $inject */ /** @var Inject $inject */
$inject->execute($object, $property); $inject->execute($object, $property);
} }
@@ -216,7 +214,7 @@ class Container extends BaseObject implements ContainerInterface
*/ */
public function getMethodAttribute($className, $method = null): array public function getMethodAttribute($className, $method = null): array
{ {
$methods = $this->getMethodNote($this->getReflect($className)); $methods = NoteManager::getMethodNote($this->getReflect($className));
if (!empty($method)) { if (!empty($method)) {
return $methods[$method] ?? []; return $methods[$method] ?? [];
} }
@@ -232,7 +230,7 @@ class Container extends BaseObject implements ContainerInterface
*/ */
public function getClassReflectionProperty(string $class, string $property = null): ReflectionProperty|null|array public function getClassReflectionProperty(string $class, string $property = null): ReflectionProperty|null|array
{ {
$lists = $this->getProperty($this->getReflect($class)); $lists = NoteManager::getProperty($this->getReflect($class));
if (empty($lists)) { if (empty($lists)) {
return null; return null;
} }
@@ -272,9 +270,9 @@ class Container extends BaseObject implements ContainerInterface
if ($reflect->isAbstract() || $reflect->isTrait() || $reflect->isInterface()) { if ($reflect->isAbstract() || $reflect->isTrait() || $reflect->isInterface()) {
return $this->_reflection[$class] = $reflect; return $this->_reflection[$class] = $reflect;
} }
$this->setPropertyNote($reflect); NoteManager::setPropertyNote($reflect);
$this->setTargetNote($reflect); NoteManager::setTargetNote($reflect);
$this->setMethodNote($reflect); NoteManager::setMethodNote($reflect);
$construct = $reflect->getConstructor(); $construct = $reflect->getConstructor();
if (!empty($construct) && $construct->getNumberOfParameters() > 0) { if (!empty($construct) && $construct->getNumberOfParameters() > 0) {
$this->_constructs[$class] = $construct; $this->_constructs[$class] = $construct;
@@ -293,7 +291,7 @@ class Container extends BaseObject implements ContainerInterface
if (is_string($class)) { if (is_string($class)) {
$class = $this->getReflect($class); $class = $this->getReflect($class);
} }
return $this->getMethods($class); return NoteManager::getMethods($class);
} }
+272
View File
@@ -0,0 +1,272 @@
<?php
namespace Kiri\Di;
use JetBrains\PhpStorm\Pure;
use ReflectionAttribute;
use ReflectionClass;
use ReflectionProperty;
class NoteManager
{
private static array $_classTarget = [];
private static array $_classMethodNote = [];
private static array $_classMethod = [];
private static array $_classPropertyNote = [];
private static array $_classProperty = [];
private static array $_mapping = [];
/**
* @param ReflectionClass $class
*/
public static function setTargetNote(ReflectionClass $class)
{
$className = $class->getName();
if (!isset(static::$_classTarget[$className])) {
static::$_classTarget[$className] = [];
}
foreach ($class->getAttributes() as $attribute) {
if (!class_exists($attribute->getName())) {
continue;
}
$instance = $attribute->newInstance();
static::$_classTarget[$className][] = $instance;
self::setMappingClass($attribute, $className);
}
}
/**
* @param ReflectionAttribute $attribute
* @param string $class
*/
public static function setMappingClass(ReflectionAttribute $attribute, string $class)
{
if (!isset(static::$_mapping[$attribute->getName()])) {
static::$_mapping[$attribute->getName()] = [];
}
if (!isset(static::$_mapping[$attribute->getName()][$class])) {
static::$_mapping[$attribute->getName()][$class] = [];
}
}
/**
* @param ReflectionAttribute $attribute
* @param string $class
* @param string $method
* @param mixed $instance
*/
public static function setMappingMethod(ReflectionAttribute $attribute, string $class, string $method, mixed $instance)
{
self::setMappingClass($attribute, $class);
if (!isset(static::$_mapping[$attribute->getName()][$class]['method'])) {
static::$_mapping[$attribute->getName()][$class]['method'] = [];
}
static::$_mapping[$attribute->getName()][$class]['method'][] = [$method => $instance];
}
/**
* @param ReflectionAttribute $attribute
* @param string $class
* @param string $property
* @param $instance
*/
public static function setMappingProperty(ReflectionAttribute $attribute, string $class, string $property, $instance)
{
self::setMappingClass($attribute, $class);
$mapping = static::$_mapping[$attribute->getName()][$class];
if (!isset($mapping['property'])) {
$mapping['property'] = [];
}
$mapping['property'][] = [$property => $instance];
static::$_mapping[$attribute->getName()][$class] = $mapping;
}
/**
* @param mixed $class
* @return array
*/
public static function getTargetNote(mixed $class): array
{
if (!is_string($class)) {
$class = $class::class;
}
return static::$_classTarget[$class] ?? [];
}
/**
* @param ReflectionClass $class
*/
public static function setMethodNote(ReflectionClass $class)
{
$className = $class->getName();
static::$_classMethodNote[$className] = static::$_classMethod[$className] = [];
foreach ($class->getMethods() as $ReflectionMethod) {
static::$_classMethod[$className][$ReflectionMethod->getName()] = $ReflectionMethod;
static::$_classMethodNote[$className][$ReflectionMethod->getName()] = [];
foreach ($ReflectionMethod->getAttributes() as $attribute) {
if (!class_exists($attribute->getName())) {
continue;
}
$instance = $attribute->newInstance();
static::$_classMethodNote[$className][$ReflectionMethod->getName()][] = $instance;
self::setMappingMethod($attribute, $className, $ReflectionMethod->getName(), $instance);
}
}
}
/**
* @param string $class
* @param string $method
* @return bool
*/
public static function hasMethod(string $class, string $method): bool
{
return isset(static::$_classMethod[$class]) && isset(static::$_classMethod[$class][$method]);
}
/**
* @param ReflectionClass $class
* @return array
*/
#[Pure] public static function getMethodNote(ReflectionClass $class): array
{
return static::$_classMethodNote[$class->getName()] ?? [];
}
/**
* @param ReflectionClass $class
*/
public static function setPropertyNote(ReflectionClass $class)
{
$className = $class->getName();
static::$_classProperty[$className] = static::$_classPropertyNote[$className] = [];
foreach ($class->getProperties(ReflectionProperty::IS_PRIVATE | ReflectionProperty::IS_PUBLIC |
ReflectionProperty::IS_PROTECTED) as $ReflectionMethod) {
static::$_classProperty[$className][$ReflectionMethod->getName()] = $ReflectionMethod;
foreach ($ReflectionMethod->getAttributes() as $attribute) {
if (!class_exists($attribute->getName())) {
continue;
}
$instance = $attribute->newInstance();
static::$_classPropertyNote[$className][$ReflectionMethod->getName()] = $instance;
self::setMappingProperty($attribute, $className, $ReflectionMethod->getName(), $instance);
}
}
}
/**
* @param string $attribute
* @param string|null $class
* @return array[]
*/
public static function getAttributeTrees(string $attribute, string $class = null): array
{
$mapping = static::$_mapping[$attribute] ?? [];
if (empty($mapping) || empty($class)) {
return $mapping;
}
return $mapping[$class] ?? [];
}
/**
* @param string $attribute
* @param string $class
* @param string|null $method
* @return array
*/
public static function getMethodByAnnotation(string $attribute, string $class, string $method = null): mixed
{
$class = self::getAttributeTrees($attribute, $class);
if (empty($class) || !isset($class['method']) || empty($method)) {
return $class['method'] ?? [];
}
foreach ($class['method'] as $value) {
$key = key($value);
if ($method == $key) {
return $value[$key];
}
}
return null;
}
/**
* @param string $attribute
* @param string $class
* @param string $method
* @return mixed
*/
public static function getPropertyByAnnotation(string $attribute, string $class, string $method): mixed
{
$class = self::getAttributeTrees($attribute, $class);
if (empty($class) || !isset($class['property'])) {
return [];
}
foreach ($class['property'] as $value) {
$key = key($value);
if ($method == $key) {
return $value[$key];
}
}
return null;
}
/**
* @param ReflectionClass|string $class
* @return array
* @throws \ReflectionException
*/
public static function getMethods(ReflectionClass|string $class): array
{
if (is_string($class)) {
$class = self::getReflect($class);
}
return static::$_classMethod[$class->getName()] ?? [];
}
/**
* @param ReflectionClass $class
* @return ReflectionProperty[]
*/
#[Pure] public static function getProperty(ReflectionClass $class): array
{
return static::$_classProperty[$class->getName()] ?? [];
}
/**
* @param ReflectionClass $class
* @return array
*/
#[Pure] public static function getPropertyNote(ReflectionClass $class): array
{
return static::$_classPropertyNote[$class->getName()] ?? [];
}
}
+797 -796
View File
File diff suppressed because it is too large Load Diff
+1 -1
View File
@@ -89,7 +89,7 @@ class ApplicationStore
*/ */
#[Pure] public function getStatus(): string #[Pure] public function getStatus(): string
{ {
return env('state'); return env('state','start');
} }
} }
+415 -405
View File
@@ -27,466 +27,476 @@ use Swoole\WebSocket\Server as WServer;
class ServerManager class ServerManager
{ {
/** @var string */ /** @var string */
public string $host = ''; public string $host = '';
public int $port = 0; public int $port = 0;
/** @var array<string,Port> */ /** @var array<string,Port> */
public array $ports = []; public array $ports = [];
public int $mode = SWOOLE_TCP; public int $mode = SWOOLE_TCP;
private Server|null $server = null; private Server|null $server = null;
/** /**
* @return Server|WServer|HServer|null * @return Server|WServer|HServer|null
*/ */
public function getServer(): Server|WServer|HServer|null public function getServer(): Server|WServer|HServer|null
{ {
return $this->server; return $this->server;
} }
/** /**
* @param string $type * @param string $type
* @param string $host * @param string $host
* @param int $port * @param int $port
* @param int $mode * @param int $mode
* @param array $settings * @param array $settings
* @throws ReflectionException * @throws ReflectionException
* @throws ConfigException * @throws ConfigException
* @throws Exception * @throws Exception
*/ */
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 = [])
{ {
if ($this->checkPortIsAlready($port)) $this->stopServer($port); if ($this->checkPortIsAlready($port)) $this->stopServer($port);
if (!$this->server) { if (!$this->server) {
$this->createBaseServer($type, $host, $port, $mode, $settings); $this->createBaseServer($type, $host, $port, $mode, $settings);
} else { } else {
if (!isset($settings['settings'])) { if (!isset($settings['settings'])) {
$settings['settings'] = []; $settings['settings'] = [];
} }
$this->addNewListener($type, $host, $port, $mode, $settings); $this->addNewListener($type, $host, $port, $mode, $settings);
} }
} }
/** /**
* @throws NotFindClassException * @throws NotFindClassException
* @throws ReflectionException * @throws ReflectionException
* @throws ConfigException * @throws ConfigException
*/ */
public function initBaseServer($configs, int $daemon = 0): void public function initBaseServer($configs, int $daemon = 0): void
{ {
$context = di(ServerManager::class); $context = di(ServerManager::class);
foreach ($this->sortService($configs['ports']) as $config) { foreach ($this->sortService($configs['ports']) as $config) {
$this->startListenerHandler($context, $config, $daemon); $this->startListenerHandler($context, $config, $daemon);
} }
$this->bindCallback($this->server, [Constant::PIPE_MESSAGE => [OnPipeMessage::class, 'onPipeMessage']]); $this->bindCallback($this->server, [Constant::PIPE_MESSAGE => [OnPipeMessage::class, 'onPipeMessage']]);
$this->bindCallback($this->server, $this->getSystemEvents($configs)); $this->bindCallback($this->server, $this->getSystemEvents($configs));
} }
/** /**
* @return bool * @return bool
* @throws ConfigException * @throws ConfigException
* @throws Exception * @throws Exception
*/ */
public function isRunner(): bool public function isRunner(): bool
{ {
$configs = Config::get('server', [], true); $configs = Config::get('server', [], true);
foreach ($this->sortService($configs['ports']) as $config) { foreach ($this->sortService($configs['ports']) as $config) {
if ($this->checkPortIsAlready($config['port'])) { if ($this->checkPortIsAlready($config['port'])) {
return true; return true;
} }
} }
return false; return false;
} }
/** /**
* @param string|CustomProcess $customProcess * @param string|CustomProcess $customProcess
* @param null $redirect_stdin_and_stdout * @param null $redirect_stdin_and_stdout
* @param int|null $pipe_type * @param int|null $pipe_type
* @param bool $enable_coroutine * @param bool $enable_coroutine
* @throws Exception * @throws Exception
*/ */
public function addProcess(string|CustomProcess $customProcess, $redirect_stdin_and_stdout = null, ?int $pipe_type = SOCK_DGRAM, bool $enable_coroutine = true) public function addProcess(string|CustomProcess $customProcess, $redirect_stdin_and_stdout = null, ?int $pipe_type = SOCK_DGRAM, bool $enable_coroutine = true)
{ {
$process = $this->initProcess($customProcess, $redirect_stdin_and_stdout, $pipe_type, $enable_coroutine); $process = $this->initProcess($customProcess, $redirect_stdin_and_stdout, $pipe_type, $enable_coroutine);
$this->server->addProcess($process); $this->server->addProcess($process);
if ($customProcess instanceof CustomProcess) { if ($customProcess instanceof CustomProcess) {
Kiri::app()->addProcess($customProcess::class, $process); Kiri::app()->addProcess($customProcess::class, $process);
} else { } else {
Kiri::app()->addProcess($customProcess, $process); Kiri::app()->addProcess($customProcess, $process);
} }
} }
/** /**
* @param $customProcess * @param $customProcess
* @param $redirect_stdin_and_stdout * @param $redirect_stdin_and_stdout
* @param $pipe_type * @param $pipe_type
* @param $enable_coroutine * @param $enable_coroutine
* @return Process * @return Process
*/ */
private function initProcess($customProcess, $redirect_stdin_and_stdout, $pipe_type, $enable_coroutine): Process private function initProcess($customProcess, $redirect_stdin_and_stdout, $pipe_type, $enable_coroutine): Process
{ {
$server = $this->server; $server = $this->server;
return new Process(function (Process $soloProcess) use ($customProcess, $server) { return new Process(function (Process $soloProcess) use ($customProcess, $server) {
if (is_string($customProcess)) { if (is_string($customProcess)) {
$customProcess = Kiri::createObject($customProcess, [$server]); $customProcess = Kiri::createObject($customProcess, [$server]);
} }
$name = $customProcess->getProcessName($soloProcess); $name = $customProcess->getProcessName($soloProcess);
info(sprintf("Process %s start.", $name)); info(sprintf("Process %s start.", $name));
scan_directory(directory('app'), 'App'); scan_directory(directory('app'), 'App');
$system = sprintf('%s.process[%d]', Config::get('id', 'system-service'), $soloProcess->pid); $system = sprintf('%s.process[%d]', Config::get('id', 'system-service'), $soloProcess->pid);
if (Kiri::getPlatform()->isLinux()) { if (Kiri::getPlatform()->isLinux()) {
$soloProcess->name($system . '.' . $name . ' start.'); $soloProcess->name($system . '.' . $name . ' start.');
} }
$customProcess->signListen($soloProcess); $customProcess->signListen($soloProcess);
$customProcess->onHandler($soloProcess); $customProcess->onHandler($soloProcess);
}, },
$redirect_stdin_and_stdout, $redirect_stdin_and_stdout,
$pipe_type, $pipe_type,
$enable_coroutine $enable_coroutine
); );
} }
/** /**
* @return array * @return array
*/ */
public function getSetting(): array public function getSetting(): array
{ {
return $this->server->setting; return $this->server->setting;
} }
/** /**
* @param array $ports * @param array $ports
* @return array * @return array
*/ */
public function sortService(array $ports): array public function sortService(array $ports): array
{ {
$array = []; $array = [];
foreach ($ports as $port) { foreach ($ports as $port) {
if ($port['type'] == Constant::SERVER_TYPE_WEBSOCKET) { if ($port['type'] == Constant::SERVER_TYPE_WEBSOCKET) {
array_unshift($array, $port); array_unshift($array, $port);
} else if ($port['type'] == Constant::SERVER_TYPE_HTTP) { } else if ($port['type'] == Constant::SERVER_TYPE_HTTP) {
if (!empty($array) && $array[0]['type'] == Constant::SERVER_TYPE_WEBSOCKET) { if (!empty($array) && $array[0]['type'] == Constant::SERVER_TYPE_WEBSOCKET) {
$array[] = $port; $array[] = $port;
} else { } else {
array_unshift($array, $port); array_unshift($array, $port);
} }
} else { } else {
$array[] = $port; $array[] = $port;
} }
} }
return $array; return $array;
} }
/** /**
* @param array $configs * @param $key
* @return array * @param $value
*/ */
private function getSystemEvents(array $configs): array public static function setEnv(string $key, string|int $value): void
{ {
return array_intersect_key($configs['events'] ?? [], [ putenv(sprintf('%s=%s', $key, (string)$value));
Constant::SHUTDOWN => '', }
Constant::WORKER_START => '',
Constant::WORKER_ERROR => '',
Constant::WORKER_EXIT => '',
Constant::WORKER_STOP => '',
Constant::MANAGER_START => '',
Constant::MANAGER_STOP => '',
Constant::BEFORE_RELOAD => '',
Constant::AFTER_RELOAD => '',
Constant::START => '',
]);
}
/** /**
* @param ServerManager $context * @param array $configs
* @param array $config * @return array
* @param int $daemon */
* @throws ConfigException private function getSystemEvents(array $configs): array
* @throws ReflectionException {
* @throws Exception return array_intersect_key($configs['events'] ?? [], [
*/ Constant::SHUTDOWN => '',
private function startListenerHandler(ServerManager $context, array $config, int $daemon = 0) Constant::WORKER_START => '',
{ Constant::WORKER_ERROR => '',
if (!$this->server) { Constant::WORKER_EXIT => '',
$config = $this->mergeConfig($config, $daemon); Constant::WORKER_STOP => '',
} Constant::MANAGER_START => '',
$context->addListener( Constant::MANAGER_STOP => '',
$config['type'], $config['host'], $config['port'], $config['mode'], Constant::BEFORE_RELOAD => '',
$config); Constant::AFTER_RELOAD => '',
} Constant::START => '',
]);
}
/** /**
* @param $config * @param ServerManager $context
* @param $daemon * @param array $config
* @return array * @param int $daemon
* @throws Exception * @throws ConfigException
*/ * @throws ReflectionException
private function mergeConfig($config, $daemon): array * @throws Exception
{ */
$config['settings'] = $config['settings'] ?? []; private function startListenerHandler(ServerManager $context, array $config, int $daemon = 0)
if (!isset($config['settings']['daemonize']) || !$config['settings']['daemonize'] != $daemon) { {
$config['settings']['daemonize'] = $daemon; if (!$this->server) {
} $config = $this->mergeConfig($config, $daemon);
if (!isset($config['settings']['log_file'])) { }
$config['settings']['log_file'] = storage('system.log'); $context->addListener(
} $config['type'], $config['host'], $config['port'], $config['mode'],
$config['settings']['pid_file'] = storage('.swoole.pid'); $config);
$config['events'] = $config['events'] ?? []; }
return $config;
}
/** /**
* @param string $type * @param $config
* @param string $host * @param $daemon
* @param int $port * @return array
* @param int $mode * @throws Exception
* @param array $settings */
* @throws ReflectionException private function mergeConfig($config, $daemon): array
* @throws Exception {
*/ $config['settings'] = $config['settings'] ?? [];
private function addNewListener(string $type, string $host, int $port, int $mode, array $settings = []) if (!isset($config['settings']['daemonize']) || !$config['settings']['daemonize'] != $daemon) {
{ $config['settings']['daemonize'] = $daemon;
echo sprintf("\033[36m[" . date('Y-m-d H:i:s') . "]\033[0m $type service %s::%d start.", $host, $port) . PHP_EOL; }
/** @var Server\Port $service */ if (!isset($config['settings']['log_file'])) {
$this->ports[$port] = $this->server->addlistener($host, $port, $mode); $config['settings']['log_file'] = storage('system.log');
if ($this->ports[$port] === false) { }
throw new Exception("The port is already in use[$host::$port]"); $config['settings']['pid_file'] = storage('.swoole.pid');
} $config['events'] = $config['events'] ?? [];
$this->ports[$port]->set($settings['settings'] ?? []); return $config;
$this->addServiceEvents($settings['events'] ?? [], $this->ports[$port]); }
}
/** /**
* @param int $port * @param string $type
* @param string $event * @param string $host
* @return Closure|array|null * @param int $port
*/ * @param int $mode
public function getPortCallback(int $port, string $event): Closure|array|null * @param array $settings
{ * @throws ReflectionException
/** @var Server\Port $_port */ * @throws Exception
$_port = $this->ports[$port] ?? null; */
if (is_null($_port)) { private function addNewListener(string $type, string $host, int $port, int $mode, array $settings = [])
return null; {
} echo sprintf("\033[36m[" . date('Y-m-d H:i:s') . "]\033[0m $type service %s::%d start.", $host, $port) . PHP_EOL;
return $_port->getCallback($event); /** @var Server\Port $service */
} $this->ports[$port] = $this->server->addlistener($host, $port, $mode);
if ($this->ports[$port] === false) {
throw new Exception("The port is already in use[$host::$port]");
}
$this->ports[$port]->set($settings['settings'] ?? []);
$this->addServiceEvents($settings['events'] ?? [], $this->ports[$port]);
}
/** /**
* @param string $type * @param int $port
* @param string $host * @param string $event
* @param int $port * @return Closure|array|null
* @param int $mode */
* @param array $settings public function getPortCallback(int $port, string $event): Closure|array|null
* @throws ReflectionException {
* @throws ConfigException /** @var Server\Port $_port */
*/ $_port = $this->ports[$port] ?? null;
private function createBaseServer(string $type, string $host, int $port, int $mode, array $settings = []) if (is_null($_port)) {
{ return null;
$match = match ($type) { }
Constant::SERVER_TYPE_BASE, Constant::SERVER_TYPE_TCP, return $_port->getCallback($event);
Constant::SERVER_TYPE_UDP => Server::class, }
Constant::SERVER_TYPE_HTTP => HServer::class,
Constant::SERVER_TYPE_WEBSOCKET => WServer::class
};
$this->server = new $match($host, $port, SWOOLE_PROCESS, $mode);
$this->server->set(array_merge(Config::get('server.settings', []), $settings['settings']));
echo sprintf("\033[36m[" . date('Y-m-d H:i:s') . "]\033[0m $type service %s::%d start.", $host, $port) . PHP_EOL;
$this->addDefaultListener($type, $settings);
}
/** /**
* @param int $port * @param string $type
* @throws Exception * @param string $host
*/ * @param int $port
public function stopServer(int $port) * @param int $mode
{ * @param array $settings
if (!($pid = $this->checkPortIsAlready($port))) { * @throws ReflectionException
return; * @throws ConfigException
} */
while ($this->checkPortIsAlready($port)) { private function createBaseServer(string $type, string $host, int $port, int $mode, array $settings = [])
Process::kill($pid, SIGTERM); {
usleep(300); $match = match ($type) {
} Constant::SERVER_TYPE_BASE, Constant::SERVER_TYPE_TCP,
} Constant::SERVER_TYPE_UDP => Server::class,
Constant::SERVER_TYPE_HTTP => HServer::class,
Constant::SERVER_TYPE_WEBSOCKET => WServer::class
};
$this->server = new $match($host, $port, SWOOLE_PROCESS, $mode);
$this->server->set(array_merge(Config::get('server.settings', []), $settings['settings']));
echo sprintf("\033[36m[" . date('Y-m-d H:i:s') . "]\033[0m $type service %s::%d start.", $host, $port) . PHP_EOL;
$this->addDefaultListener($type, $settings);
}
/** /**
* @param $port * @param int $port
* @return bool|string * @throws Exception
* @throws Exception */
*/ public function stopServer(int $port)
private function checkPortIsAlready($port): bool|string {
{ if (!($pid = $this->checkPortIsAlready($port))) {
if (!Kiri::getPlatform()->isLinux()) { return;
exec("lsof -i :" . $port . " | grep -i 'LISTEN' | awk '{print $2}'", $output); }
if (empty($output)) return false; while ($this->checkPortIsAlready($port)) {
$output = explode(PHP_EOL, $output[0]); Process::kill($pid, SIGTERM);
return $output[0]; usleep(300);
} }
}
$serverPid = file_get_contents(storage('.swoole.pid'));
if (!empty($serverPid) && shell_exec('ps -ef | grep ' . $serverPid . ' | grep -v grep')) {
Process::kill($serverPid, SIGTERM);
}
exec('netstat -lnp | grep ' . $port . ' | grep "LISTEN" | awk \'{print $7}\'', $output);
if (empty($output)) {
return false;
}
return explode('/', $output[0])[0];
}
/** /**
* @param string $type * @param $port
* @param array $settings * @return bool|string
* @throws ReflectionException * @throws Exception
* @throws Exception */
*/ private function checkPortIsAlready($port): bool|string
private function addDefaultListener(string $type, array $settings): void {
{ if (!Kiri::getPlatform()->isLinux()) {
if (($this->server->setting['task_worker_num'] ?? 0) > 0) { exec("lsof -i :" . $port . " | grep -i 'LISTEN' | awk '{print $2}'", $output);
$this->addTaskListener($settings['events']); if (empty($output)) return false;
} $output = explode(PHP_EOL, $output[0]);
$this->addServiceEvents($settings['events'] ?? [], $this->server); return $output[0];
Kiri::getDi()->setBindings(SwooleServerInterface::class, }
$this->server);
} $serverPid = file_get_contents(storage('.swoole.pid'));
if (!empty($serverPid) && shell_exec('ps -ef | grep ' . $serverPid . ' | grep -v grep')) {
Process::kill($serverPid, SIGTERM);
}
exec('netstat -lnp | grep ' . $port . ' | grep "LISTEN" | awk \'{print $7}\'', $output);
if (empty($output)) {
return false;
}
return explode('/', $output[0])[0];
}
/** /**
* @param array $events * @param string $type
* @param Server|Port $server * @param array $settings
* @throws ReflectionException * @throws ReflectionException
*/ * @throws Exception
private function addServiceEvents(array $events, Server|Port $server) */
{ private function addDefaultListener(string $type, array $settings): void
foreach ($events as $name => $event) { {
if (is_array($event) && is_string($event[0])) { if (($this->server->setting['task_worker_num'] ?? 0) > 0) {
$event[0] = Kiri::getDi()->get($event[0], [$server]); $this->addTaskListener($settings['events']);
} }
$server->on($name, $event); $this->addServiceEvents($settings['events'] ?? [], $this->server);
} Kiri::getDi()->setBindings(SwooleServerInterface::class,
} $this->server);
}
/** /**
* * @param array $events
*/ * @param Server|Port $server
public function start() * @throws ReflectionException
{ */
$this->server->start(); private function addServiceEvents(array $events, Server|Port $server)
} {
foreach ($events as $name => $event) {
if (is_array($event) && is_string($event[0])) {
$event[0] = Kiri::getDi()->get($event[0], [$server]);
}
$server->on($name, $event);
}
}
/** /**
* @param string $class *
* @return object */
* @throws NotFindClassException public function start()
* @throws ReflectionException {
*/ $this->server->start();
private function getNewInstance(string $class): object }
{
return Kiri::getDi()->newObject($class);
}
/** /**
* @param TaskExecute|string $handler * @param string $class
* @param array $params * @return object
* @param int|null $workerId * @throws NotFindClassException
* @throws ReflectionException * @throws ReflectionException
* @throws Exception */
*/ private function getNewInstance(string $class): object
public function task(TaskExecute|string $handler, array $params = [], int $workerId = null) {
{ return Kiri::getDi()->newObject($class);
if ($workerId === null || $workerId <= $this->server->setting['worker_num']) { }
$workerId = random_int($this->server->setting['worker_num'] + 1,
$this->server->setting['worker_num'] + 1 + $this->server->setting['task_worker_num']);
}
if (is_string($handler)) {
$implements = Kiri::getDi()->getReflect($handler);
if (!in_array(TaskExecute::class, $implements->getInterfaceNames())) {
throw new Exception('Task must instance ' . TaskExecute::class);
}
$handler = $implements->newInstanceArgs($params);
}
$this->server->task(serialize($handler), $workerId);
}
/** /**
* @param mixed $message * @param TaskExecute|string $handler
* @param int $workerId * @param array $params
* @return mixed * @param int|null $workerId
*/ * @throws ReflectionException
public function sendMessage(mixed $message, int $workerId): mixed * @throws Exception
{ */
return $this->server?->sendMessage($message, $workerId); public function task(TaskExecute|string $handler, array $params = [], int $workerId = null)
} {
if ($workerId === null || $workerId <= $this->server->setting['worker_num']) {
$workerId = random_int($this->server->setting['worker_num'] + 1,
$this->server->setting['worker_num'] + 1 + $this->server->setting['task_worker_num']);
}
if (is_string($handler)) {
$implements = Kiri::getDi()->getReflect($handler);
if (!in_array(TaskExecute::class, $implements->getInterfaceNames())) {
throw new Exception('Task must instance ' . TaskExecute::class);
}
$handler = $implements->newInstanceArgs($params);
}
$this->server->task(serialize($handler), $workerId);
}
/** /**
* @param array $events * @param mixed $message
* @throws ReflectionException * @param int $workerId
*/ * @return mixed
private function addTaskListener(array $events = []): void */
{ public function sendMessage(mixed $message, int $workerId): mixed
$task_use_object = $this->server->setting['task_object'] ?? $this->server->setting['task_use_object'] ?? false; {
$reflect = Kiri::getDi()->getReflect(OnServerTask::class)?->newInstance(); return $this->server?->sendMessage($message, $workerId);
if ($task_use_object || $this->server->setting['task_enable_coroutine']) { }
$this->server->on('task', $events[Constant::TASK] ?? [$reflect, 'onCoroutineTask']);
} else {
$this->server->on('task', $events[Constant::TASK] ?? [$reflect, 'onTask']);
}
$this->server->on('finish', $events[Constant::FINISH] ?? [$reflect, 'onFinish']);
}
/** /**
* @param Port|Server $server * @param array $events
* @param array|null $settings * @throws ReflectionException
* @throws ReflectionException */
*/ private function addTaskListener(array $events = []): void
public function bindCallback(Port|Server $server, ?array $settings = []) {
{ $task_use_object = $this->server->setting['task_object'] ?? $this->server->setting['task_use_object'] ?? false;
// TODO: Implement bindCallback() method. $reflect = Kiri::getDi()->getReflect(OnServerTask::class)?->newInstance();
if (count($settings) < 1) { if ($task_use_object || $this->server->setting['task_enable_coroutine']) {
return; $this->server->on('task', $events[Constant::TASK] ?? [$reflect, 'onCoroutineTask']);
} } else {
foreach ($settings as $event_type => $callback) { $this->server->on('task', $events[Constant::TASK] ?? [$reflect, 'onTask']);
if ($this->server->getCallback($event_type) !== null) { }
continue; $this->server->on('finish', $events[Constant::FINISH] ?? [$reflect, 'onFinish']);
} }
if (is_array($callback) && !is_object($callback[0])) {
$callback[0] = Kiri::getDi()->get($callback[0]);
} /**
$this->server->on($event_type, $callback); * @param Port|Server $server
} * @param array|null $settings
} * @throws ReflectionException
*/
public function bindCallback(Port|Server $server, ?array $settings = [])
{
// TODO: Implement bindCallback() method.
if (count($settings) < 1) {
return;
}
foreach ($settings as $event_type => $callback) {
if ($this->server->getCallback($event_type) !== null) {
continue;
}
if (is_array($callback) && !is_object($callback[0])) {
$callback[0] = Kiri::getDi()->get($callback[0]);
}
$this->server->on($event_type, $callback);
}
}
} }
+5 -4
View File
@@ -14,6 +14,7 @@ use Server\Events\OnWorkerError;
use Server\Events\OnWorkerExit; use Server\Events\OnWorkerExit;
use Server\Events\OnWorkerStart; use Server\Events\OnWorkerStart;
use Server\Events\OnWorkerStop; use Server\Events\OnWorkerStop;
use Server\ServerManager;
use Swoole\Server; use Swoole\Server;
use Swoole\Timer; use Swoole\Timer;
@@ -54,8 +55,7 @@ class OnServerWorker extends \Server\Abstracts\Server
*/ */
public function setConfigure(OnBeforeWorkerStart $worker) public function setConfigure(OnBeforeWorkerStart $worker)
{ {
putenv('state=start'); ServerManager::setEnv('worker', $worker->workerId);
putenv('worker=' . $worker->workerId);
$serialize = file_get_contents(storage(Runtime::CONFIG_NAME)); $serialize = file_get_contents(storage(Runtime::CONFIG_NAME));
if (!empty($serialize)) { if (!empty($serialize)) {
Config::sets(unserialize($serialize)); Config::sets(unserialize($serialize));
@@ -83,7 +83,7 @@ class OnServerWorker extends \Server\Abstracts\Server
*/ */
public function onWorkerExit(Server $server, int $workerId) public function onWorkerExit(Server $server, int $workerId)
{ {
putenv('state=exit'); ServerManager::setEnv('state', 'exit');
$this->eventDispatch->dispatch(new OnWorkerExit($server, $workerId)); $this->eventDispatch->dispatch(new OnWorkerExit($server, $workerId));
} }
@@ -104,7 +104,8 @@ class OnServerWorker extends \Server\Abstracts\Server
$message = sprintf('Worker#%d::%d error stop. signal %d, exit_code %d, msg %s', $message = sprintf('Worker#%d::%d error stop. signal %d, exit_code %d, msg %s',
$worker_id, $worker_pid, $signal, $exit_code, swoole_strerror(swoole_last_error(), 9) $worker_id, $worker_pid, $signal, $exit_code, swoole_strerror(swoole_last_error(), 9)
); );
write($message, 'worker-exit');
$this->logger->error($message);
$this->system_mail($message); $this->system_mail($message);
} }
+4 -3
View File
@@ -12,6 +12,7 @@ use Kiri\Kiri;
use Kiri\Runtime; use Kiri\Runtime;
use Psr\EventDispatcher\EventDispatcherInterface; use Psr\EventDispatcher\EventDispatcherInterface;
use ReflectionException; use ReflectionException;
use Server\ServerManager;
class OnWorkerStart implements EventDispatcherInterface class OnWorkerStart implements EventDispatcherInterface
{ {
@@ -35,17 +36,17 @@ class OnWorkerStart implements EventDispatcherInterface
{ {
$isWorker = $event->workerId < $event->server->setting['worker_num']; $isWorker = $event->workerId < $event->server->setting['worker_num'];
$this->annotation->read(APP_PATH . 'app','App', $isWorker ? [] : [CONTROLLER_PATH]); $this->annotation->read(APP_PATH . 'app', 'App', $isWorker ? [] : [CONTROLLER_PATH]);
$this->interpretDirectory(); $this->interpretDirectory();
if ($isWorker) { if ($isWorker) {
putenv('environmental=' . Kiri::WORKER); ServerManager::setEnv('environmental', Kiri::WORKER);
Kiri::getFactory()->getRouter()->_loader(); Kiri::getFactory()->getRouter()->_loader();
echo sprintf("\033[36m[" . date('Y-m-d H:i:s') . "]\033[0m Worker[%d].%d start.", $event->server->worker_pid, $event->workerId) . PHP_EOL; echo sprintf("\033[36m[" . date('Y-m-d H:i:s') . "]\033[0m Worker[%d].%d start.", $event->server->worker_pid, $event->workerId) . PHP_EOL;
$this->setProcessName(sprintf('Worker[%d].%d', $event->server->worker_pid, $event->workerId)); $this->setProcessName(sprintf('Worker[%d].%d', $event->server->worker_pid, $event->workerId));
} else { } else {
putenv('environmental=' . Kiri::TASK); ServerManager::setEnv('environmental', Kiri::TASK);
echo sprintf("\033[36m[" . date('Y-m-d H:i:s') . "]\033[0m Tasker[%d].%d start.", $event->server->worker_pid, $event->workerId) . PHP_EOL; echo sprintf("\033[36m[" . date('Y-m-d H:i:s') . "]\033[0m Tasker[%d].%d start.", $event->server->worker_pid, $event->workerId) . PHP_EOL;
-19
View File
@@ -31,23 +31,4 @@ use Kiri\Kiri;
} }
/**
* @param static $params
* @param mixed $class
* @param mixed|null $method
* @return Router
* @throws Exception
*/
public static function execute(mixed $params, mixed $class, mixed $method = null): Router
{
// TODO: Implement setHandler() method.
$router = Kiri::app()->getRouter();
$path = $params->event . '::' . (is_null($params->uri) ? 'event' : $params->uri);
$router->addRoute($path, [di($class), $method], 'sw::socket');
return $router;
}
} }