Monitor.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 Workerman\Timer;
  16. use Workerman\Worker;
  17. /**
  18. * Class FileMonitor
  19. * @package process
  20. */
  21. class Monitor
  22. {
  23. /**
  24. * @var array
  25. */
  26. protected $_paths = [];
  27. /**
  28. * @var array
  29. */
  30. protected $_extensions = [];
  31. /**
  32. * FileMonitor constructor.
  33. * @param $monitor_dir
  34. * @param $monitor_extensions
  35. * @param $memory_limit
  36. */
  37. public function __construct($monitor_dir, $monitor_extensions, $memory_limit = null)
  38. {
  39. $this->_paths = (array)$monitor_dir;
  40. $this->_extensions = $monitor_extensions;
  41. if (!Worker::getAllWorkers()) {
  42. return;
  43. }
  44. $disable_functions = explode(',', ini_get('disable_functions'));
  45. if (in_array('exec', $disable_functions, true)) {
  46. echo "\nMonitor file change turned off because exec() has been disabled by disable_functions setting in " . PHP_CONFIG_FILE_PATH . "/php.ini\n";
  47. } else {
  48. if (!Worker::$daemonize) {
  49. Timer::add(1, function () {
  50. $this->checkAllFilesChange();
  51. });
  52. }
  53. }
  54. $memory_limit = $this->getMemoryLimit($memory_limit);
  55. if ($memory_limit && DIRECTORY_SEPARATOR === '/') {
  56. Timer::add(60, [$this, 'checkMemory'], [$memory_limit]);
  57. }
  58. }
  59. /**
  60. * @param $monitor_dir
  61. */
  62. public function checkFilesChange($monitor_dir)
  63. {
  64. static $last_mtime;
  65. if (!$last_mtime) {
  66. $last_mtime = time();
  67. }
  68. clearstatcache();
  69. if (!is_dir($monitor_dir)) {
  70. if (!is_file($monitor_dir)) {
  71. return;
  72. }
  73. $iterator = [new \SplFileInfo($monitor_dir)];
  74. } else {
  75. // recursive traversal directory
  76. $dir_iterator = new \RecursiveDirectoryIterator($monitor_dir, \FilesystemIterator::FOLLOW_SYMLINKS);
  77. $iterator = new \RecursiveIteratorIterator($dir_iterator);
  78. }
  79. foreach ($iterator as $file) {
  80. /** var SplFileInfo $file */
  81. if (is_dir($file)) {
  82. continue;
  83. }
  84. // check mtime
  85. if ($last_mtime < $file->getMTime() && in_array($file->getExtension(), $this->_extensions, true)) {
  86. $var = 0;
  87. exec(PHP_BINARY . " -l " . $file, $out, $var);
  88. if ($var) {
  89. $last_mtime = $file->getMTime();
  90. continue;
  91. }
  92. $last_mtime = $file->getMTime();
  93. echo $file . " update and reload\n";
  94. // send SIGUSR1 signal to master process for reload
  95. if (DIRECTORY_SEPARATOR === '/') {
  96. posix_kill(posix_getppid(), SIGUSR1);
  97. } else {
  98. return true;
  99. }
  100. break;
  101. }
  102. }
  103. }
  104. /**
  105. * @return bool
  106. */
  107. public function checkAllFilesChange()
  108. {
  109. foreach ($this->_paths as $path) {
  110. if ($this->checkFilesChange($path)) {
  111. return true;
  112. }
  113. }
  114. return false;
  115. }
  116. /**
  117. * @param $memory_limit
  118. * @return void
  119. */
  120. public function checkMemory($memory_limit)
  121. {
  122. $ppid = posix_getppid();
  123. $children_file = "/proc/$ppid/task/$ppid/children";
  124. if (!is_file($children_file) || !($children = file_get_contents($children_file))) {
  125. return;
  126. }
  127. foreach (explode(' ', $children) as $pid) {
  128. $pid = (int)$pid;
  129. $status_file = "/proc/$pid/status";
  130. if (!is_file($status_file) || !($status = file_get_contents($status_file))) {
  131. continue;
  132. }
  133. $mem = 0;
  134. if (preg_match('/VmRSS\s*?:\s*?(\d+?)\s*?kB/', $status, $match)) {
  135. $mem = $match[1];
  136. }
  137. $mem = (int)($mem / 1024);
  138. if ($mem >= $memory_limit) {
  139. posix_kill($pid, SIGINT);
  140. }
  141. }
  142. }
  143. /**
  144. * Get memory limit
  145. * @return float
  146. */
  147. protected function getMemoryLimit($memory_limit)
  148. {
  149. if ($memory_limit === 0) {
  150. return 0;
  151. }
  152. $use_php_ini = false;
  153. if (!$memory_limit) {
  154. $memory_limit = ini_get('memory_limit');
  155. $use_php_ini = true;
  156. }
  157. if ($memory_limit == -1) {
  158. return 0;
  159. }
  160. $unit = $memory_limit[strlen($memory_limit) - 1];
  161. if ($unit == 'G') {
  162. $memory_limit = 1024 * (int)$memory_limit;
  163. } else if ($unit == 'M') {
  164. $memory_limit = (int)$memory_limit;
  165. } else if ($unit == 'K') {
  166. $memory_limit = (int)($memory_limit / 1024);
  167. } else {
  168. $memory_limit = (int)($memory_limit / (1024 * 1024));
  169. }
  170. if ($memory_limit < 30) {
  171. $memory_limit = 30;
  172. }
  173. if ($use_php_ini) {
  174. $memory_limit = (int)(0.8 * $memory_limit);
  175. }
  176. return $memory_limit;
  177. }
  178. }