Monitor.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. /**
  3. * This file is part of webman.
  4. *
  5. * Licensed under The MIT License
  6. * For full copyright and license information, please see the MIT-LICENSE.txt
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @author walkor<walkor@workerman.net>
  10. * @copyright walkor<walkor@workerman.net>
  11. * @link http://www.workerman.net/
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace process;
  15. use FilesystemIterator;
  16. use RecursiveDirectoryIterator;
  17. use RecursiveIteratorIterator;
  18. use SplFileInfo;
  19. use Workerman\Timer;
  20. use Workerman\Worker;
  21. /**
  22. * Class FileMonitor
  23. * @package process
  24. */
  25. class Monitor
  26. {
  27. /**
  28. * @var array
  29. */
  30. protected $paths = [];
  31. /**
  32. * @var array
  33. */
  34. protected $extensions = [];
  35. /**
  36. * @var string
  37. */
  38. public static $lockFile = __DIR__ . '/../runtime/monitor.lock';
  39. /**
  40. * Pause monitor
  41. * @return void
  42. */
  43. public static function pause()
  44. {
  45. file_put_contents(static::$lockFile, time());
  46. }
  47. /**
  48. * Resume monitor
  49. * @return void
  50. */
  51. public static function resume(): void
  52. {
  53. clearstatcache();
  54. if (is_file(static::$lockFile)) {
  55. unlink(static::$lockFile);
  56. }
  57. }
  58. /**
  59. * Whether monitor is paused
  60. * @return bool
  61. */
  62. public static function isPaused(): bool
  63. {
  64. clearstatcache();
  65. return file_exists(static::$lockFile);
  66. }
  67. /**
  68. * FileMonitor constructor.
  69. * @param $monitorDir
  70. * @param $monitorExtensions
  71. * @param array $options
  72. */
  73. public function __construct($monitorDir, $monitorExtensions, array $options = [])
  74. {
  75. static::resume();
  76. $this->paths = (array)$monitorDir;
  77. $this->extensions = $monitorExtensions;
  78. if (!Worker::getAllWorkers()) {
  79. return;
  80. }
  81. $disableFunctions = explode(',', ini_get('disable_functions'));
  82. if (in_array('exec', $disableFunctions, true)) {
  83. echo "\nMonitor file change turned off because exec() has been disabled by disable_functions setting in " . PHP_CONFIG_FILE_PATH . "/php.ini\n";
  84. } else {
  85. if ($options['enable_file_monitor'] ?? true) {
  86. Timer::add(1, function () {
  87. $this->checkAllFilesChange();
  88. });
  89. }
  90. }
  91. $memoryLimit = $this->getMemoryLimit($options['memory_limit'] ?? null);
  92. if ($memoryLimit && ($options['enable_memory_monitor'] ?? true)) {
  93. Timer::add(60, [$this, 'checkMemory'], [$memoryLimit]);
  94. }
  95. }
  96. /**
  97. * @param $monitorDir
  98. * @return bool
  99. */
  100. public function checkFilesChange($monitorDir): bool
  101. {
  102. static $lastMtime, $tooManyFilesCheck;
  103. if (!$lastMtime) {
  104. $lastMtime = time();
  105. }
  106. clearstatcache();
  107. if (!is_dir($monitorDir)) {
  108. if (!is_file($monitorDir)) {
  109. return false;
  110. }
  111. $iterator = [new SplFileInfo($monitorDir)];
  112. } else {
  113. // recursive traversal directory
  114. $dirIterator = new RecursiveDirectoryIterator($monitorDir, FilesystemIterator::SKIP_DOTS | FilesystemIterator::FOLLOW_SYMLINKS);
  115. $iterator = new RecursiveIteratorIterator($dirIterator);
  116. }
  117. $count = 0;
  118. foreach ($iterator as $file) {
  119. $count ++;
  120. /** var SplFileInfo $file */
  121. if (is_dir($file->getRealPath())) {
  122. continue;
  123. }
  124. // check mtime
  125. if (in_array($file->getExtension(), $this->extensions, true) && $lastMtime < $file->getMTime()) {
  126. $var = 0;
  127. exec('"'.PHP_BINARY . '" -l ' . $file, $out, $var);
  128. $lastMtime = $file->getMTime();
  129. if ($var) {
  130. continue;
  131. }
  132. echo $file . " update and reload\n";
  133. // send SIGUSR1 signal to master process for reload
  134. if (DIRECTORY_SEPARATOR === '/') {
  135. posix_kill(posix_getppid(), SIGUSR1);
  136. } else {
  137. return true;
  138. }
  139. break;
  140. }
  141. }
  142. if (!$tooManyFilesCheck && $count > 1000) {
  143. echo "Monitor: There are too many files ($count files) in $monitorDir which makes file monitoring very slow\n";
  144. $tooManyFilesCheck = 1;
  145. }
  146. return false;
  147. }
  148. /**
  149. * @return bool
  150. */
  151. public function checkAllFilesChange(): bool
  152. {
  153. if (static::isPaused()) {
  154. return false;
  155. }
  156. foreach ($this->paths as $path) {
  157. if ($this->checkFilesChange($path)) {
  158. return true;
  159. }
  160. }
  161. return false;
  162. }
  163. /**
  164. * @param $memoryLimit
  165. * @return void
  166. */
  167. public function checkMemory($memoryLimit)
  168. {
  169. if (static::isPaused() || $memoryLimit <= 0) {
  170. return;
  171. }
  172. $ppid = posix_getppid();
  173. $childrenFile = "/proc/$ppid/task/$ppid/children";
  174. if (!is_file($childrenFile) || !($children = file_get_contents($childrenFile))) {
  175. return;
  176. }
  177. foreach (explode(' ', $children) as $pid) {
  178. $pid = (int)$pid;
  179. $statusFile = "/proc/$pid/status";
  180. if (!is_file($statusFile) || !($status = file_get_contents($statusFile))) {
  181. continue;
  182. }
  183. $mem = 0;
  184. if (preg_match('/VmRSS\s*?:\s*?(\d+?)\s*?kB/', $status, $match)) {
  185. $mem = $match[1];
  186. }
  187. $mem = (int)($mem / 1024);
  188. if ($mem >= $memoryLimit) {
  189. posix_kill($pid, SIGINT);
  190. }
  191. }
  192. }
  193. /**
  194. * Get memory limit
  195. * @return float
  196. */
  197. protected function getMemoryLimit($memoryLimit)
  198. {
  199. if ($memoryLimit === 0) {
  200. return 0;
  201. }
  202. $usePhpIni = false;
  203. if (!$memoryLimit) {
  204. $memoryLimit = ini_get('memory_limit');
  205. $usePhpIni = true;
  206. }
  207. if ($memoryLimit == -1) {
  208. return 0;
  209. }
  210. $unit = strtolower($memoryLimit[strlen($memoryLimit) - 1]);
  211. if ($unit === 'g') {
  212. $memoryLimit = 1024 * (int)$memoryLimit;
  213. } else if ($unit === 'm') {
  214. $memoryLimit = (int)$memoryLimit;
  215. } else if ($unit === 'k') {
  216. $memoryLimit = ((int)$memoryLimit / 1024);
  217. } else {
  218. $memoryLimit = ((int)$memoryLimit / (1024 * 1024));
  219. }
  220. if ($memoryLimit < 30) {
  221. $memoryLimit = 30;
  222. }
  223. if ($usePhpIni) {
  224. $memoryLimit = (int)(0.8 * $memoryLimit);
  225. }
  226. return $memoryLimit;
  227. }
  228. }