/** * Get a job from queue. * This call is blocking. * * @param string $key * @param int $timeout * * @return string|null */ public function get($key, $timeout = 30) { if ($this->checkConnection()) { $return = $this->predis->blpop(array(self::QUEUE_PREFIX . ':' . QueuePriority::PRIORITY_HIGH . ':' . $key, self::QUEUE_PREFIX . ':' . QueuePriority::PRIORITY_NORMAL . ':' . $key, self::QUEUE_PREFIX . ':' . QueuePriority::PRIORITY_LOW . ':' . $key), $timeout); if (is_array($return)) { return $return[1]; } } return null; }
/** * @throws QueueException * @throws \Exception * @return Task */ public function waitToAssignTask() { $this->setActive(); // A nil multi-bulk when no element could be popped and the timeout expired. // A two-element multi-bulk with the first element being the name of the key // where an element was popped and the second element being the value of // the popped element. $redisData = $this->redisClient->blpop($this->announceListKey, 5); //Pop timed out rather than got a task if ($redisData === null) { return null; } list(, $taskKey) = $redisData; $serializedTask = $this->redisClient->get($this->taskListKey . $taskKey); if (!$serializedTask) { $data = var_export($serializedTask, true); throw new \Exception("Failed to find expected task " . $taskKey . ". Data returned was " . $data); } $task = @unserialize($serializedTask); if ($task == false) { $this->setStatus($taskKey, TaskQueue::STATE_ERROR); throw new QueueException("Failed to unserialize string {$serializedTask}"); } $this->setStatus($task, TaskQueue::STATE_WORKING); return $task; }
public function receiveMessage($queueId = null, $waitTime = 0) { $queueId = $this->normaliseQueueId($queueId); if (empty($waitTime)) { $waitTime = $this->waitTime; } $message = $this->predis->blpop([$queueId], $waitTime); if (empty($message[1])) { return null; } /** @var QueueMessage $queueMessage */ $queueMessage = $this->messageFactory->createMessage($message[1], $queueId); $index = $this->receivedMessageCounter++; $this->receivedMessages[$index] = $queueMessage; $queueMessage->setReceiptId($index); return $queueMessage; }
/** * @param string $commandIdentifier * @param int $timeout * @return array */ public function readCommandReply($commandIdentifier, $timeout = null) { $timeout = $timeout ? $timeout : $this->timeout; return $this->client->blpop([sprintf(self::COMMAND_RESPONSE_KEY, $commandIdentifier)], $timeout); }