From a85cdf49d8df55531ef6e419ed4874d6a7521dec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E6=99=AF=E7=AB=8B?= Date: Wed, 18 Dec 2024 14:55:22 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E7=94=B1=E4=BA=8E=E9=83=A8=E5=88=86?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E5=9C=BA=E6=99=AF=E5=BC=82=E5=B8=B8=EF=BC=8C?= =?UTF-8?q?=E5=8E=BB=E9=99=A4=E6=96=87=E4=BB=B6=E5=8A=A8=E6=80=81=E7=9B=91?= =?UTF-8?q?=E5=90=AC=E4=B8=8E=E5=86=85=E5=AD=98=E7=9B=91=E6=8E=A7=E9=87=8D?= =?UTF-8?q?=E5=90=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugin/think-plugs-worker/readme.md | 16 -- plugin/think-plugs-worker/src/Monitor.php | 253 ------------------ .../think-plugs-worker/src/command/Worker.php | 5 - .../src/support/HttpServer.php | 35 --- plugin/think-plugs-worker/stc/worker.php | 16 -- 5 files changed, 325 deletions(-) delete mode 100644 plugin/think-plugs-worker/src/Monitor.php diff --git a/plugin/think-plugs-worker/readme.md b/plugin/think-plugs-worker/readme.md index 55507a672..fffbf5d82 100644 --- a/plugin/think-plugs-worker/readme.md +++ b/plugin/think-plugs-worker/readme.md @@ -58,22 +58,6 @@ return [ // 进程数量 'count' => 4, ], - // 监控文件变更重载 - 'files' => [ - // 监控检测间隔(单位秒,零不监控) - 'time' => 3, - // 文件监控目录(默认监控 app+config 目录) - 'path' => [], - // 文件监控后缀(默认监控 所有 文件) - 'exts' => ['*'] - ], - // 监控内存超限重载 - 'memory' => [ - // 监控检测间隔(单位秒,零不监控) - 'time' => 60, - // 限制内存大小(可选单位有 G M K ) - 'limit' => '1G' - ], // 自定义服务配置(可选) 'customs' => [ // 自定义 text 服务 diff --git a/plugin/think-plugs-worker/src/Monitor.php b/plugin/think-plugs-worker/src/Monitor.php deleted file mode 100644 index f58a10b4b..000000000 --- a/plugin/think-plugs-worker/src/Monitor.php +++ /dev/null @@ -1,253 +0,0 @@ - -1) Timer::del(self::$changeTimerId); - self::$changeTimerId = Timer::add($interval, static function () { - if (self::isPaused()) return false; - foreach (self::$paths as $path => $exts) { - if (self::_checkFilesChange($path, $exts)) { - return true; - } - } - return false; - }); - return true; - } - } - - /** - * Check Files Change - * @param string $path - * @param array $exts - * @return boolean - */ - private static function _checkFilesChange(string $path, array $exts): bool - { - static $lastMtime, $tooManyFilesCheck; - if (!$lastMtime) $lastMtime = time(); - - clearstatcache(); - if (!is_dir($path)) { - if (!is_file($path)) return false; - $iterator = [new SplFileInfo($path)]; - } else { - // recursive traversal directory - $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS | FilesystemIterator::FOLLOW_SYMLINKS)); - } - $count = 0; - foreach ($iterator as $file) { - $count++; - /** var SplFileInfo $file */ - if (is_dir($file->getRealPath())) continue; - // check mtime - if ($lastMtime < $file->getMTime() && (in_array('*', $exts) || in_array($file->getExtension(), $exts, true))) { - if ($file->getExtension() === 'php') { - exec(ProcessService::php("-l {$file}"), $out, $var); - if ($var) continue; - } - $lastMtime = $file->getMTime(); - echo "{$file} update and reload\n"; - // send SIGUSR1 signal to master process for reload - if (DIRECTORY_SEPARATOR === '/') { - posix_kill(posix_getppid(), SIGUSR1); - break; - } else { - return true; - } - } - } - if (!$tooManyFilesCheck && $count > 10000) { - echo "Monitor: There are too many files ($count files) in $path which makes file monitoring very slow\n"; - $tooManyFilesCheck = 1; - } - return false; - } - - /** - * Enable Member Monitor,only windows - * @param ?string $limit - * @param integer $interval - * @return boolean - */ - public static function enableMemoryMonitor(?string $limit = null, int $interval = 60): bool - { - if ($interval <= 0) return false; - if (!Worker::getAllWorkers()) return false; - if (!ProcessService::isUnix()) return false; - if ($memoryLimit = self::_getMemoryLimit($limit ?: self::defaultMaxMemory)) { - self::$memoryTimerId > -1 && Timer::del(self::$memoryTimerId); - self::$memoryTimerId = Timer::add($interval, [self::class, 'checkMemory'], [$memoryLimit]); - return true; - } else { - return false; - } - } - - /** - * Check Memory Limit - * @param $memoryLimit - * @return void - */ - public static function checkMemory($memoryLimit) - { - if (static::isPaused() || $memoryLimit <= 0) return; - $ppid = posix_getppid(); - $childrenFile = "/proc/$ppid/task/$ppid/children"; - if (!is_file($childrenFile) || !($children = file_get_contents($childrenFile))) { - return; - } - foreach (explode(' ', $children) as $pid) { - $pid = (int)$pid; - $statusFile = "/proc/$pid/status"; - if (!is_file($statusFile) || !($status = file_get_contents($statusFile))) { - continue; - } - $mem = 0; - if (preg_match('/VmRSS\s*?:\s*?(\d+?)\s*?kB/', $status, $match)) { - $mem = $match[1]; - } - $mem = (int)($mem / 1024); - $mem >= $memoryLimit && posix_kill($pid, SIGINT); - } - } - - /** - * Get memory limit - * @param mixed $memoryLimit - * @return float - */ - private static function _getMemoryLimit($memoryLimit): float - { - if ($memoryLimit === 0) { - return floatval(0); - } - $usePhpIni = false; - if (!$memoryLimit) { - $usePhpIni = true; - $memoryLimit = ini_get('memory_limit'); - } - if ($memoryLimit == -1) return floatval(0); - $unit = strtolower($memoryLimit[strlen($memoryLimit) - 1]); - if ($unit === 'g') { - $memoryLimit = 1024 * intval($memoryLimit); - } else if ($unit === 'm') { - $memoryLimit = intval($memoryLimit); - } else if ($unit === 'k') { - $memoryLimit = intval($memoryLimit / 1024); - } else { - $memoryLimit = intval($memoryLimit / 1024 / 1024); - } - if ($memoryLimit < 30) $memoryLimit = 30; - if ($usePhpIni) $memoryLimit = (int)(0.8 * $memoryLimit); - return floatval($memoryLimit); - } -} \ No newline at end of file diff --git a/plugin/think-plugs-worker/src/command/Worker.php b/plugin/think-plugs-worker/src/command/Worker.php index c27eadeca..534a2e96a 100644 --- a/plugin/think-plugs-worker/src/command/Worker.php +++ b/plugin/think-plugs-worker/src/command/Worker.php @@ -116,11 +116,6 @@ class Worker extends Command if ('start' === $action) $output->writeln('Starting Workerman http server...'); $worker = new HttpServer($host, $port, $this->config['context'] ?? [], $this->config['callable'] ?? null); $worker->setRoot($this->app->getRootPath()); - // 设置热更新监听文件后缀及目录 - if (empty($this->config['files']['exts'])) $this->config['files']['exts'] = ['*']; - if (empty($this->config['files']['path'])) $this->config['files']['path'] = [$this->app->getBasePath(), $this->app->getConfigPath()]; - $worker->setMonitorChange(intval($this->config['files']['time'] ?? 0), $this->config['files']['path'], $this->config['files']['exts']); - $worker->setMonitorMemory(intval($this->config['memory']['time'] ?? 0), $this->config['memory']['limit'] ?? null); } else { if (strtolower($this->config['type']) !== 'business') { if (empty($this->config['listen'])) { diff --git a/plugin/think-plugs-worker/src/support/HttpServer.php b/plugin/think-plugs-worker/src/support/HttpServer.php index 49fdc129f..46e1c22b0 100644 --- a/plugin/think-plugs-worker/src/support/HttpServer.php +++ b/plugin/think-plugs-worker/src/support/HttpServer.php @@ -42,9 +42,6 @@ class HttpServer extends Server /** @var string */ protected $root; - /** @var array */ - protected $monitor; - /** @var callable */ protected $callable; @@ -79,12 +76,6 @@ class HttpServer extends Server class_alias(ThinkResponseFile::class, 'think\response\File'); } - // 设置文件变化及内存超限监控管理 - if (0 == $worker->id && $this->monitor && Library::$sapp->isDebug()) { - Monitor::enableChangeMonitor($this->monitor['change_path'] ?? [], $this->monitor['change_exts'] ?? ['*'], $this->monitor['change_time'] ?? 0); - Monitor::enableMemoryMonitor($this->monitor['memory_limit'] ?? null, $this->monitor['memory_time'] ?? 0); - } - // 初始化运行环境 RuntimeService::init($this->app)->initialize(); @@ -145,30 +136,4 @@ class HttpServer extends Server { $this->root = $path; } - - /** - * 设置文件监控配置 - * @param integer $time - * @param array $path 监听目录 - * @param array $exts 文件后缀 - * @return void - */ - public function setMonitorChange(int $time = 2, array $path = [], array $exts = ['*']) - { - $this->monitor['change_path'] = $path; - $this->monitor['change_exts'] = $exts; - $this->monitor['change_time'] = $time; - } - - /** - * 设置内存监控配置 - * @param integer $time - * @param ?string $limit - * @return void - */ - public function setMonitorMemory(int $time = 60, ?string $limit = null) - { - $this->monitor['memory_time'] = $time; - $this->monitor['memory_limit'] = $limit; - } } \ No newline at end of file diff --git a/plugin/think-plugs-worker/stc/worker.php b/plugin/think-plugs-worker/stc/worker.php index 974fe2415..c00338fdf 100644 --- a/plugin/think-plugs-worker/stc/worker.php +++ b/plugin/think-plugs-worker/stc/worker.php @@ -33,20 +33,4 @@ return [ 'name' => 'ThinkAdmin', 'count' => 4, ], - // 监控文件变更重载,仅 Debug 模式有效 - 'files' => [ - // 监控检测间隔(单位秒,零不监控) - 'time' => 3, - // 文件监控目录(默认监控 app+config 目录) - 'path' => [], - // 文件监控后缀(默认监控 所有 文件) - 'exts' => ['*'] - ], - // 监控内存超限重载,仅 Debug 模式有效 - 'memory' => [ - // 监控检测间隔(单位秒,零不监控) - 'time' => 60, - // 限制内存大小(可选单位有 G M K ) - 'limit' => '1G' - ], ]; \ No newline at end of file