start.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. use Workerman\Worker;
  3. use Workerman\Lib\Timer;
  4. // watch Applications catalogue
  5. $monitor_dir = realpath(__DIR__ . '/..');
  6. // worker
  7. $worker = new Worker();
  8. $worker->name = 'FileMonitor';
  9. $worker->reloadable = false;
  10. $last_mtime = time();
  11. $worker->onWorkerStart = function () {
  12. global $monitor_dir;
  13. // watch files only in daemon mode
  14. if (!Worker::$daemonize) {
  15. // chek mtime of files per second
  16. Timer::add(1, 'check_files_change', array($monitor_dir));
  17. }
  18. };
  19. // check files func
  20. function check_files_change($monitor_dir)
  21. {
  22. global $last_mtime;
  23. // recursive traversal directory
  24. $dir_iterator = new RecursiveDirectoryIterator($monitor_dir);
  25. $iterator = new RecursiveIteratorIterator($dir_iterator);
  26. foreach ($iterator as $file) {
  27. // only check php files
  28. if (pathinfo($file, PATHINFO_EXTENSION) != 'php') {
  29. continue;
  30. }
  31. // check mtime
  32. if ($last_mtime < $file->getMTime()) {
  33. echo $file . " update and reload\n";
  34. // send SIGUSR1 signal to master process for reload
  35. posix_kill(posix_getppid(), SIGUSR1);
  36. $last_mtime = $file->getMTime();
  37. break;
  38. }
  39. }
  40. }