pinguo / php-msf

PHP微服务框架即Micro Service Framework For PHP
GNU General Public License v2.0
1.75k stars 317 forks source link

worker Reload时增加自定义回调机制 #120

Closed wuchuguang closed 6 years ago

wuchuguang commented 6 years ago
swoole_event_add($this->inotifyFd, function ($inotifyFd) use (&$monitorFiles) {
            $events = inotify_read($inotifyFd);
            $flag = true;
            foreach ($events as $ev) {
                if (pathinfo($ev['name'], PATHINFO_EXTENSION) != 'php') {
                    //创建目录添加监听
                    if ($ev['mask'] == 1073742080) {
                        $path = $monitorFiles[$ev['wd']] .'/'. $ev['name'];

                        $wd = inotify_add_watch($inotifyFd, $path, IN_MODIFY | IN_CREATE | IN_IGNORED | IN_DELETE);
                        $monitorFiles[$wd] = $path;
                    }
                    $flag = false;
                    continue;
                }
                writeln('RELOAD ' . $monitorFiles[$ev['wd']] .'/'. $ev['name'] . ' update');
            }
            if ($flag == true) {
                $this->MSFServer->server->reload();
            }
        }, null, SWOOLE_EVENT_READ);

改为

swoole_event_add($this->inotifyFd, function ($inotifyFd) use (&$monitorFiles) {
            $events = inotify_read($inotifyFd);
            $flag = true;
            $changes = [];//record change
            foreach ($events as $ev) {
                if (pathinfo($ev['name'], PATHINFO_EXTENSION) != 'php') {
                    //创建目录添加监听
                    if ($ev['mask'] == 1073742080) {
                        $path = $monitorFiles[$ev['wd']] .'/'. $ev['name'];

                        $wd = inotify_add_watch($inotifyFd, $path, IN_MODIFY | IN_CREATE | IN_IGNORED | IN_DELETE);
                        $monitorFiles[$wd] = $path;
                    }
                    $flag = false;
                    continue;
                }
                writeln('RELOAD ' . $monitorFiles[$ev['wd']] .'/'. $ev['name'] . ' update');
                $changes[$monitorFiles[$ev['wd']] .'/'. $ev['name']] = 'update';
            }
            if ($flag == true) {
                $callback = $this->config->get('reload.callback');//reload callback
                if($callback && is_callable($callback)){
                    $callback($changes);//call back
                }
                $this->MSFServer->server->reload();
            }
        }, null, SWOOLE_EVENT_READ);
shellvon commented 6 years ago

每次reload的时候worker收到信号后都会主动回调onWorkerStop然后自杀。 启动的时候会触发回调onWorkerStart,你要做什么可以在自己的appServer中做。就像默认的Server会尝试清空apc缓存: https://github.com/pinguo/php-msf/blob/d5b77dd3bbc0285792c7bd22a5edf271de53d650/src/Server.php#L557-L566 需要注意的是这里是每一个worker重启时都会执行一次。

viaweb3 commented 6 years ago

嗯 建议在自己的AppServer里面实现这个逻辑,不应该在inotify这里实现。