start.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. {
  13. global $monitor_dir;
  14. // watch files only in daemon mode
  15. if(!Worker::$daemonize)
  16. {
  17. // chek mtime of files per second
  18. Timer::add(1, 'check_files_change', array($monitor_dir));
  19. }
  20. };
  21. // check files func
  22. function check_files_change($monitor_dir)
  23. {
  24. global $last_mtime;
  25. // recursive traversal directory
  26. $dir_iterator = new RecursiveDirectoryIterator($monitor_dir);
  27. $iterator = new RecursiveIteratorIterator($dir_iterator);
  28. foreach ($iterator as $file)
  29. {
  30. // only check php files
  31. if(pathinfo($file, PATHINFO_EXTENSION) != 'php')
  32. {
  33. continue;
  34. }
  35. // check mtime
  36. if($last_mtime < $file->getMTime())
  37. {
  38. echo $file." update and reload\n";
  39. // send SIGUSR1 signal to master process for reload
  40. posix_kill(posix_getppid(), SIGUSR1);
  41. $last_mtime = $file->getMTime();
  42. break;
  43. }
  44. }
  45. }