File-Watcher 组件

用于在 EasySwoole 中实现热重启,让开发变得更简便。

组件要求

  • easyswoole/spl: ^1.3
  • easyswoole/component: ^2.2

安装方法

composer require easyswoole/file-watcher

仓库地址

easyswoole/file-watcher

WatchRule

监控目录:

  1. $watchRule = new \EasySwoole\FileWatcher\WatchRule(EASYSWOOLE_ROOT."/App");

指定忽略目录:

  1. /**@var \EasySwoole\FileWatcher\WatchRule $watchRule **/
  2. $watchRule->setIgnorePaths([EASYSWOOLE_ROOT."/App/Api", EASYSWOOLE_ROOT."/App/Admin"]);

指定忽略文件:

  1. /**@var \EasySwoole\FileWatcher\WatchRule $watchRule **/
  2. $watchRule->setIgnoreFiles([EASYSWOOLE_ROOT."/App/Api/Teacher.php", EASYSWOOLE_ROOT."/App/Admin/Teacher.php"]);

指定匹配后缀:

  1. /**@var \EasySwoole\FileWatcher\WatchRule $watchRule **/
  2. $watchRule->setType($watchRule::SCAN_TYPE_SUFFIX_MATCH);
  3. //$watchRule->setType($watchRule::SCAN_TYPE_IGNORE_SUFFIX);
  4. $watchRule->setSuffix(['php']);
  5. typeSCAN_TYPE_SUFFIX_MATCH时,只匹配后缀必须在suffix规则数组的文件。 typeSCAN_TYPE_IGNORE_SUFFIX时,会忽略掉后缀在suffix规则数组的文件。

FileWatcher

设置监控程序:

  1. $fileWatcher = new \EasySwoole\FileWatcher\FileWatcher();
  2. $fileWatcher->setScannerDriver(\EasySwoole\FileWatcher\Scanner\Inotify::class);
  3. $fileWatcher->setScannerDriver(\EasySwoole\FileWatcher\Scanner\FileScanner::class);
  4. 二选一 不调用此方法 存在inotify扩展默认为Inotify::class反之FileScanner::class

增加监控规则:

  1. /**@var \EasySwoole\FileWatcher\FileWatcher $fileWatcher **/
  2. $fileWatcher->addRule(new \EasySwoole\FileWatcher\WatchRule(__DIR__));
  3. $fileWatcher->addRule(new \EasySwoole\FileWatcher\WatchRule(EASYSWOOLE_ROOT. '/App'));
  4. 可进行多次调用 对不同目录设置不同的规则

设置异常回调:

  1. /**@var \EasySwoole\FileWatcher\FileWatcher $fileWatcher **/
  2. $fileWatcher->setOnException(function (\Throwable $throwable){
  3. });

设置检测周期(默认1000ms):

  1. /**@var \EasySwoole\FileWatcher\FileWatcher $fileWatcher **/
  2. $fileWatcher->setCheckInterval(1000);

设置触发回调(文件有变化):

  1. /**@var \EasySwoole\FileWatcher\FileWatcher $fileWatcher **/
  2. $fileWatcher->setOnChange(function (array $list, \EasySwoole\FileWatcher\WatchRule $rule){
  3. // list为变化的文件列表
  4. });

启动(swoole服务中使用):

  1. /**@var \EasySwoole\FileWatcher\FileWatcher $fileWatcher **/
  2. /**@var \Swoole\Server $server **/
  3. $fileWatcher->attachServer($server);

EasySwoole 中用于热重启

例如在 EasySwoole 开发模式中,我们希望当有代码变动的时候,实现 Server 重启,只需要在 EasySwoole 的全局事件 EasySwooleEvent 中注册一下即可实现。 示例代码如下:

  1. <?php
  2. namespace EasySwoole\EasySwoole;
  3. use EasySwoole\EasySwoole\AbstractInterface\Event;
  4. use EasySwoole\EasySwoole\Swoole\EventRegister;
  5. use EasySwoole\FileWatcher\FileWatcher;
  6. use EasySwoole\FileWatcher\WatchRule;
  7. class EasySwooleEvent implements Event
  8. {
  9. public static function initialize()
  10. {
  11. date_default_timezone_set('Asia/Shanghai');
  12. }
  13. public static function mainServerCreate(EventRegister $register)
  14. {
  15. $watcher = new FileWatcher();
  16. $rule = new WatchRule(EASYSWOOLE_ROOT . "/App"); // 设置监控规则和监控目录
  17. $watcher->addRule($rule);
  18. $watcher->setOnChange(function () {
  19. Logger::getInstance()->info('file change ,reload!!!');
  20. ServerManager::getInstance()->getSwooleServer()->reload();
  21. });
  22. $watcher->attachServer(ServerManager::getInstance()->getSwooleServer());
  23. }
  24. }

注意,reload 仅仅针对 Worker进程 加载的代码有效。