public function save(Queue $queue) { if ($queue->getId()) { $query = $this->dbAdapter->query('UPDATE `queues` SET `name` = :name WHERE `id = :id`'); $query->execute($queue->getArrayCopy()); } else { $query = $this->dbAdapter->query("INSERT INTO `queues` (`name`) VALUES (:name)"); $query->execute(['name' => $queue->getName()]); $queue->setId($this->dbAdapter->getDriver()->getLastGeneratedValue()); } return $queue; }
public function view() { $this->assertLoggedIn(); $this->set('area', 'queues'); try { //how do we find them? if ($this->args('id')) { $q = new Queue($this->args('id')); } else { throw new Exception("Could not find that queue."); } //did we really get someone? if (!$q->isHydrated()) { throw new Exception("Could not find that queue."); } if (!$q->isMine()) { throw new Exception("You do not have permission to view this queue."); } $this->setTitle("View Queue - " . $q->getName()); $this->set('queue', $q); $available = $q->getJobs('available', 'user_sort', 'ASC'); $this->set('available', $available->getRange(0, 20)); $this->set('available_count', $available->count()); $taken = $q->getJobs('taken', 'user_sort', 'DESC'); $this->set('taken', $taken->getRange(0, 20)); $this->set('taken_count', $taken->count()); $complete = $q->getJobs('complete', 'user_sort', 'DESC'); $this->set('complete', $complete->getRange(0, 20)); $this->set('complete_count', $complete->count()); $qa = $q->getJobs('qa', 'finished_time', 'DESC'); $this->set('qa', $qa->getRange(0, 20)); $this->set('qa_count', $qa->count()); $failure = $q->getJobs('failure', 'user_sort', 'DESC'); $this->set('failure', $failure->getRange(0, 20)); $this->set('failure_count', $failure->count()); $this->set('stats', QueueStats::getStats($q)); $bots = $q->getBots(); $this->set('bots', $bots->getRange(0, 20)); $this->set('bot_count', $bots->count()); $this->set('errors', $q->getErrorLog()->getRange(0, 50)); } catch (Exception $e) { $this->setTitle('View Queue - Error'); $this->set('megaerror', $e->getMessage()); } }
/** * Select unselected messages from queue * * @param Queue $queue * @param int $max * @param int $timeout * @throws Exception * @return \ArrayObject */ private function receiveQueueMessages(Queue $queue, $max, $timeout) { $messages = array(); $microtime = microtime(true); // cache microtime $db = $this->getDb(); $qid = $this->getQueueId($queue->getName()); // start transaction handling try { if ($max > 0) { $db->beginTransaction(); $sql = "SELECT *\n FROM message\n WHERE queue_id = :queue_id\n AND (handle IS NULL OR timeout+" . (int) $timeout . " < " . (int) $microtime . ")\n LIMIT " . $max; $stmt = $db->prepare($sql); $stmt->execute(array('queue_id' => $qid)); foreach ($stmt->fetchAll() as $data) { $data['handle'] = md5(uniqid(rand(), true)); $sql = "UPDATE message\n SET\n handle = :handle,\n timeout = :timeout\n WHERE\n message_id = :id\n AND (handle IS NULL OR timeout+" . (int) $timeout . " < " . (int) $microtime . ")"; $stmt = $db->prepare($sql); $stmt->bindParam(':handle', $data['handle'], \PDO::PARAM_STR); $stmt->bindParam(':id', $data['message_id'], \PDO::PARAM_STR); $stmt->bindValue(':timeout', $microtime); $updated = $stmt->execute(); if ($updated) { $messages[] = $data; } } $db->commit(); } } catch (\Exception $e) { $db->rollBack(); throw $e; } $m = array(); foreach ($messages as $msg) { $message = unserialize(base64_decode($msg['body'])); if ($message instanceof Message) { $message->message_id = $msg['message_id']; $m[] = $message; } } return new \ArrayObject($m); }
public function testGetQueueName() { $queueName = 'testQueueName-cz:xy'; $queue = new Queue($this->redis, $queueName); $this->assertSame($queueName, $queue->getName()); }
/** * Pops an element form the queue and atomically pushes into another queue * Blocks $timeout if there is no element to pop. * * @param Queue $target Popped element is pushed to this queue * @param int $timeout After $timeout seconds null is returned if queue is empty * * @return mixed element being popped and pushed, null on timeout * * @link http://redis.io/commands/brpoplpush */ public function blockingDequeueEnqueue(Queue $target, $timeout) { return $this->client->brpoplpush($this->getName(), $target->getName(), $timeout); }