Acquires lock on this job.

@return bool Whether or not acquiring the lock succeeded.
555820
public function acquireLock() {
        $this->log("[JOB] attempting to acquire lock for job::{$this->job_id} on {$this->worker_name}", self::INFO);

        $lock = $this->runUpdate("
            UPDATE " . self::$jobsTable . "
            SET    locked_at = NOW(), locked_by = ?
            WHERE  id = ? AND (locked_at IS NULL OR locked_by = ?) AND failed_at IS NULL
        ", array($this->worker_name, $this->job_id, $this->worker_name));

        if (!$lock) {
            $this->log("[JOB] failed to acquire lock for job::{$this->job_id}", self::INFO);
            return false;
        }

        return true;
    }
676458
public function work()
    {
        if (!$this->connect()) {
            $this->logger()->alert(sprintf('Worker unable to connect, exiting'));
            $this->dispatchEvent('Worker.job.connectionFailed');

            return false;
        }

        $jobClass = $this->engine->getJobClass();
        $time = microtime(true);
        while ($this->running) {
            if (is_int($this->maxRuntime) && $this->runtime >= $this->maxRuntime) {
                $this->logger()->debug('Max runtime reached, exiting');
                $this->dispatchEvent('Worker.maxRuntime');
                break;
            } elseif (is_int($this->maxIterations) && $this->iterations >= $this->maxIterations) {
                $this->logger()->debug('Max iterations reached, exiting');
                $this->dispatchEvent('Worker.maxIterations');
                break;
            }

            $this->runtime += microtime(true) - $time;
            $time = microtime(true);
            $this->iterations++;
            $item = $this->engine->pop($this->queue);
            $this->dispatchEvent('Worker.job.seen', ['item' => $item]);
            if (empty($item)) {
                $this->dispatchEvent('Worker.job.empty');
                sleep($this->interval);
                continue;
            }

            $success = false;
            $job = new $jobClass($item, $this->engine);
            if (!is_callable($item['class'])) {
                $this->logger()->alert('Invalid callable for job. Rejecting job from queue.');
                $job->reject();
                $this->dispatchEvent('Worker.job.invalid', ['job' => $job]);
                continue;
            }

            $this->dispatchEvent('Worker.job.start', ['job' => $job]);

            try {
                $success = $this->perform($item, $job);
            } catch (Exception $e) {
                $this->dispatchEvent('Worker.job.exception', [
                    'job' => $job,
                    'exception' => $e,
                ]);
            }

            if ($success) {
                $job->acknowledge();
                $this->dispatchEvent('Worker.job.success', ['job' => $job]);
                continue;
            }

            $this->logger()->info('Failed. Releasing job to queue');
            $job->release();
            $this->dispatchEvent('Worker.job.failure', ['job' => $job]);
        }

        return true;
    }
142419
public function isUnlocked()
    {
        if (!is_null($this->unlockedAt)) {
            return true;
        }
        if ($this->points >= $this->details->points) {
            return true;
        }
        return false;
    }
1229493
public function is_locked() {
        if ($this->_locked) {
            return true;
        }

        if ($parent = $this->get_parent_context()) {
            return $parent->is_locked();
        }

        return false;
    }
1358880
protected function _release_lock()
    {
        if (!$this->_lock) {
            return true;
        }
        if ($this->getConnection()->query("SELECT RELEASE_LOCK('".$this->_lock."') AS ci_session_lock")->row()->ci_session_lock) {
            $this->_lock = false;
            return true;
        }
        return false;
    }
1327456
public function acquire($block = self::NON_BLOCKING)
    {
        if ($this->handler) {
            throw new \LogicException('Lock is already acquired');
        }
        // For flock() to work properly, the file must exist at the moment we do fopen()
        // So we create it first. touch()'s return value can be ignored, as the possible
        // error would be cought in the next step.
        touch($this->file);
        // The manuals usually recommend to use 'c' mode. But there can be a race condition.
        // If the file is deleted after touch() but before fopen(), it can be recreated and opened with flock()
        // by two processes simultaneously, and they both would be able to acquire an exclusive lock!
        // So when opening the file we MUST BE SURE it exists!
        // The 'r+' mode enables writing to an existing file, but it fails if the file does not exist.
        $this->handler = @fopen($this->file, 'r+');
        if (!$this->handler) {
            $this->throwLastErrorException();
        }
        $flag = LOCK_EX;
        if (!$block) {
            $flag |= LOCK_NB;
        }
        if (!flock($this->handler, $flag)) {
            $this->closeFile();
            return false;
        }
        if (@ftruncate($this->handler, 0) && @fwrite($this->handler, getmypid()) && @fflush($this->handler)) {
            return true;
        }
        $this->throwLastErrorException();
    }
367618
private function acquireLock($workerName, Job $job)
    {
        $affectedRows = $this->_em->getConnection()->executeUpdate(
            'UPDATE jms_jobs SET workerName = :worker WHERE id = :id AND workerName IS NULL',
            [
                'worker' => $workerName,
                'id' => $job->getId(),
            ]
        );

        if ($affectedRows > 0) {
            $job->setWorkerName($workerName);

            return true;
        }

        return false;
    }
67599
def release_lock(self):
    
    if not self._lock.acquired:
      return False
    else:
      self._lock.release()
      return True
405758
public function acquireLock($name, $timeout = null)
    {
        $blocking = $timeout === null;
        $start = microtime(true);
        $end = $start + $timeout / 1000;
        $locked = false;
        while (!(empty($this->locks[$name]) && $locked = $this->getLock($name, $blocking)) && ($blocking || ($timeout > 0 && microtime(true) < $end))) {
            usleep(static::USLEEP_TIME);
        }

        if ($locked) {
            $this->locks[$name] = true;

            return true;
        }

        return false;
    }
1056677
public function acquireLocks()
    {
        if (! isset($this->locks) && ! is_array($this->locks)) {
            $message = "Il ne semble pas y avoir de verrous définis pour ce process. Arrêt";
            self::getLogger()->err($message);
            self::alertError($message);
            $this->setAlive(false);
            return false;
        }
        foreach ($this->locks as $name => $mode) {
            self::getLogger()->info($this->translate("Trying to acquire the lock ") . $name . " " . $this->translate("in mode") . " : " . ($mode == LOCK_EX ? $this->translate('exclusive') : $this->translate('shared')));
            if (true === $this->lockFile($name, $mode))
                self::getLogger()->info($this->translate("Successfully acquired the lock ") . $name . " " . $this->translate("in mode") . " : " . ($mode == LOCK_EX ? $this->translate('exclusive') : $this->translate('shared')));
            else {
                $message = $this->translate("Unable to acquire the lock ") . $name . " " . $this->translate("in mode") . " : " . ($mode == LOCK_EX ? $this->translate('exclusive') : $this->translate('shared'));
                self::getLogger()->err($message);
                self::alertError($message);
                $this->setAlive(false);
                return false;
            }
        }
        return true;
    }
309180
public function pop() {
		$this->release_reserved();

		$sql = $this->database->prepare( "
			SELECT * FROM {$this->jobs_table}
			WHERE reserved_at IS NULL
			AND available_at <= %s
			ORDER BY available_at
			LIMIT 1
		", $this->datetime() );

		$raw_job = $this->database->get_row( $sql );

		if ( is_null( $raw_job ) ) {
			return false;
		}

		$job = $this->vitalize_job( $raw_job );

		$this->reserve( $job );

		return $job;
	}
71402
public function release_lock(lock $lock) {
        $handle = $lock->get_key();

        if (!$handle) {
            // We didn't have a lock.
            return false;
        }

        $result = flock($handle, LOCK_UN);
        fclose($handle);
        return $result;
    }
327633
@Override
  public void lock() throws JobLockException {
    try {
      this.lock.acquire();
    } catch (Exception e) {
      throw new JobLockException("Failed to acquire lock " + this.lockPath, e);
    }
  }
1339918
public function release()
    {
        if ($this->locked) {
            flock($this->fp, LOCK_UN);
            fclose($this->fp);

            $this->fp = null;
            $this->locked = false;
        }

        return $this;
    }
227917
protected function __lock()
    {
        $beginTime = microtime(true);
        do {
            if($this->__tryLock())
            {
                return true;
            }
            if(0 === $this->waitTimeout || microtime(true) - $beginTime < $this->waitTimeout / 1000)
            {
                if(Coroutine::isIn())
                {
                    Coroutine::sleep($this->waitSleepTime / 1000);
                }
                else
                {
                    usleep($this->waitSleepTime * 1000);
                }
            }
            else
            {
                break;
            }
        } while(true);
        return false;
    }
687353
public function unLock()
    {
        if (is_resource($this->handle)) {
            fflush($this->handle);
            flock($this->handle, LOCK_UN);
            fclose($this->handle);
        }

        $this->lockAcquired = false;
        $this->handle       = null;

        return $this;
    }
1110290
def acquire_lock
      return true if @lock_handle
      begin
        @lock_handle = File.open(lock_file_path, File::RDWR | File::CREAT)
        raise StandardError.new('Already locked') unless @lock_handle.flock(File::LOCK_EX | File::LOCK_NB)
        @lock_handle.rewind
        @lock_handle.truncate 0
        @stat_handle = File.open(status_file_path, File::RDWR | File::CREAT)
        raise StandardError.new('Failed to open status') unless @stat_handle
        @stat_handle.rewind
        @stat_handle.truncate 0
      rescue
        if @stat_handle
          @stat_handle.close rescue nil
        end
        if @lock_handle
          @lock_handle.flock(File::LOCK_UN) rescue nil
          @lock_handle.close rescue nil
        end
        @stat_handle = nil
        @lock_handle = nil
      end
      !!@lock_handle
    end