├── README.md └── Applications └── FileMonitor └── start.php /README.md: -------------------------------------------------------------------------------- 1 | # workerman-filemonitor 2 | 监控文件更新并自动reload workerman。默认只监控Applications目录,如果要监控其它目录,请更改Applications/FileMonitor/start.php中$monitor_dir,路径最好使用绝对路径。 3 | 4 | # 注意 5 | 只能更新onXXX中加载的文件,启动脚本直接加载的文件和代码无法自动更新 6 | 7 | 只有在debug模式启动后才有效,daemon模式不生效。 8 | 9 | # 使用 10 | 直接将Applications下的FileMonitor目录拷贝到你自己的Applications目录下,重启workerman即可 11 | -------------------------------------------------------------------------------- /Applications/FileMonitor/start.php: -------------------------------------------------------------------------------- 1 | name = 'FileMonitor'; 11 | $worker->reloadable = false; 12 | $last_mtime = time(); 13 | 14 | $worker->onWorkerStart = function() use ($monitor_dir) 15 | { 16 | // global $monitor_dir; 17 | // watch files only in debug mode 18 | if(!Worker::$daemonize) 19 | { 20 | // chek mtime of files per second 21 | Timer::add(1, 'check_files_change', array($monitor_dir)); 22 | } 23 | }; 24 | 25 | // check files func 26 | function check_files_change($monitor_dir) 27 | { 28 | global $last_mtime; 29 | // recursive traversal directory 30 | $dir_iterator = new RecursiveDirectoryIterator($monitor_dir); 31 | $iterator = new RecursiveIteratorIterator($dir_iterator); 32 | foreach ($iterator as $file) 33 | { 34 | // only check php files 35 | if(pathinfo($file, PATHINFO_EXTENSION) != 'php') 36 | { 37 | continue; 38 | } 39 | // check mtime 40 | if($last_mtime < $file->getMTime()) 41 | { 42 | echo $file." update and reload\n"; 43 | // send SIGUSR1 signal to master process for reload 44 | posix_kill(posix_getppid(), SIGUSR1); 45 | $last_mtime = $file->getMTime(); 46 | break; 47 | } 48 | } 49 | } 50 | --------------------------------------------------------------------------------