• swoole热启动


    通过扫描指定的要扫描的目录,把所有文件找出来,分别md5 连接字符串,最后再md5返回 

    启动定时器,扫描,当前的加密值和以前一样不管,否则就重启服务,把当前赋值给旧值 。

    httpServer.php

    <?php
    
    namespace WangCoreServer;
    
    use WangCoreBeanBeanFactory;
    use WangCoreRouteAnnotationMappingRequestMapping;
    use WangCoreRouteAnnotationParserRequestMappingParser;
    use WangCoreRouteRoute;
    
    class HttpServer
    {
    
        public $server;
    
        public function run()
        {
            $config = BeanFactory::make("config")->get("http-server");
            $this->server = new SwooleHttpServer($config['host'], $config['port']);
            $this->server->set($config['setting']);
            $this->server->on("request", [$this, 'httpRequest']);
            $this->server->on("workerstart", [$this, "workerStart"]);
            $this->server->on("start", [$this, "start"]);
    
            $this->server->start();
        }
    
        public function start($server)
        {
            echo "main worker  启动" . PHP_EOL;
            $dirs = BeanFactory::make("config")->get('reload_dirs');
            $reload = bean("reload");
            SwooleTimer::Tick(3000, function () use ($reload, $dirs) {
                //echo "定时器" . PHP_EOL;
                $newMd5 = $reload->md5Dirs($dirs);
                if ($reload->needReload($newMd5)) {
                    $this->server->reload();
                }
            });
        }
    
    
        /**
         * onworker start 回调
         * @param $server
         * @param $workerId
         * author: brady
         * date: 2020/7/23 16:03
         */
        public function workerStart($server, $workerId)
        {
            echo "worker " . $workerId . " 启动" . PHP_EOL;
            $this->loadAnnotations();
        }
    
        /**
         * http 请求接受
         * @param $request
         * @param $response
         * author: brady
         * date: 2020/7/23 16:03
         */
        public function httpRequest($request, $response)
        {
            if ($request->server['request_uri'] != "/favicon.ico") {
                echo "client connect fd:" . $request->fd . PHP_EOL;
                $path_info = $request->server['path_info'];
                $method = $request->server['request_method'];
    
                $content = Route::dispatch($method, $path_info);
    
                $response->header("Content-Type", "text/html; charset=utf-8");
                $response->end($content);
            }
    
        }
    
        /**
         * 获取 apppath的路由 下面所有文件,得到注解路由 放到路由属性$routes 对象里面
         */
        public function loadAnnotations()
        {
            get_files_by_tree(APP_PATH, $dirs, $filter = "controller");
            if (!empty($dirs)) {
                foreach ($dirs as $file) {
                    // 根据绝对路径文件名 获取带命名空间的类
                    $className = getClassNameByFilePath($file);
                    $obj = new $className;
                    $reflect = new ReflectionObject($obj);
                    $methods = $reflect->getMethods();
                    if (!empty($methods)) {
                        foreach ($methods as $method) {
                            if ($method->getName() != '__construct') {
                                // 注解对象 设置注解路由相关信息
                                $annotation = new RequestMapping($reflect, $method);
    
                                // 解析 将上一步收集到的信息组装后添加到路由数组里面 $routes
                                (new RequestMappingParser())->parse($annotation);
                            }
                        }
                    }
                }
            }
        }
    }
    

      

    reload.php

    <?php
    
    
    namespace WangCoreReload;
    
    
    use WangCoreBeanBeanFactory;
    
    class Reload
    {
    
        protected $oldMd5 = '';
    
        public function __construct()
        {
            $dirs = BeanFactory::make("config")->get('reload_dirs');
            $this->oldMd5 = $this->md5Dirs($dirs);
        }
    
        /**
         * 是否需要重启
         * author: brady
         * date: 2020/7/23 16:42
         */
        public function needReload($newMd5)
        {
           if($newMd5 != $this->oldMd5){
               $this->oldMd5 = $newMd5;
               return true;
           } else {
               return false;
           }
        }
    
        /**
         * 对传入的目录所有文件进行递归加密
         * @param array $dirs
         * author: brady
         * date: 2020/7/23 16:42
         */
        public function md5Dirs($dirs)
        {
            $files = [];
            foreach($dirs as $dir){
                $files += get_files_by_tree($dir);
            }
    
            $md5File = '';
            foreach($files as $file){
                $md5File .= md5_file($file);
            }
    
            return  md5($md5File);
        }
    
    
    }
    

      

    每当代码改动 会重启服务 

    [2020-07-24 10:29:46 $2346.0]	INFO	Server is reloading all workers now
    worker 0 启动
    client connect fd:1
    client connect fd:1
    [2020-07-24 10:30:43 $2346.0]	INFO	Server is reloading all workers now
    worker 0 启动
    [2020-07-24 10:31:43 $2346.0]	INFO	Server is reloading all workers now
    worker 0 启动
    

      

    代码完整地址

    https://github.com/brady-wang/WangFrm.git

  • 相关阅读:
    Civil 3D 二次开发 创建Civil 3D 对象—— 01 —— 创建几何空间点
    Civil 3D 二次开发 创建Civil 3D 对象—— 00 ——
    Civil 3D 二次开发 创建AutoCAD对象—— 01 —— 创建直线
    Civil 3D 二次开发 新建CLR项目出现错误C2143
    Civil 3D 二次开发 创建AutoCAD对象—— 00 ——
    了解AutoCAD对象层次结构 —— 6 ——块表记录
    datepicker97使用
    使用angular 外接 templateUrl,使用ng-include
    angularJs 遮罩
    网上找的有关css兼容问题
  • 原文地址:https://www.cnblogs.com/brady-wang/p/13370615.html
Copyright © 2020-2023  润新知