easy_swoole熱重啓文件變更自動重載

由於是多協程,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());
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章