/** * @param string $lockName * @param string $method * @param int $timeout * @return bool */ public function lock($lockName, $method, $timeout = 5) { $lockName = $this->addQuotes($this->makeLockName($lockName)); $result = $this->query("SELECT GET_LOCK({$lockName}, {$timeout}) AS lockstatus", $method); $row = $this->fetchObject($result); if ($row->lockstatus == 1) { parent::lock($lockName, $method, $timeout); // record return true; } wfDebug(__METHOD__ . " failed to acquire lock\n"); return false; }
/** * See http://www.postgresql.org/docs/8.2/static/functions-admin.html#FUNCTIONS-ADVISORY-LOCKS * @param string $lockName * @param string $method * @param int $timeout * @return bool */ public function lock($lockName, $method, $timeout = 5) { $key = $this->addQuotes($this->bigintFromLockName($lockName)); for ($attempts = 1; $attempts <= $timeout; ++$attempts) { $result = $this->query("SELECT pg_try_advisory_lock({$key}) AS lockstatus", $method); $row = $this->fetchObject($result); if ($row->lockstatus === 't') { parent::lock($lockName, $method, $timeout); // record return true; } else { sleep(1); } } wfDebug(__METHOD__ . " failed to acquire lock\n"); return false; }
public function lock($lockName, $method, $timeout = 5) { // http://www.postgresql.org/docs/8.2/static/functions-admin.html#FUNCTIONS-ADVISORY-LOCKS $key = $this->addQuotes($this->bigintFromLockName($lockName)); $loop = new WaitConditionLoop(function () use($lockName, $key, $timeout, $method) { $res = $this->query("SELECT pg_try_advisory_lock({$key}) AS lockstatus", $method); $row = $this->fetchObject($res); if ($row->lockstatus === 't') { parent::lock($lockName, $method, $timeout); // record return true; } return WaitConditionLoop::CONDITION_CONTINUE; }, $timeout); return $loop->invoke() === $loop::CONDITION_REACHED; }