*/ class StrictPriorityStrategy implements SelectorStrategyInterface, LoggerAwareInterface { use LoggerAwareTrait; use PhpSerializeStateless; /** * @var QueueInterface[] */ private $sortedQueues = []; private $nextQueueIndex = 0; /** * @inheritdoc */ public function pickNextTask(array $queues) { $this->sortQueues($queues); // if the next index is bigger than the max index if ($this->nextQueueIndex > count($this->sortedQueues) - 1) { $this->nextQueueIndex = 0; } $pickedQueue = $this->sortedQueues[$this->nextQueueIndex]; $this->logDebug('Queue "' . strtoupper($pickedQueue->getName()) . '" picked by StrictPriorityStrategy'); $task = $pickedQueue->dequeue(); if (is_null($task)) { // let's use the next queue in the next iteration $this->nextQueueIndex++; } else { // always start with the first queue after having a task from any queue $this->nextQueueIndex = 0; } return $task; } /** * @return int */ public function getWaitTime() { // sleeping 5 sec only after a complete iteration (every queue has been selected once in a row), otherwise 0 sec return $this->nextQueueIndex - 1 == count($this->sortedQueues) - 1 ? 5 : 0; } /** * @param QueueInterface[] $queues */ private function sortQueues(array $queues) { usort($queues, function (QueueInterface $queueA, QueueInterface $queueB) { if ($queueA->getWeight() == $queueB->getWeight()) { return 0; } return ($queueB->getWeight() < $queueA->getWeight()) ? -1 : 1; }); $this->sortedQueues = $queues; } }