-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); } }