Compare commits
108 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 7e43775b04 | |||
| dd4e9be06a | |||
| 73b01c288b | |||
| f4f48ebad7 | |||
| ef95f912cb | |||
| 8bbb6adcc7 | |||
| a855765365 | |||
| 3110e69350 | |||
| 4862e7e913 | |||
| 0a52bd502b | |||
| 95492e2863 | |||
| 3cc0a9bef2 | |||
| d743382868 | |||
| 47f1407118 | |||
| 04395f293d | |||
| a44b7a31ef | |||
| 493ef65ce8 | |||
| 3943142b86 | |||
| 786f96363b | |||
| bcf73a8cad | |||
| 2b152504ec | |||
| e554ad3642 | |||
| c801aec334 | |||
| 2c2c16819a | |||
| a48aa9a20f | |||
| 8dadcbed16 | |||
| 86dd776388 | |||
| d83d0871f5 | |||
| fc6baa83a7 | |||
| 35dd2803fc | |||
| e3527752a7 | |||
| 340e0f1686 | |||
| 2ad6153405 | |||
| 0f172ae62d | |||
| f4596bf02c | |||
| ef4f84f696 | |||
| 08d19c9c05 | |||
| e0537f2ee4 | |||
| 0edcf8c83f | |||
| 6ac2c73a8c | |||
| b1a5db0a6b | |||
| 8ec1a81ecd | |||
| b98de03317 | |||
| 251bca3d64 | |||
| b2a79ef0ed | |||
| d9451f5087 | |||
| 84d8dda8eb | |||
| 1d846a4903 | |||
| fe4f985315 | |||
| c5c1d991f2 | |||
| 9ec6573ca4 | |||
| 9800024d59 | |||
| 57b2214433 | |||
| 2f038e5637 | |||
| 0f238f488d | |||
| 59243a6fed | |||
| fbd7f874ac | |||
| 7aee3c8458 | |||
| f3446da34e | |||
| 755594b4bb | |||
| abcf5e9feb | |||
| f243e19b24 | |||
| 5d91b61f83 | |||
| d3ed35e948 | |||
| c5bfe8110a | |||
| 1c18ee94ee | |||
| f72c73766d | |||
| 092139c0b9 | |||
| 43a1fad278 | |||
| dbc4cb15dd | |||
| 758bd650b5 | |||
| ba804c4d9c | |||
| cac794ca20 | |||
| ea09d3e3ba | |||
| f6552c45ae | |||
| 114f5d0279 | |||
| 5638d171d0 | |||
| fa78d360b0 | |||
| ffc35090b9 | |||
| 5156a606e0 | |||
| 2393ef820f | |||
| 9986900b58 | |||
| 07c52ef08f | |||
| 0a6fdda720 | |||
| 841a6b4a19 | |||
| 873da66a90 | |||
| be9f4281e6 | |||
| 57be085166 | |||
| 454c77f37a | |||
| 323a4b8ff0 | |||
| c66e5120e1 | |||
| 1fa7d3a3ba | |||
| a290108ddd | |||
| 446fd740f2 | |||
| a040099b99 | |||
| 78fdc7c1ca | |||
| dfc249b56c | |||
| 577be309cc | |||
| 9798be99e6 | |||
| e3348a11c8 | |||
| 1643a839b3 | |||
| d4a15d26ed | |||
| 485d926dc0 | |||
| 6a5205e9a1 | |||
| 471f7a92bb | |||
| 429ffe561c | |||
| 27cdf5082f | |||
| 4c3751ca4d |
+48
-1
@@ -18,6 +18,7 @@ use Kiri\Exception\NotFindClassException;
|
||||
use Kiri\Kiri;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Server\ServerManager;
|
||||
use Swoole\Process;
|
||||
use Swoole\WebSocket\Server;
|
||||
|
||||
if (!function_exists('make')) {
|
||||
@@ -52,6 +53,38 @@ if (!function_exists('make')) {
|
||||
}
|
||||
|
||||
|
||||
if (!function_exists('checkPortIsAlready')) {
|
||||
|
||||
|
||||
/**
|
||||
* @param $port
|
||||
* @return bool|string
|
||||
* @throws Exception
|
||||
*/
|
||||
function checkPortIsAlready($port): bool|string
|
||||
{
|
||||
if (!Kiri::getPlatform()->isLinux()) {
|
||||
exec("lsof -i :" . $port . " | grep -i 'LISTEN' | awk '{print $2}'", $output);
|
||||
if (empty($output)) return false;
|
||||
$output = explode(PHP_EOL, $output[0]);
|
||||
return $output[0];
|
||||
}
|
||||
|
||||
$serverPid = file_get_contents(storage('.swoole.pid'));
|
||||
if (!empty($serverPid) && shell_exec('ps -ef | grep ' . $serverPid . ' | grep -v grep')) {
|
||||
Process::kill($serverPid, 0) && 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];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
if (!function_exists('done')) {
|
||||
|
||||
/**
|
||||
@@ -59,12 +92,26 @@ if (!function_exists('done')) {
|
||||
*/
|
||||
function done()
|
||||
{
|
||||
ServerManager::setEnv('state', 'exit');
|
||||
set_env('state', 'exit');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
if (!function_exists('set_env')) {
|
||||
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @param $value
|
||||
*/
|
||||
function set_env($key, $value)
|
||||
{
|
||||
putenv(sprintf('%s=%s', $key, $value));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (!function_exists('enable_file_modification_listening')) {
|
||||
|
||||
|
||||
|
||||
@@ -29,7 +29,7 @@ use Kiri\Jwt\Jwt;
|
||||
use Kiri\Kiri;
|
||||
use ReflectionException;
|
||||
use Server\ServerManager;
|
||||
use Server\SInterface\OnTaskInterface;
|
||||
use Server\Contract\OnTaskInterface;
|
||||
use Swoole\Table;
|
||||
|
||||
/**
|
||||
|
||||
@@ -21,203 +21,223 @@ use Swoole\Coroutine;
|
||||
class BaseObject implements Configure
|
||||
{
|
||||
|
||||
/**
|
||||
* BaseAbstract constructor.
|
||||
*
|
||||
* @param array $config
|
||||
* @throws Exception
|
||||
*/
|
||||
public function __construct(array $config = [])
|
||||
{
|
||||
if (!empty($config) && is_array($config)) {
|
||||
Kiri::configure($this, $config);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* BaseAbstract constructor.
|
||||
*
|
||||
* @param array $config
|
||||
* @throws Exception
|
||||
*/
|
||||
public function __construct(array $config = [])
|
||||
{
|
||||
if (!empty($config) && is_array($config)) {
|
||||
Kiri::configure($this, $config);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
}
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array|callable $callback
|
||||
* @param object $scope
|
||||
*/
|
||||
public function async_create(array|callable $callback, object $scope)
|
||||
{
|
||||
Coroutine::create($callback, $scope);
|
||||
}
|
||||
/**
|
||||
* @param array|callable $callback
|
||||
* @param object $scope
|
||||
*/
|
||||
public function async_create(array|callable $callback, object $scope)
|
||||
{
|
||||
Coroutine::create($callback, $scope);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
#[Pure] public static function className(): string
|
||||
{
|
||||
return static::class;
|
||||
}
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
#[Pure] public static function className(): string
|
||||
{
|
||||
return static::class;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param $value
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
public function __set($name, $value)
|
||||
{
|
||||
$method = 'set' . ucfirst($name);
|
||||
if (method_exists($this, $method)) {
|
||||
$this->{$method}($value);
|
||||
} else {
|
||||
throw new Exception('The set name ' . $name . ' not find in class ' . static::class);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @param $name
|
||||
* @param $value
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
public function __set($name, $value)
|
||||
{
|
||||
$method = 'set' . ucfirst($name);
|
||||
if (method_exists($this, $method)) {
|
||||
$this->{$method}($value);
|
||||
} else {
|
||||
throw new Exception('The set name ' . $name . ' not find in class ' . static::class);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
*
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function __get($name): mixed
|
||||
{
|
||||
$method = 'get' . ucfirst($name);
|
||||
if (method_exists($this, $method)) {
|
||||
return $this->$method();
|
||||
} else {
|
||||
throw new Exception('The get name ' . $name . ' not find in class ' . static::class);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @param $name
|
||||
*
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function __get($name): mixed
|
||||
{
|
||||
$method = 'get' . ucfirst($name);
|
||||
if (method_exists($this, $method)) {
|
||||
return $this->$method();
|
||||
} else {
|
||||
throw new Exception('The get name ' . $name . ' not find in class ' . static::class);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $message
|
||||
* @param string $model
|
||||
* @return bool
|
||||
* @throws Exception
|
||||
*/
|
||||
public function addError($message, string $model = 'app'): bool
|
||||
{
|
||||
if ($message instanceof \Throwable) {
|
||||
$this->error(jTraceEx($message));
|
||||
} else {
|
||||
if (!is_string($message)) {
|
||||
$message = json_encode($message, JSON_UNESCAPED_UNICODE);
|
||||
}
|
||||
$this->error($message);
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
/**
|
||||
* @param $message
|
||||
* @param string $model
|
||||
* @return bool
|
||||
* @throws Exception
|
||||
*/
|
||||
public function addError($message, string $model = 'app'): bool
|
||||
{
|
||||
if ($message instanceof \Throwable) {
|
||||
$this->error(jTraceEx($message));
|
||||
} else {
|
||||
if (!is_string($message)) {
|
||||
$message = json_encode($message, JSON_UNESCAPED_UNICODE);
|
||||
}
|
||||
$this->error($message);
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return Logger
|
||||
* @throws Exception
|
||||
*/
|
||||
private function logger(): Logger
|
||||
{
|
||||
return Kiri::getDi()->get(Logger::class);
|
||||
}
|
||||
/**
|
||||
* @return Logger
|
||||
* @throws Exception
|
||||
*/
|
||||
private function logger(): Logger
|
||||
{
|
||||
return Kiri::getDi()->get(Logger::class);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $message
|
||||
* @param string $method
|
||||
* @param string $file
|
||||
* @throws Exception
|
||||
*/
|
||||
public function debug(mixed $message, string $method = __METHOD__, string $file = __FILE__)
|
||||
{
|
||||
if (!is_string($message)) {
|
||||
$message = print_r($message, true);
|
||||
}
|
||||
$message = "\033[35m[" . date('Y-m-d H:i:s') . '][DEBUG]: ' . $message . "\033[0m";
|
||||
$message .= PHP_EOL;
|
||||
|
||||
$this->logger()->debug(Logger::DEBUG, [$message, $method, $file]);
|
||||
}
|
||||
/**
|
||||
* @param mixed $message
|
||||
* @param string $method
|
||||
* @param string $file
|
||||
* @throws Exception
|
||||
*/
|
||||
public function debug(mixed $message, string $method = '', string $file = '')
|
||||
{
|
||||
if (!is_string($message)) {
|
||||
$message = print_r($message, true);
|
||||
}
|
||||
$message = "\033[35m" . $message . "\033[0m";
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $message
|
||||
* @param string $method
|
||||
* @param string $file
|
||||
* @throws Exception
|
||||
*/
|
||||
public function info(mixed $message, string $method = __METHOD__, string $file = __FILE__)
|
||||
{
|
||||
if (!is_string($message)) {
|
||||
$message = print_r($message, true);
|
||||
}
|
||||
$message = "\033[34m[" . date('Y-m-d H:i:s') . '][INFO]: ' . $message . "\033[0m";
|
||||
$message .= PHP_EOL;
|
||||
$context = [];
|
||||
if (!empty($method)) $context['method'] = $method;
|
||||
if (!empty($file)) $context['file'] = $file;
|
||||
|
||||
$this->logger()->info(Logger::NOTICE, [$message, $method, $file]);
|
||||
}
|
||||
$this->logger()->debug($message, $context);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $message
|
||||
* @param string $method
|
||||
* @param string $file
|
||||
* @throws Exception
|
||||
*/
|
||||
public function success(mixed $message, string $method = __METHOD__, string $file = __FILE__)
|
||||
{
|
||||
if (!is_string($message)) {
|
||||
$message = print_r($message, true);
|
||||
}
|
||||
|
||||
$message = "\033[36m[" . date('Y-m-d H:i:s') . '][SUCCESS]: ' . $message . "\033[0m";
|
||||
$message .= PHP_EOL;
|
||||
|
||||
$this->logger()->notice(Logger::NOTICE, [$message, $method, $file]);
|
||||
}
|
||||
/**
|
||||
* @param mixed $message
|
||||
* @param string $method
|
||||
* @param string $file
|
||||
* @throws Exception
|
||||
*/
|
||||
public function info(mixed $message, string $method = '', string $file = '')
|
||||
{
|
||||
if (!is_string($message)) {
|
||||
$message = print_r($message, true);
|
||||
}
|
||||
$message = "\033[34m" . $message . "\033[0m";
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $message
|
||||
* @param string $method
|
||||
* @param string $file
|
||||
* @throws Exception
|
||||
*/
|
||||
public function warning(mixed $message, string $method = __METHOD__, string $file = __FILE__)
|
||||
{
|
||||
if (!is_string($message)) {
|
||||
$message = print_r($message, true);
|
||||
}
|
||||
$context = [];
|
||||
if (!empty($method)) $context['method'] = $method;
|
||||
if (!empty($file)) $context['file'] = $file;
|
||||
|
||||
$message = "\033[33m[" . date('Y-m-d H:i:s') . '][WARNING]: ' . $message . "\033[0m";
|
||||
$message .= PHP_EOL;
|
||||
|
||||
$this->logger()->critical(Logger::NOTICE, [$message, $method, $file]);
|
||||
}
|
||||
$this->logger()->info($message, $context);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $message
|
||||
* @param null $method
|
||||
* @param null $file
|
||||
* @throws Exception
|
||||
*/
|
||||
public function error(mixed $message, $method = null, $file = null)
|
||||
{
|
||||
if ($message instanceof \Throwable) {
|
||||
$message = $message->getMessage() . " on line " . $message->getLine() . " at file " . $message->getFile();
|
||||
}
|
||||
$content = (empty($method) ? '' : $method . ': ') . $message;
|
||||
/**
|
||||
* @param mixed $message
|
||||
* @param string $method
|
||||
* @param string $file
|
||||
* @throws Exception
|
||||
*/
|
||||
public function success(mixed $message, string $method = '', string $file = '')
|
||||
{
|
||||
if (!is_string($message)) {
|
||||
$message = print_r($message, true);
|
||||
}
|
||||
|
||||
$message = "\033[41;37m[" . date('Y-m-d H:i:s') . '][ERROR]: ' . $content . "\033[0m";
|
||||
$message = "\033[36m" . $message . "\033[0m";
|
||||
|
||||
if (!empty($file)) {
|
||||
$message .= PHP_EOL . "\033[41;37m[" . date('Y-m-d H:i:s') . '][ERROR]: ' . $file . "\033[0m";
|
||||
}
|
||||
|
||||
$this->logger()->error(Logger::ERROR, [$message, $method, $file]);
|
||||
}
|
||||
$context = [];
|
||||
if (!empty($method)) $context['method'] = $method;
|
||||
if (!empty($file)) $context['file'] = $file;
|
||||
|
||||
$this->logger()->notice($message, $context);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $message
|
||||
* @param string $method
|
||||
* @param string $file
|
||||
* @throws Exception
|
||||
*/
|
||||
public function warning(mixed $message, string $method = '', string $file = '')
|
||||
{
|
||||
if (!is_string($message)) {
|
||||
$message = print_r($message, true);
|
||||
}
|
||||
|
||||
$message = "\033[33m" . $message . "\033[0m";
|
||||
|
||||
|
||||
$context = [];
|
||||
if (!empty($method)) $context['method'] = $method;
|
||||
if (!empty($file)) $context['file'] = $file;
|
||||
|
||||
$this->logger()->critical($message, $context);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $message
|
||||
* @param null $method
|
||||
* @param null $file
|
||||
* @throws Exception
|
||||
*/
|
||||
public function error(mixed $message, $method = null, $file = null)
|
||||
{
|
||||
if ($message instanceof \Throwable) {
|
||||
$message = $message->getMessage() . " on line " . $message->getLine() . " at file " . $message->getFile();
|
||||
}
|
||||
$content = (empty($method) ? '' : $method . ': ') . $message;
|
||||
|
||||
$message = "\033[41;37m" . $content . "\033[0m";
|
||||
|
||||
if (!empty($file)) {
|
||||
$message .= PHP_EOL . "\03341;37m[" . $file . "\033[0m";
|
||||
}
|
||||
|
||||
$context = [];
|
||||
if (!empty($method)) $context['method'] = $method;
|
||||
if (!empty($file)) $context['file'] = $file;
|
||||
|
||||
$this->logger()->error($message, $context);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -223,6 +223,7 @@ class Application extends BaseApplication
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @throws NotFindClassException
|
||||
* @throws ReflectionException
|
||||
@@ -240,6 +241,8 @@ class Application extends BaseApplication
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @param $className
|
||||
* @param null $abstracts
|
||||
|
||||
@@ -5,8 +5,9 @@ namespace Kiri\Error;
|
||||
|
||||
|
||||
use Exception;
|
||||
use Http\Aspect\OnAspectInterface;
|
||||
use Http\Aspect\OnJoinPointInterface;
|
||||
use Http\Constrict\RequestInterface;
|
||||
use Kiri\IAspect;
|
||||
use Kiri\Kiri;
|
||||
|
||||
|
||||
@@ -14,20 +15,24 @@ use Kiri\Kiri;
|
||||
* Class LoggerAspect
|
||||
* @package Kiri\Error
|
||||
*/
|
||||
class LoggerAspect implements IAspect
|
||||
class LoggerAspect implements OnAspectInterface
|
||||
{
|
||||
|
||||
private float $time;
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $handler
|
||||
* @param array $params
|
||||
* @param OnJoinPointInterface $joinPoint
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function invoke(mixed $handler, array $params = []): mixed
|
||||
public function process(OnJoinPointInterface $joinPoint): mixed
|
||||
{
|
||||
return call_user_func($handler, ...$params);
|
||||
$time = microtime(true);
|
||||
|
||||
$response = $joinPoint->process();
|
||||
|
||||
$this->print_runtime($time);
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
|
||||
@@ -44,20 +49,4 @@ class LoggerAspect implements IAspect
|
||||
echo PHP_EOL;
|
||||
}
|
||||
|
||||
|
||||
public function before(): void
|
||||
{
|
||||
// TODO: Implement before() method.
|
||||
$this->time = microtime(true);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function after(mixed $response): void
|
||||
{
|
||||
// TODO: Implement after() method.
|
||||
$this->print_runtime($this->time);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,22 +20,16 @@ use Server\Abstracts\BaseProcess;
|
||||
class LoggerProcess extends BaseProcess
|
||||
{
|
||||
|
||||
/**
|
||||
* @param Process $process
|
||||
* @return string
|
||||
*/
|
||||
#[Pure] public function getProcessName(Process $process): string
|
||||
{
|
||||
// TODO: Implement getProcessName() method.
|
||||
return get_called_class();
|
||||
}
|
||||
|
||||
public string $name = 'logger process';
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @param Process $process
|
||||
* @throws ComponentException
|
||||
*/
|
||||
public function onHandler(Process $process): void
|
||||
public function process(Process $process): void
|
||||
{
|
||||
// TODO: Implement onHandler() method.
|
||||
$this->message($process);
|
||||
@@ -49,10 +43,6 @@ class LoggerProcess extends BaseProcess
|
||||
*/
|
||||
public function message(Process $process)
|
||||
{
|
||||
if ($this->checkProcessIsStop()) {
|
||||
$this->exit();
|
||||
return;
|
||||
}
|
||||
$message = Json::decode($process->read());
|
||||
if (!empty($message)) {
|
||||
Kiri::writeFile($this->getDirName($message), $message[0], FILE_APPEND);
|
||||
|
||||
@@ -2,9 +2,10 @@
|
||||
|
||||
namespace Kiri\FileListen;
|
||||
|
||||
use Annotation\Inject;
|
||||
use Exception;
|
||||
use Kiri\Abstracts\Config;
|
||||
use Kiri\Abstracts\Logger;
|
||||
use Kiri\Error\Logger;
|
||||
use Kiri\Exception\ConfigException;
|
||||
use Kiri\Kiri;
|
||||
use Swoole\Coroutine;
|
||||
@@ -24,12 +25,24 @@ class HotReload extends Command
|
||||
public bool $isReloading = false;
|
||||
public bool $isReloadingOut = false;
|
||||
public ?array $dirs = [];
|
||||
|
||||
public int $events;
|
||||
|
||||
public int $int = -1;
|
||||
|
||||
|
||||
private ?Process $process = null;
|
||||
|
||||
|
||||
public Inotify|Scaner $driver;
|
||||
|
||||
|
||||
#[Inject(Logger::class)]
|
||||
public Logger $logger;
|
||||
|
||||
|
||||
protected mixed $source = null;
|
||||
|
||||
protected mixed $pipes = [];
|
||||
|
||||
protected ?Coroutine\Channel $channel = null;
|
||||
@@ -56,40 +69,51 @@ class HotReload extends Command
|
||||
{
|
||||
// TODO: Implement onHandler() method.
|
||||
set_error_handler([$this, 'onErrorHandler']);
|
||||
|
||||
$this->dirs = Config::get('inotify', [APP_PATH . 'app']);
|
||||
swoole_async_set(['enable_coroutine' => false]);
|
||||
if (!extension_loaded('inotify')) {
|
||||
$driver = Kiri::getDi()->get(Scaner::class, [$this->dirs, $this]);
|
||||
$this->driver = Kiri::getDi()->get(Scaner::class, [$this->dirs, $this]);
|
||||
} else {
|
||||
$driver = Kiri::getDi()->get(Inotify::class, [$this->dirs, $this]);
|
||||
$this->driver = Kiri::getDi()->get(Inotify::class, [$this->dirs, $this]);
|
||||
}
|
||||
|
||||
if (Kiri::getPlatform()->isLinux()) {
|
||||
swoole_set_process_name('[' . Config::get('id', 'sw service.') . '].sw:wather');
|
||||
}
|
||||
$this->trigger_reload();
|
||||
Coroutine::create(function () use ($driver) {
|
||||
$driver->start();
|
||||
});
|
||||
|
||||
var_dump(getmypid());
|
||||
Process::signal(SIGTERM, [$this, 'onSignal']);
|
||||
Process::signal(SIGKILL, [$this, 'onSignal']);
|
||||
|
||||
$this->driver->start();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $data
|
||||
* @throws Exception
|
||||
*/
|
||||
private function stop(): void
|
||||
public function onSignal($data)
|
||||
{
|
||||
if (is_resource($this->source)) {
|
||||
proc_terminate($this->source);
|
||||
while (proc_get_status($this->source)['running']) {
|
||||
Coroutine::sleep(1);
|
||||
}
|
||||
proc_close($this->source);
|
||||
$this->source = null;
|
||||
if (!$data) {
|
||||
return;
|
||||
}
|
||||
$this->driver->clear();
|
||||
$pid = file_get_contents(storage('.swoole.pid'));
|
||||
if (!empty($pid) && Process::kill($pid, 0)) {
|
||||
Process::kill($pid, SIGTERM);
|
||||
}
|
||||
if ($this->process && Process::kill($this->process->pid, 0)) {
|
||||
Process::kill($this->process->pid) && Process::wait(true);
|
||||
}
|
||||
while ($ret = Process::wait(true)) {
|
||||
echo "PID={$ret['pid']}\n";
|
||||
sleep(1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $code
|
||||
* @param $message
|
||||
@@ -113,12 +137,17 @@ class HotReload extends Command
|
||||
*/
|
||||
public function trigger_reload()
|
||||
{
|
||||
Kiri::getDi()->get(Logger::class)->warning('change reload');
|
||||
|
||||
$this->stop();
|
||||
Coroutine::create(function () {
|
||||
$this->source = proc_open("php " . APP_PATH . "kiri.php", [], $pipes);
|
||||
$this->logger->warning('change reload');
|
||||
$pid = $this->process?->pid;
|
||||
$process = new Process(function (Process $process) {
|
||||
$process->exec(PHP_BINARY, [APP_PATH . "kiri.php", "sw:server", "restart"]);
|
||||
});
|
||||
$process->start();
|
||||
if ($pid && Process::kill($pid, 0)) {
|
||||
Process::kill($pid) && Process::wait(true);
|
||||
}
|
||||
$this->process = null;
|
||||
$this->process = $process;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -53,6 +53,7 @@ class Inotify
|
||||
|
||||
/**
|
||||
* 开始监听
|
||||
* @throws Exception
|
||||
*/
|
||||
public function check()
|
||||
{
|
||||
@@ -60,11 +61,9 @@ class Inotify
|
||||
return;
|
||||
}
|
||||
if ($this->process->isReloading) {
|
||||
if (!$this->process->isReloadingOut) {
|
||||
$this->process->isReloadingOut = true;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
$LISTEN_TYPE = [IN_CREATE, IN_DELETE, IN_MODIFY, IN_MOVED_TO, IN_MOVED_FROM];
|
||||
foreach ($events as $ev) {
|
||||
if (!in_array($ev['mask'], $LISTEN_TYPE)) {
|
||||
@@ -72,12 +71,7 @@ class Inotify
|
||||
}
|
||||
//非重启类型
|
||||
if (str_ends_with($ev['name'], '.php')) {
|
||||
if ($this->process->int !== -1) {
|
||||
return;
|
||||
}
|
||||
$this->process->int = @swoole_timer_after(2000, [$this, 'reload']);
|
||||
|
||||
$this->process->isReloading = true;
|
||||
$this->reload();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -87,16 +81,19 @@ class Inotify
|
||||
*/
|
||||
public function reload()
|
||||
{
|
||||
$this->process->isReloading = true;
|
||||
$this->process->trigger_reload();
|
||||
if ($this->process->isReloading) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->process->isReloading = true;
|
||||
|
||||
$this->process->trigger_reload();
|
||||
$this->clearWatch();
|
||||
foreach ($this->dirs as $root) {
|
||||
$this->watch($root);
|
||||
}
|
||||
$this->process->int = -1;
|
||||
$this->process->isReloading = FALSE;
|
||||
$this->process->isReloadingOut = FALSE;
|
||||
}
|
||||
|
||||
|
||||
@@ -151,7 +148,6 @@ class Inotify
|
||||
} else if (!str_ends_with($f, '.php')) {
|
||||
continue;
|
||||
}
|
||||
|
||||
//检测文件类型
|
||||
if (strstr($f, '.') == '.php') {
|
||||
$wd = @inotify_add_watch($this->inotify, $path, $this->events);
|
||||
|
||||
@@ -103,6 +103,9 @@ class Scaner
|
||||
*/
|
||||
public function timerReload()
|
||||
{
|
||||
if ($this->process->isReloading) {
|
||||
return;
|
||||
}
|
||||
$this->process->isReloading = true;
|
||||
$this->process->trigger_reload();
|
||||
|
||||
|
||||
@@ -31,7 +31,7 @@ defined('TASK_PATH') or define('TASK_PATH', APP_PATH . 'app/Async/');
|
||||
defined('LISTENER_PATH') or define('LISTENER_PATH', APP_PATH . 'app/Listener/');
|
||||
defined('KAFKA_PATH') or define('KAFKA_PATH', APP_PATH . 'app/Kafka/');
|
||||
defined('RPC_CLIENT_PATH') or define('RPC_CLIENT_PATH', APP_PATH . 'app/Client/Rpc/');
|
||||
defined('MODEL_PATH') or define('MODEL_PATH', APP_PATH . 'app/Models/');
|
||||
defined('MODEL_PATH') or define('MODEL_PATH', APP_PATH . 'app/Model/');
|
||||
|
||||
|
||||
/**
|
||||
|
||||
@@ -220,6 +220,13 @@ class Pool extends Component
|
||||
return;
|
||||
}
|
||||
while (static::$_connections[$name]->length() > 0) {
|
||||
if (static::$_connections[$name] instanceof Channel)
|
||||
{
|
||||
if (!Context::inCoroutine())
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
$client = static::$_connections[$name]->pop();
|
||||
if ($client instanceof StopHeartbeatCheck) {
|
||||
$client->stopHeartbeatCheck();
|
||||
|
||||
+4
-4
@@ -30,11 +30,11 @@ class Gii
|
||||
|
||||
private InputInterface $input;
|
||||
|
||||
public string $modelPath = APP_PATH . 'app/Models/';
|
||||
public string $modelNamespace = 'App\\Models\\';
|
||||
public string $modelPath = APP_PATH . 'app/Model/';
|
||||
public string $modelNamespace = 'App\\Model\\';
|
||||
|
||||
public string $controllerPath = APP_PATH . 'app/Http/Controllers/';
|
||||
public string $controllerNamespace = 'App\\Controllers\\';
|
||||
public string $controllerPath = APP_PATH . 'app/Http/Controller/';
|
||||
public string $controllerNamespace = 'App\\Controller\\';
|
||||
|
||||
|
||||
public static array $createSqls = [];
|
||||
|
||||
@@ -24,11 +24,11 @@ abstract class GiiBase
|
||||
|
||||
protected InputInterface $input;
|
||||
|
||||
public string $modelPath = APP_PATH . 'app/Models/';
|
||||
public string $modelNamespace = 'App\Models\\';
|
||||
public string $modelPath = APP_PATH . 'app/Model/';
|
||||
public string $modelNamespace = 'App\Model\\';
|
||||
|
||||
public string $controllerPath = APP_PATH . 'app/Http/Controllers/';
|
||||
public string $controllerNamespace = 'App\\Controllers\\';
|
||||
public string $controllerPath = APP_PATH . 'app/Http/Controller/';
|
||||
public string $controllerNamespace = 'App\\Controller\\';
|
||||
|
||||
public ?string $module = null;
|
||||
|
||||
|
||||
@@ -92,7 +92,7 @@ use Kiri\Core\Json;
|
||||
use Database\Connection;
|
||||
use Database\Annotation\Get;
|
||||
use Database\Annotation\Set;
|
||||
use Annotation\Model\Relation;
|
||||
use Database\Relation;
|
||||
use Database\Model;
|
||||
' . PHP_EOL;
|
||||
}
|
||||
|
||||
@@ -31,7 +31,7 @@ class GiiTask extends GiiBase
|
||||
|
||||
namespace App\Async;
|
||||
|
||||
use Server\SInterface\OnTaskInterface;
|
||||
use Server\Contract\OnTaskInterface;
|
||||
|
||||
';
|
||||
|
||||
|
||||
@@ -1,10 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Process;
|
||||
|
||||
interface OnProcessInterface
|
||||
{
|
||||
|
||||
public function handle();
|
||||
|
||||
}
|
||||
@@ -1,88 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Process;
|
||||
|
||||
abstract class Process implements OnProcessInterface
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @var \Swoole\Process
|
||||
*/
|
||||
protected \Swoole\Process $process;
|
||||
|
||||
|
||||
/**
|
||||
* @var mixed
|
||||
*/
|
||||
protected mixed $redirect_stdin_and_stdout = null;
|
||||
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected int $pipe_type = SOCK_DGRAM;
|
||||
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected bool $enable_coroutine = true;
|
||||
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected string $name = '';
|
||||
|
||||
/**
|
||||
* @return \Swoole\Process
|
||||
*/
|
||||
public function getProcess(): \Swoole\Process
|
||||
{
|
||||
return $this->process;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getRedirectStdinAndStdout(): mixed
|
||||
{
|
||||
return $this->redirect_stdin_and_stdout;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getPipeType(): int
|
||||
{
|
||||
return $this->pipe_type;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isEnableCoroutine(): bool
|
||||
{
|
||||
return $this->enable_coroutine;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getName(): string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param \Swoole\Process $process
|
||||
*/
|
||||
public function start(\Swoole\Process $process)
|
||||
{
|
||||
$this->process = $process;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,52 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Process;
|
||||
|
||||
require_once 'OnProcessInterface.php';
|
||||
require_once 'Process.php';
|
||||
|
||||
use function Co\run;
|
||||
|
||||
class TestProcess extends Process
|
||||
{
|
||||
|
||||
|
||||
protected string $name = 'test process';
|
||||
|
||||
|
||||
/**
|
||||
*/
|
||||
public function onStart()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
public function handle()
|
||||
{
|
||||
// TODO: Implement doWhile() method.
|
||||
}
|
||||
|
||||
|
||||
public function onShutdown()
|
||||
{
|
||||
// TODO: Implement onShutdown() method.
|
||||
}
|
||||
}
|
||||
|
||||
$array = [];
|
||||
|
||||
for ($i = 0; $i < 10; $i++) {
|
||||
$class = new TestProcess();
|
||||
$process = new \Swoole\Process([$class, 'start'], $class->getRedirectStdinAndStdout(),
|
||||
$class->getPipeType(), $class->isEnableCoroutine());
|
||||
$process->start();
|
||||
|
||||
array_push($array, $process);
|
||||
}
|
||||
run(function () use ($array) {
|
||||
|
||||
foreach ($array as $value) {
|
||||
var_dump($value->getCallback());
|
||||
}
|
||||
|
||||
});
|
||||
Reference in New Issue
Block a user