hyperf框架热重启实现

1.为什么需要热重启

a.在开发过程中,尤其在功能调试期间,经常会有微调整。每次如果都手动重启,那肯定还是很抓狂的;
b.在生成部署时,一键热重启,实现服务持续可用;

2.实现代码

注意:需要将服务设置成守护进程模式。(config/server.php => 'daemonize' => 1)

手动关闭和重启命令:
php bin/hyberf.php stop/reload
代码:
1.首先生成一个command文件;
2.handle的代码:
          $config = $this->container->get(ConfigInterface::class);
          $serverConfig = $config->get('server');
         if (file_exists($serverConfig['settings']['pid_file'])) {
            $pid = (int)file_get_contents($serverConfig['settings']['pid_file']);
            posix_kill($pid, SIGTERM); // 手动关闭,重启:posix_kill($pid, SIGUSR1);
            $this->line('stop server success!!!', 'info');
        } else {
            $this->line('file not exit, stop server fail!!!', 'error');
        }
自动重启(仅支持linux系统)
1.随着server启动,自动监听。通过自定义process来实现
2.核心代码:
class HotReload extends AbstractProcess
{
    protected $pid;
    /**
     * @var resource
     */
    protected $inotify;

    protected $reloading = false;

    protected $afterNSeconds = 1;

    protected $watchFiles = array();

    protected $reloadFileTypes = array('.php' => true);

    protected $events;

    protected $rootDirs = array();

    public function handle(): void
    {
        $config = $this->container->get(ConfigInterface::class);
        $serverConfig = $config->get('server');
        if (file_exists($serverConfig['settings']['pid_file'])) {
            $this->pid = $serverPid =  (int)file_get_contents($serverConfig['settings']['pid_file']);
        } else {
            throw new ServerException('pid file not exit');
        }
        if (posix_kill($serverPid, 0) === false) {
            throw new ServerException("Process#$serverPid not found.");
        }
        $this->inotify = inotify_init();
        $this->events = IN_MODIFY | IN_DELETE | IN_CREATE | IN_MOVE;
        swoole_event_add((int) $this->inotify, function ($ifd) {
            $events = inotify_read($this->inotify);
            if (!$events) {
                return;
            }
            foreach ($events as $ev) {
                if ($ev['mask'] == IN_IGNORED) {
                    continue;
                } else if ($ev['mask'] == IN_CREATE or $ev['mask'] == IN_DELETE or $ev['mask'] == IN_MODIFY or $ev['mask'] == IN_MOVED_TO or $ev['mask'] == IN_MOVED_FROM) {
                    $fileType = strrchr($ev['name'], '.');
                    //非重启类型
                    if (!isset($this->reloadFileTypes[$fileType])) {
                        continue;
                    }
                }
                //正在reload,不再接受任何事件,冻结10秒
                if (!$this->reloading) {
                    //有事件发生了,进行重启
                    swoole_timer_after($this->afterNSeconds * 1000, array($this, 'reload'));
                    $this->reloading = true;
                }
            }
        });

        $this->watch(BASE_PATH . '/app');
        $this->watch(BASE_PATH . '/config');
        $this->addFileType('.php');
        $this->run();
    }

    /**
     * 测试环境自动重启
     * @return bool
     */
    public function isEnable(): bool
    {
        $appEnv = env('APP_ENV', 'dev');
        if ($appEnv == 'dev') {
            if (!function_exists('inotify_init')) {
                return false;
            }
            return true;
        } else {
            return false;
        }
    }

    protected function watch($dir, $root = true)
    {
        //目录不存在
        if (!is_dir($dir)) {
            throw new ServerException("[$dir] is not a directory.");
        }
        //避免重复监听
        if (isset($this->watchFiles[$dir])) {
            return false;
        }
        //根目录
        if ($root) {
            $this->rootDirs[] = $dir;
        }
        $wd = inotify_add_watch($this->inotify, $dir, $this->events);
        $this->watchFiles[$dir] = $wd;
        $files = scandir($dir);
        foreach ($files as $f) {
            if ($f == '.' or $f == '..') {
                continue;
            }
            $path = $dir . '/' . $f;
            //递归目录
            if (is_dir($path)) {
                $this->watch($path, false);
            }
            //检测文件类型
            $fileType = strrchr($f, '.');
            if (isset($this->reloadFileTypes[$fileType])) {
                $wd = inotify_add_watch($this->inotify, $path, $this->events);
                $this->watchFiles[$path] = $wd;
            }
        }
        return true;
    }

}

你可能感兴趣的:(hyperf框架热重启实现)