Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 17 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| StrictPriorityStrategy | |
0.00% |
0 / 17 |
|
0.00% |
0 / 3 |
72 | |
0.00% |
0 / 1 |
| pickNextTask | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
12 | |||
| getWaitTime | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
| sortQueues | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2017 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\taoTaskQueue\model\TaskSelector; |
| 23 | |
| 24 | use oat\oatbox\log\LoggerAwareTrait; |
| 25 | use oat\oatbox\PhpSerializeStateless; |
| 26 | use oat\tao\model\taskQueue\Queue\TaskSelector\SelectorStrategyInterface; |
| 27 | use oat\tao\model\taskQueue\QueueInterface; |
| 28 | use Psr\Log\LoggerAwareInterface; |
| 29 | |
| 30 | /** |
| 31 | * Walking through all the queues having respect for the priorities. |
| 32 | * |
| 33 | * 1: generating a list of queues in the order of weights |
| 34 | * 2: get a queue by the current index (starting with the highest priority queue always) |
| 35 | * 3a: IF there is a task in the current queue, let's process it and restart form point 2 |
| 36 | * 3b: IF there is no task go to the next queue and repeat from point 2 |
| 37 | * 3c: IF there is no task and we have a complete iteration, sleep a while and repeat from point 2 |
| 38 | * |
| 39 | * @author Gyula Szucs <gyula@taotesting.com> |
| 40 | */ |
| 41 | class StrictPriorityStrategy implements SelectorStrategyInterface, LoggerAwareInterface |
| 42 | { |
| 43 | use LoggerAwareTrait; |
| 44 | use PhpSerializeStateless; |
| 45 | |
| 46 | /** |
| 47 | * @var QueueInterface[] |
| 48 | */ |
| 49 | private $sortedQueues = []; |
| 50 | |
| 51 | private $nextQueueIndex = 0; |
| 52 | |
| 53 | /** |
| 54 | * @inheritdoc |
| 55 | */ |
| 56 | public function pickNextTask(array $queues) |
| 57 | { |
| 58 | $this->sortQueues($queues); |
| 59 | |
| 60 | // if the next index is bigger than the max index |
| 61 | if ($this->nextQueueIndex > count($this->sortedQueues) - 1) { |
| 62 | $this->nextQueueIndex = 0; |
| 63 | } |
| 64 | |
| 65 | $pickedQueue = $this->sortedQueues[$this->nextQueueIndex]; |
| 66 | |
| 67 | $this->logDebug('Queue "' . strtoupper($pickedQueue->getName()) . '" picked by StrictPriorityStrategy'); |
| 68 | |
| 69 | $task = $pickedQueue->dequeue(); |
| 70 | |
| 71 | if (is_null($task)) { |
| 72 | // let's use the next queue in the next iteration |
| 73 | $this->nextQueueIndex++; |
| 74 | } else { |
| 75 | // always start with the first queue after having a task from any queue |
| 76 | $this->nextQueueIndex = 0; |
| 77 | } |
| 78 | |
| 79 | return $task; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * @return int |
| 84 | */ |
| 85 | public function getWaitTime() |
| 86 | { |
| 87 | // sleeping 5 sec only after a complete iteration (every queue has been selected once in a row), otherwise 0 sec |
| 88 | return $this->nextQueueIndex - 1 == count($this->sortedQueues) - 1 ? 5 : 0; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * @param QueueInterface[] $queues |
| 93 | */ |
| 94 | private function sortQueues(array $queues) |
| 95 | { |
| 96 | usort($queues, function (QueueInterface $queueA, QueueInterface $queueB) { |
| 97 | if ($queueA->getWeight() == $queueB->getWeight()) { |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | return ($queueB->getWeight() < $queueA->getWeight()) ? -1 : 1; |
| 102 | }); |
| 103 | |
| 104 | $this->sortedQueues = $queues; |
| 105 | } |
| 106 | } |