由于是多协程,swoole把大妈都加载到了内存,所以本地文件变更之后,运行代码仍然是变更前的代码,这给开发带来非常大的麻烦,谁也不想每次改动之后就去手动重启一下easy_swoole,所以easy_swoole也给出了两种方案。
方案一:inotify
使用linux的inotify特性,异步监控文件修改,发现变更就重载easy_swoole服务,这里面需要用到php的拓展inotify来调用linux系统的inotify命令;
方案二:暴力扫描
使用swoole进行暴力扫描,指定目录,发现变更就重载服务;
以下代码是整合两种方案的php代码:
D:\WWW\passport_v1.xxx.com\App\Process\HotReload.php
<?php
namespace App\Process;
use EasySwoole\Component\Process\AbstractProcess;
use EasySwoole\EasySwoole\ServerManager;
use EasySwoole\Utility\File;
use Swoole\Process;
use Swoole\Table;
use Swoole\Timer;
/**
* 暴力热重载
* Class HotReload
* @package App\Process
*/
class HotReload extends AbstractProcess
{
/**
* @var \swoole_table $table
*/
protected $table;
protected $isReady = false;
protected $monitorDir; // 需要监控的目录
protected $monitorExt; // 需要监控的后缀
/**
* 启动定时器进行循环扫描
*/
public function run($arg)
{
// 此处指定需要监视的目录 建议只监视App目录下的文件变更
$this->monitorDir = !empty($arg['monitorDir']) ? $arg['monitorDir'] : EASYSWOOLE_ROOT . '/App';
// 指定需要监控的扩展名 不属于指定类型的的文件 无视变更 不重启
$this->monitorExt = !empty($arg['monitorExt']) && is_array($arg['monitorExt']) ? $arg['monitorExt'] : ['php'];
if (extension_loaded('inotify') && empty($arg['disableInotify'])) {
// 扩展可用 优先使用扩展进行处理
$this->registerInotifyEvent();
echo "server hot reload start : use inotify\n";
} else {
// 扩展不可用时 进行暴力扫描
$this->table = new Table(512);
$this->table->column('mtime', Table::TYPE_INT, 4);
$this->table->create();
$this->runComparison();
Timer::tick(1000, function () {
$this->runComparison();
});
echo "server hot reload start : use timer tick comparison\n";
}
}
/**
* 扫描文件变更
*/
private function runComparison()
{
$startTime = microtime(true);
$doReload = false;
$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;
} else {
// 修改文件 但未发生inode变更
$oldTime = $this->table->get($inode)['mtime'];
if ($oldTime != $mtime) {
$this->table->set($inode, ['mtime' => $mtime]);
$doReload = true;
}
}
}
}
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;
}
}
}
/**
* 注册Inotify监听事件
*/
private function registerInotifyEvent()
{
// 因为进程独立 且当前是自定义进程 全局变量只有该进程使用
// 在确定不会造成污染的情况下 也可以合理使用全局变量
global $lastReloadTime;
global $inotifyResource;
$lastReloadTime = 0;
$files = File::scanDirectory(EASYSWOOLE_ROOT . '/App');
$files = array_merge($files['files'], $files['dirs']);
$inotifyResource = inotify_init();
// 为当前所有的目录和文件添加事件监听
foreach ($files as $item) {
inotify_add_watch($inotifyResource, $item, IN_CREATE | IN_DELETE | IN_MODIFY);
}
// 加入事件循环
swoole_event_add($inotifyResource, function () {
global $lastReloadTime;
global $inotifyResource;
$events = inotify_read($inotifyResource);
if ($lastReloadTime < time() && !empty($events)) {
// 限制1s内不能进行重复reload
$lastReloadTime = time();
ServerManager::getInstance()->getSwooleServer()->reload();
}
});
}
public function onShutDown()
{
// TODO: Implement onShutDown() method.
}
public function onReceive(string $str)
{
// TODO: Implement onReceive() method.
}
}
把监控服务加入事务中:
D:\WWW\passport_v1.xx.com\EasySwooleEvent.php
public static function mainServerCreate(EventRegister $register)
{
# 启动mysql服务
$config = new Config(GlobalConfig::getInstance()->getConf("MYSQL"));
DbManager::getInstance()->addConnection(new Connection($config));
$register->add($register::onWorkerStart, function () {
//链接预热
DbManager::getInstance()->getConnection()->getClientPool()->keepMin();
});
# 文件变更之后热加载
$swooleServer = ServerManager::getInstance()->getSwooleServer();
$swooleServer->addProcess((new HotReload('HotReload', ['disableInotify' => true]))->getProcess());
}