浏览代码

Avoid running multiple times

walkor 6 年之前
父节点
当前提交
9f36bb4cb7
共有 1 个文件被更改,包括 8 次插入0 次删除
  1. 8 0
      Worker.php

+ 8 - 0
Worker.php

@@ -1178,6 +1178,14 @@ class Worker
         if (static::$_OS !== OS_TYPE_LINUX) {
             return;
         }
+
+        clearstatcache();
+        $master_pid      = is_file(static::$pidFile) ? file_get_contents(static::$pidFile) : 0;
+        $master_is_alive = $master_pid && posix_kill($master_pid, 0) && posix_getpid() != $master_pid;
+        if ($master_is_alive) {
+            static::log("Workerman already running");
+            exit;
+        }
         static::$_masterPid = posix_getpid();
         if (false === file_put_contents(static::$pidFile, static::$_masterPid)) {
             throw new Exception('can not save pid to ' . static::$pidFile);