Explorar el Código

Add file lock for avoid running multiple times

close #400
walkor hace 6 años
padre
commit
c141341367
Se han modificado 1 ficheros con 27 adiciones y 7 borrados
  1. 27 7
      Worker.php

+ 27 - 7
Worker.php

@@ -498,11 +498,13 @@ class Worker
     {
         static::checkSapiEnv();
         static::init();
+        static::lock();
         static::parseCommand();
         static::daemonize();
         static::initWorkers();
         static::installSignal();
         static::saveMasterPid();
+        static::unlock();
         static::displayUI();
         static::forkWorkers();
         static::resetStd();
@@ -576,6 +578,31 @@ class Worker
     }
 
     /**
+     * Lock.
+     *
+     * @return void
+     */
+    protected static function lock()
+    {
+        $fd = fopen(static::$_startFile, 'r');
+        if (!$fd || !flock($fd, LOCK_EX)) {
+            static::log("Workerman[".static::$_startFile."] already running");
+            exit;
+        }
+    }
+
+    /**
+     * Unlock.
+     *
+     * @return void
+     */
+    protected static function unlock()
+    {
+        $fd = fopen(static::$_startFile, 'r');
+        $fd && flock($fd, LOCK_UN);
+    }
+
+    /**
      * Init All worker instances.
      *
      * @return void
@@ -1179,13 +1206,6 @@ class Worker
             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);