|
@@ -498,11 +498,13 @@ class Worker
|
|
|
{
|
|
{
|
|
|
static::checkSapiEnv();
|
|
static::checkSapiEnv();
|
|
|
static::init();
|
|
static::init();
|
|
|
|
|
+ static::lock();
|
|
|
static::parseCommand();
|
|
static::parseCommand();
|
|
|
static::daemonize();
|
|
static::daemonize();
|
|
|
static::initWorkers();
|
|
static::initWorkers();
|
|
|
static::installSignal();
|
|
static::installSignal();
|
|
|
static::saveMasterPid();
|
|
static::saveMasterPid();
|
|
|
|
|
+ static::unlock();
|
|
|
static::displayUI();
|
|
static::displayUI();
|
|
|
static::forkWorkers();
|
|
static::forkWorkers();
|
|
|
static::resetStd();
|
|
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.
|
|
* Init All worker instances.
|
|
|
*
|
|
*
|
|
|
* @return void
|
|
* @return void
|
|
@@ -1179,13 +1206,6 @@ class Worker
|
|
|
return;
|
|
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();
|
|
static::$_masterPid = posix_getpid();
|
|
|
if (false === file_put_contents(static::$pidFile, static::$_masterPid)) {
|
|
if (false === file_put_contents(static::$pidFile, static::$_masterPid)) {
|
|
|
throw new Exception('can not save pid to ' . static::$pidFile);
|
|
throw new Exception('can not save pid to ' . static::$pidFile);
|