This commit is contained in:
xl
2022-05-03 06:56:28 +08:00
parent ab2ec0f790
commit def499c2f6
4 changed files with 3 additions and 583 deletions
+3 -16
View File
@@ -17,7 +17,6 @@ use Kiri;
use Kiri\Abstracts\{BaseApplication, Config, Kernel};
use Kiri\Crontab\CrontabProviders;
use Kiri\Events\{OnAfterCommandExecute, OnBeforeCommandExecute};
use Kiri\FileListen\HotReload;
use Kiri\Server\ServerProviders;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
@@ -110,18 +109,6 @@ class Application extends BaseApplication
}
/**
* @throws
*/
public function withFileChangeListen()
{
$container = Kiri::getDi();
$console = $container->get(ConsoleApplication::class);
$console->add($container->get(HotReload::class));
}
/**
* @param Closure|array $closure
* @return $this
@@ -239,9 +226,9 @@ class Application extends BaseApplication
$this->container->setBindings(OutputInterface::class, $output);
if (!($class instanceof HotReload)) {
scan_directory(MODEL_PATH, 'app\Model');
}
if (!($class instanceof Kiri\Server\ServerCommand)) {
scan_directory(MODEL_PATH,'app\Model');
}
$class->run($input, $output);
fire(new OnAfterCommandExecute());
-230
View File
@@ -1,230 +0,0 @@
<?php
namespace Kiri\FileListen;
use Exception;
use Kiri;
use Kiri\Abstracts\Config;
use Kiri\Core\Json;
use Kiri\Error\StdoutLoggerInterface;
use Kiri\Exception\ConfigException;
use Swoole\Coroutine;
use Swoole\Process;
use Swoole\Timer;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
/**
*
*/
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;
public StdoutLoggerInterface $logger;
protected mixed $source = NULL;
protected mixed $pipes = [];
protected ?Coroutine\Channel $channel = NULL;
/**
*/
protected function configure()
{
$this->setName('sw:wather')->setDescription('server start');
$this->logger = Kiri::getDi()->get(StdoutLoggerInterface::class);
}
/**
* @throws ConfigException
* @throws Exception
*/
protected function initCore()
{
set_error_handler([$this, 'errorHandler']);
$this->dirs = Config::get('inotify', [APP_PATH . 'app']);
$container = Kiri::getDi();
if (!extension_loaded('inotify')) {
$this->driver = $container->make(Scaner::class, [$this->dirs, $this, $this->logger]);
} else {
$this->driver = $container->make(Inotify::class, [$this->dirs, $this, $this->logger]);
}
$this->clearOtherService();
$this->setProcessName();
}
/**
* @throws ConfigException
*/
public function setProcessName()
{
swoole_async_set(['enable_coroutine' => FALSE]);
if (Kiri::getPlatform()->isLinux()) {
swoole_set_process_name('[' . Config::get('id', 'sw service.') . '].sw:wather');
}
}
/**
* @throws Exception
*/
public function clearOtherService()
{
if (file_exists(storage('.manager.pid'))) {
$pid = (int)file_get_contents(storage('.manager.pid'));
if ($pid > 0 && Process::kill($pid, 0)) {
Process::kill($pid, 15) && Process::wait(TRUE);
}
}
file_put_contents(storage('.manager.pid'), getmypid());
}
/**
* @throws Exception
*/
public function errorHandler()
{
$error = func_get_args();
$path = ['file' => $error[2], 'line' => $error[3]];
if ($error[0] === 0) {
$error[0] = 500;
}
$data = Json::to($error[0], $error[1], $path);
$this->logger->error('error', [$data]);
}
/**
* @param InputInterface $input
* @param OutputInterface $output
* @return int
* @throws ConfigException
* @throws Exception
*/
public function execute(InputInterface $input, OutputInterface $output): int
{
$this->initCore();
$this->trigger_reload();
Timer::tick(1000, fn() => $this->healthCheck());
pcntl_signal(SIGTERM, function () {
$this->onSignal(func_get_args());
});
$this->driver->start();
return 0;
}
/**
* @throws Exception
*/
public function healthCheck()
{
$pid = (int)file_get_contents(storage('.swoole.pid'));
if ($this->int == 1) {
return;
}
if (empty($pid)) {
$this->logger->warning('service is shutdown you need reload.');
$this->trigger_reload();
} else if (!Process::kill($pid, 0)) {
$this->logger->warning('service is shutdown you need reload.');
$this->trigger_reload();
}
}
/**
* @param $data
* @throws Exception
*/
public function onSignal($data)
{
if (!$data) {
return;
}
Timer::clearAll();
$this->driver->clear();
$this->stopServer();
while ($ret = Process::wait(TRUE)) {
echo "PID={$ret['pid']}\n";
sleep(1);
}
}
/**
* @throws Exception
*/
protected function stopServer()
{
$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);
}
}
/**
* 重启
*
* @throws Exception
*/
public function trigger_reload(string $path = '')
{
if ($this->int == 1) {
return;
}
$this->int = 1;
$this->logger->warning('restart');
$this->stopServer();
$this->process = new Process(function (Process $process) {
$config = Config::get('scanner', []);
scan_directory(MODEL_PATH, 'app\Model');
if (is_array($config)) foreach ($config as $key => $value) {
scan_directory($value, $key);
}
Kiri::app()->getServer()->start();
});
$this->process->start();
sleep(3);
$this->int = -1;
}
}
-178
View File
@@ -1,178 +0,0 @@
<?php
namespace Kiri\FileListen;
use Exception;
use Kiri\Error\StdoutLoggerInterface;
use Swoole\Event;
use Swoole\Timer;
class Inotify
{
private mixed $inotify;
private mixed $events;
private array $watchFiles = [];
public bool $isReloading = FALSE;
protected int $cid;
const IG_DIR = [APP_PATH . 'commands', APP_PATH . '.git', APP_PATH . '.gitee'];
/**
* @param array $dirs
* @param HotReload $process
* @param StdoutLoggerInterface $logger
*
*/
public function __construct(protected array $dirs, public HotReload $process, public StdoutLoggerInterface $logger)
{
set_error_handler([$this, 'error']);
set_exception_handler([$this, 'error']);
}
/**
* @return void
*/
public function error(): void
{
}
/**
* @throws Exception
*/
public function start()
{
$this->inotify = inotify_init();
$this->events = IN_MODIFY | IN_DELETE | IN_CREATE | IN_MOVE;
foreach ($this->dirs as $dir) {
if (!is_dir($dir)) continue;
$this->watch($dir);
}
$this->process->int = -1;
Event::add($this->inotify, [$this, 'check']);
Event::wait();
}
public function clear()
{
Event::del($this->inotify);
Event::exit();
}
/**
* 开始监听
* @throws Exception
*/
public function check()
{
if (!($events = inotify_read($this->inotify))) {
return;
}
if ($this->isReloading) {
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)) {
continue;
}
$search = array_search($ev['wd'], $this->watchFiles);
//非重启类型
if (str_ends_with($ev['name'], '.php')) {
if ($this->isReloading) {
break;
}
Timer::after(3000, fn() => $this->reload($search));
$this->isReloading = TRUE;
}
}
}
/**
* @throws Exception
*/
public function reload($path)
{
$this->logger->warning('file change');
$this->process->trigger_reload($path);
$this->process->int = -1;
$this->clearWatch();
foreach ($this->dirs as $root) {
$this->watch($root);
}
$this->isReloading = FALSE;
}
/**
* @throws Exception
*/
public function clearWatch()
{
foreach ($this->watchFiles as $wd) {
@inotify_rm_watch($this->inotify, $wd);
}
$this->watchFiles = [];
}
/**
* @param $dir
* @return bool
* @throws Exception
*/
public function watch($dir): bool
{
//目录不存在
if (!is_dir($dir)) {
return $this->logger->addError("[$dir] is not a directory.");
}
//避免重复监听
if (isset($this->watchFiles[$dir])) {
return FALSE;
}
if (in_array($dir, self::IG_DIR)) {
return FALSE;
}
$wd = @inotify_add_watch($this->inotify, $dir, $this->events);
$this->watchFiles[$dir] = $wd;
$files = scandir($dir);
foreach ($files as $f) {
if ($f == '.' || $f == '..') {
continue;
}
$path = $dir . '/' . $f;
//递归目录
if (is_dir($path)) {
$this->watch($path);
} else if (!str_ends_with($f, '.php')) {
continue;
}
//检测文件类型
if (strstr($f, '.') == '.php') {
$wd = @inotify_add_watch($this->inotify, $path, $this->events);
$this->watchFiles[$path] = $wd;
}
}
return TRUE;
}
}
-159
View File
@@ -1,159 +0,0 @@
<?php
namespace Kiri\FileListen;
use Exception;
use Kiri\Error\StdoutLoggerInterface;
class Scaner
{
private array $md5Map = [];
public bool $isReloading = FALSE;
/**
* @param array $dirs
* @param HotReload $process
*/
public function __construct(protected array $dirs, public HotReload $process, public StdoutLoggerInterface $logger)
{
}
/**
* @throws Exception
*/
public function start(): void
{
$this->loadDirs();
$this->tick();
}
/**
* @param bool $isReload
* @throws Exception
*/
private function loadDirs(bool $isReload = FALSE)
{
foreach ($this->dirs as $value) {
if (is_bool($path = realpath($value))) {
continue;
}
if (!is_dir($path)) continue;
$this->loadByDir($path, $isReload);
}
}
/**
* @param $path
* @param bool $isReload
* @return void
* @throws Exception
*/
private function loadByDir($path, bool $isReload = FALSE): void
{
if (!is_string($path)) {
return;
}
$path = rtrim($path, '/');
foreach (glob(realpath($path) . '/*') as $value) {
if (is_dir($value)) {
$this->loadByDir($value, $isReload);
}
if (is_file($value)) {
if ($this->checkFile($value, $isReload)) {
if ($this->isReloading) {
break;
}
$this->isReloading = TRUE;
sleep(2);
$this->timerReload($value);
break;
}
}
}
}
/**
* @param $value
* @param $isReload
* @return bool
*/
private function checkFile($value, $isReload): bool
{
$md5 = md5($value);
$mTime = filectime($value);
if (!isset($this->md5Map[$md5])) {
if ($isReload) {
return TRUE;
}
$this->md5Map[$md5] = $mTime;
} else {
if ($this->md5Map[$md5] != $mTime) {
if ($isReload) {
return TRUE;
}
$this->md5Map[$md5] = $mTime;
}
}
return FALSE;
}
/**
* @throws Exception
*/
public function timerReload($path)
{
$this->isReloading = TRUE;
$this->logger->warning('file change');
$this->process->trigger_reload($path);
$this->loadDirs();
$this->process->int = -1;
$this->isReloading = FALSE;
$this->process->isReloadingOut = FALSE;
$this->tick();
}
private bool $isStop = FALSE;
public function clear()
{
$this->isStop = TRUE;
}
/**
* @throws Exception
*/
public function tick()
{
if ($this->isReloading || $this->isStop) {
return;
}
$this->loadDirs(TRUE);
sleep(2);
$this->tick();
}
}