Skip to content

Commit

Permalink
重写HotReload例子
Browse files Browse the repository at this point in the history
  • Loading branch information
evalor committed Dec 19, 2018
1 parent 0f3356c commit b16a996
Showing 1 changed file with 45 additions and 9 deletions.
54 changes: 45 additions & 9 deletions App/Process/HotReload.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,32 @@ class HotReload extends AbstractProcess
protected $table;
protected $isReady = false;

protected $monitorDir; // 需要监控的目录
protected $monitorExt; // 需要监控的后缀

/**
* 启动定时器进行循环扫描
* @param Process $process
*/
public function run(Process $process)
{
$disableInotify = $this->getArg('disableInotify');
$monitorDir = $this->getArg('monitorDir');
$monitorExt = $this->getArg('monitorExt');

// 此处指定需要监视的目录 建议只监视App目录下的文件变更
$this->monitorDir = $monitorDir ? $monitorDir : EASYSWOOLE_ROOT . '/App';

// 指定需要监控的扩展名 不属于指定类型的的文件 无视变更 不重启
$this->monitorExt = $monitorExt && is_array($monitorExt) ? $monitorExt : ['php'];

if (extension_loaded('inotify') && !$disableInotify) {
// 扩展可用 优先使用扩展进行处理
$this->registerInotifyEvent();
echo "server hot reload start : use inotify\n";
} else {
// 扩展不可用时 进行暴力扫描
$this->table = new Table(2048);
$this->table = new Table(512);
$this->table->column('mtime', Table::TYPE_INT, 4);
$this->table->create();
$this->runComparison();
Expand All @@ -57,31 +69,55 @@ private function runComparison()
{
$startTime = microtime(true);
$doReload = false;
$files = File::scanDirectory(EASYSWOOLE_ROOT . '/App');
if (isset($files['files'])) {
foreach ($files['files'] as $file) {
$currentTime = filemtime($file);
$inode = crc32($file);

$dirIterator = new \RecursiveDirectoryIterator($this->monitorDir);
$iterator = new \RecursiveIteratorIterator($dirIterator);
$inodeList = array();

// 迭代目录全部文件进行检查
foreach ($iterator as $file) {
/** @var \SplFileInfo $file */
$ext = $file->getExtension();
if (!in_array($ext, $this->monitorExt)) {
continue; // 只检查指定类型
} else {
// 由于修改文件名称 并不需要重新载入 可以基于inode进行监控
$inode = $file->getInode();
$mtime = $file->getMTime();
array_push($inodeList, $inode);
if (!$this->table->exist($inode)) {
// 新建文件或修改文件 变更了inode
$this->table->set($inode, ['mtime' => $mtime]);
$doReload = true;
$this->table->set($inode, ['mtime' => $currentTime]);
} else {
// 修改文件 但未发生inode变更
$oldTime = $this->table->get($inode)['mtime'];
if ($oldTime != $currentTime) {
if ($oldTime != $mtime) {
$this->table->set($inode, ['mtime' => $mtime]);
$doReload = true;
}
$this->table->set($inode, ['mtime' => $currentTime]);
}
}
}

foreach ($this->table as $inode => $value) {
// 迭代table寻找需要删除的inode
if (!in_array(intval($inode), $inodeList)) {
$this->table->del($inode);
$doReload = true;
}
}

if ($doReload) {
$count = $this->table->count();
$time = date('Y-m-d H:i:s');
$usage = round(microtime(true) - $startTime, 3);
if (!$this->isReady == false) {
// 监测到需要进行热重启
echo "severReload at {$time} use : {$usage} s total: {$count} files\n";
ServerManager::getInstance()->getSwooleServer()->reload();
} else {
// 首次扫描不需要进行重启操作
echo "hot reload ready at {$time} use : {$usage} s total: {$count} files\n";
$this->isReady = true;
}
Expand Down

0 comments on commit b16a996

Please sign in to comment.