webman
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

2 anos atrás
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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, $too_many_files_check;
  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::SKIP_DOTS | \FilesystemIterator::FOLLOW_SYMLINKS);
  77. $iterator = new \RecursiveIteratorIterator($dir_iterator);
  78. }
  79. $count = 0;
  80. foreach ($iterator as $file) {
  81. $count ++;
  82. /** var SplFileInfo $file */
  83. if (is_dir($file)) {
  84. continue;
  85. }
  86. // check mtime
  87. if ($last_mtime < $file->getMTime() && in_array($file->getExtension(), $this->_extensions, true)) {
  88. $var = 0;
  89. exec(PHP_BINARY . " -l " . $file, $out, $var);
  90. if ($var) {
  91. $last_mtime = $file->getMTime();
  92. continue;
  93. }
  94. $last_mtime = $file->getMTime();
  95. echo $file . " update and reload\n";
  96. // send SIGUSR1 signal to master process for reload
  97. if (DIRECTORY_SEPARATOR === '/') {
  98. posix_kill(posix_getppid(), SIGUSR1);
  99. } else {
  100. return true;
  101. }
  102. break;
  103. }
  104. }
  105. if (!$too_many_files_check && $count > 1000) {
  106. echo "Monitor: There are too many files ($count files) in $monitor_dir which makes file monitoring very slow\n";
  107. $too_many_files_check = 1;
  108. }
  109. }
  110. /**
  111. * @return bool
  112. */
  113. public function checkAllFilesChange()
  114. {
  115. foreach ($this->_paths as $path) {
  116. if ($this->checkFilesChange($path)) {
  117. return true;
  118. }
  119. }
  120. return false;
  121. }
  122. /**
  123. * @param $memory_limit
  124. * @return void
  125. */
  126. public function checkMemory($memory_limit)
  127. {
  128. $ppid = posix_getppid();
  129. $children_file = "/proc/$ppid/task/$ppid/children";
  130. if (!is_file($children_file) || !($children = file_get_contents($children_file))) {
  131. return;
  132. }
  133. foreach (explode(' ', $children) as $pid) {
  134. $pid = (int)$pid;
  135. $status_file = "/proc/$pid/status";
  136. if (!is_file($status_file) || !($status = file_get_contents($status_file))) {
  137. continue;
  138. }
  139. $mem = 0;
  140. if (preg_match('/VmRSS\s*?:\s*?(\d+?)\s*?kB/', $status, $match)) {
  141. $mem = $match[1];
  142. }
  143. $mem = (int)($mem / 1024);
  144. if ($mem >= $memory_limit) {
  145. posix_kill($pid, SIGINT);
  146. }
  147. }
  148. }
  149. /**
  150. * Get memory limit
  151. * @return float
  152. */
  153. protected function getMemoryLimit($memory_limit)
  154. {
  155. if ($memory_limit === 0) {
  156. return 0;
  157. }
  158. $use_php_ini = false;
  159. if (!$memory_limit) {
  160. $memory_limit = ini_get('memory_limit');
  161. $use_php_ini = true;
  162. }
  163. if ($memory_limit == -1) {
  164. return 0;
  165. }
  166. $unit = $memory_limit[strlen($memory_limit) - 1];
  167. if ($unit == 'G') {
  168. $memory_limit = 1024 * (int)$memory_limit;
  169. } else if ($unit == 'M') {
  170. $memory_limit = (int)$memory_limit;
  171. } else if ($unit == 'K') {
  172. $memory_limit = (int)($memory_limit / 1024);
  173. } else {
  174. $memory_limit = (int)($memory_limit / (1024 * 1024));
  175. }
  176. if ($memory_limit < 30) {
  177. $memory_limit = 30;
  178. }
  179. if ($use_php_ini) {
  180. $memory_limit = (int)(0.8 * $memory_limit);
  181. }
  182. return $memory_limit;
  183. }
  184. }