Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 12 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| WeightStrategy | |
0.00% |
0 / 12 |
|
0.00% |
0 / 3 |
30 | |
0.00% |
0 / 1 |
| pickNextTask | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| getWaitTime | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| pickQueueByWeight | |
0.00% |
0 / 8 |
|
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\tao\model\taskQueue\Queue\TaskSelector; |
| 23 | |
| 24 | use oat\oatbox\log\LoggerAwareTrait; |
| 25 | use oat\oatbox\PhpSerializeStateless; |
| 26 | use oat\tao\model\taskQueue\QueueInterface; |
| 27 | use Psr\Log\LoggerAwareInterface; |
| 28 | |
| 29 | /** |
| 30 | * Implements a strategy for selecting a queue randomly taking into account the known weights. |
| 31 | * |
| 32 | * @author Gyula Szucs <gyula@taotesting.com> |
| 33 | */ |
| 34 | class WeightStrategy implements SelectorStrategyInterface, LoggerAwareInterface |
| 35 | { |
| 36 | use LoggerAwareTrait; |
| 37 | use PhpSerializeStateless; |
| 38 | |
| 39 | /** |
| 40 | * @inheritdoc |
| 41 | */ |
| 42 | public function pickNextTask(array $queues) |
| 43 | { |
| 44 | $pickedQueue = $this->pickQueueByWeight($queues); |
| 45 | |
| 46 | $this->logDebug('Queue "' . strtoupper($pickedQueue->getName()) . '" picked by WeightStrategy'); |
| 47 | |
| 48 | return $pickedQueue->dequeue(); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * @return int |
| 53 | */ |
| 54 | public function getWaitTime() |
| 55 | { |
| 56 | return 1; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Picks randomly a queue based on weight. |
| 61 | * |
| 62 | * For example, an array like ['A'=>5, 'B'=>45, 'C'=>50] means that "A" has a 5% chance of being selected, "B" 45%, |
| 63 | * and "C" 50%. |
| 64 | * The values are simply relative to each other. If one value weight was 2, and the other weight of 1, |
| 65 | * the value with the weight of 2 has about a 66% chance of being selected. |
| 66 | * |
| 67 | * @inheritdoc |
| 68 | */ |
| 69 | private function pickQueueByWeight(array $queues) |
| 70 | { |
| 71 | $weights = array_map(function (QueueInterface $queue) { |
| 72 | return $queue->getWeight(); |
| 73 | }, $queues); |
| 74 | |
| 75 | $rand = mt_rand(1, array_sum($weights)); |
| 76 | |
| 77 | /** @var QueueInterface $queue */ |
| 78 | foreach ($queues as $queue) { |
| 79 | $rand -= $queue->getWeight(); |
| 80 | if ($rand <= 0) { |
| 81 | return $queue; |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | } |