[已解决] MacOS本地开发模式下Monitor文件监控服务异常

小天天天天

问题描述

我本地新安装的webman,启动后热更新过一会儿就不能用了(修改代码不生效)

控制台日志如下:
➜ webman git:(main) ✗ php start.php start
Workerman[start.php] start in DEBUG mode
-------------------------------------------- WORKERMAN ---------------------------------------------
Workerman/5.1.1 PHP/8.2.28 (Jit off) Darwin/24.4.0
--------------------------------------------- WORKERS ----------------------------------------------
event-loop proto user worker listen count state
event tcp lijian main http://0.0.0.0:8787 14 [OK]
event tcp lijian monitor none 1 [OK]

Press Ctrl+C to stop. Start success.
/Users/lijian/Project/webman/app/controller/IndexController.php updated and reload---------7337
Workerman[start.php] reloading
/Users/lijian/Project/webman/app/controller/IndexController.php updated and reload---------7337
Workerman[start.php] reloading
/Users/lijian/Project/webman/app/controller/IndexController.php updated and reload---------7337
Workerman[start.php] reloading
/Users/lijian/Project/webman/app/controller/IndexController.php updated and reload---------7337
Workerman[start.php] reloading
/Users/lijian/Project/webman/app/controller/IndexController.php updated and reload---------7337
Workerman[start.php] reloading
/Users/lijian/Project/webman/app/controller/IndexController.php updated and reload---------7337
Workerman[start.php] reloading
/Users/lijian/Project/webman/app/controller/IndexController.php updated and reload---------0
/Users/lijian/Project/webman/app/controller/IndexController.php updated and reload---------0

其中7337是文件app/process/Monitor.php中的$this->ppid
奇怪的是当使用 php start.php start 启动后,最开始的几次修改都会触发Workerman[start.php] reloading,但是过一会儿$this->ppid就会=0,修改代码以后不再触发Workerman[start.php] reloading,访问接口响应是正常(响应修改前的内容),没有报错.

我尝试再使用 php start.php start 启动后出现$this->ppid=0以后,再新终端执行ps -ef | grep webman,发现
501 7337 98840 0 6:42PM ttys000 0:00.05 WorkerMan: master process start_file=/Users/lijian/Project/webman/start.php

说明主进程是正常的,不知为何app/process/Monitor.php中的$this->ppid会=0,请各位大佬指点一下

操作系统环境及workerman/webman等具体版本

macos M4pro芯片+ "workerman/webman-framework": "^2.1.2",

补充

我在控制器中写了如下代码监控ppid
echo "PID: " . posix_getpid() . ", PPID: " . posix_getppid() . "\n";

当修改代码后提示$this->ppid=0时,我请求接口,控制台输出如下:
PID: 10945, PPID: 10943

再补充

在执行检查内存的定时器时

Timer::add(60, [$this, 'checkMemory'], [$memoryLimit]);

最后定位问题在getMasterPid这个方法
/proc/*是linux下的路径,macos不支持

    public function getMasterPid(): int
    {
        if ($this->ppid === 0) {
            return 0;
        }
        $cmdline = "/proc/$this->ppid/cmdline";
        if (!is_readable($cmdline) || !($content = file_get_contents($cmdline)) || (!str_contains($content, 'WorkerMan') && !str_contains($content, 'php'))) {
            // Process not exist
            echo "MasterPid: $this->ppid not exist\n";
            $this->ppid = 0;
        }
        return $this->ppid;
    }

临时解决

修改

    public function getMasterPid(): int
    {
        $pid_path = config('server.pid_file');
        $this->ppid = file_get_contents($pid_path);
        if (intval($this->ppid) === 0) {
            return 0;
        }
        return $this->ppid;
    }

作者已经解决了

https://www.workerman.net/q/14169

241 0 1
0个回答

🔝