lizhichao / one

A minimalist high-performance php framework that supports the [swoole | php-fpm] environment
Apache License 2.0
885 stars 127 forks source link

建议添加热重启 #1

Closed yumufeng closed 5 years ago

yumufeng commented 5 years ago

首先在protocol.php配置加上

'monitor' => [
            'timer' => 2000,  //定时器间隔时间,单位毫秒
            'debug' => true,       //重启
            'path' => [
                _APP_PATH_
            ]
        ],

其次找到 vendor/lizhichao/one/src/Swoole/Server.php 修改onWorkerStart方法,加上

  $this->lastMtime = time();
  if (0 == $worker_id) {
            $this->monitor($server);
   }

接着加上monitor方法

  protected function monitor(\swoole_server $server)
    {
        $monitor = $this->conf['monitor'];
        $paths = $monitor['path'];
        $timer = $monitor['timer'] ?: 2;

        $server->tick($timer, function () use ($paths, $server) {
            foreach ($paths as $path) {
                $dir = new \RecursiveDirectoryIterator($path);
                $iterator = new \RecursiveIteratorIterator($dir);

                foreach ($iterator as $file) {
                    if (pathinfo($file, PATHINFO_EXTENSION) != 'php') {
                        continue;
                    }

                    if ($this->lastMtime < $file->getMTime()) {
                        $this->lastMtime = $file->getMTime();
                        echo '[update]' . $file . " reload...\n";
                        $server->reload();
                        return;
                    }
                }
            }
        });
    }

这样只要App目录下,有代码修改,在监控为debug=true时候,会自动重启,不必每次手动重启

lizhichao commented 5 years ago

这个功能不属于框架的需求,暂时不考虑耦合在框架里面。 inotifywait 就是一个非常好的文件监控命令 可自己实现。