使用inotify扩展监控项目目录变化
参考:https://my.oschina.net/matyhtf/blog/343508
安装inotify扩展
pecl install inotify
extension=inotify.so
使用
遍历出目录下的全部文件
inotify使用是的阻塞模式,配合swoole实现异步非阻塞
使用共享的目录则无法被监听
if(extension_loaded('inotify')){
Logger::getInstance()->console('auto reload enable');
//监控应用目录,当应用目录有变动的时候,自动热重启。
//注意,并不是全部更改热重启都会生效,如:不受主进程管理(比如自定义进程),不在回调函数内的代码,比如在全局的Event中的代码。
$a = function ($dir) use (&$a) {
$data = array();
if (is_dir($dir)) {
//是目录的话,先增当前目录进去
$data[] = $dir;
$files = array_diff(scandir($dir), array('.', '..'));
foreach ($files as $file) {
$data = array_merge($data, $a($dir . "/" . $file));
}
} else {
$data[] = $dir;
}
return $data;
};
$list = $a(EASYSWOOLE_ROOT.\Yaconf::get('common.app_dir', '/App'));
$notify = inotify_init();
// 为所有目录和文件添加inotify监视
foreach ($list as $item) {
inotify_add_watch($notify, $item, IN_CREATE | IN_DELETE | IN_MODIFY);
}
// 加入EventLoop异步非阻塞监听
swoole_event_add($notify, function () use ($notify) {
$events = inotify_read($notify);
if (!empty($events)) {
//注意更新多个文件的间隔时间处理,防止一次更新了10个文件,重启了10次,懒得做了,反正原理在这里
Logger::getInstance()->console('service is going to reload');
ServerManager::getInstance()->getServer()->reload();
}
});
}
PHP扩展 -- 使用inotify扩展监控项目目录变化
最新推荐文章于 2024-04-10 16:58:53 发布