Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 48 |
|
0.00% |
0 / 15 |
CRAP | |
0.00% |
0 / 1 |
| Queue | |
0.00% |
0 / 48 |
|
0.00% |
0 / 15 |
506 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
20 | |||
| __toString | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| __toPhpCode | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 | |||
| initialize | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setWeight | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| getWeight | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setBroker | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| getBroker | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| enqueue | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
20 | |||
| dequeue | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| acknowledge | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| count | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| isSync | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getNumberOfTasksToReceive | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 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; |
| 23 | |
| 24 | use oat\oatbox\log\LoggerAwareTrait; |
| 25 | use oat\tao\model\taskQueue\Queue\Broker\QueueBrokerInterface; |
| 26 | use oat\taoTaskQueue\model\QueueBroker\SyncQueueBrokerInterface; |
| 27 | use oat\taoTaskQueue\model\Task\TaskInterface; |
| 28 | use Zend\ServiceManager\ServiceLocatorAwareInterface; |
| 29 | use Zend\ServiceManager\ServiceLocatorAwareTrait; |
| 30 | |
| 31 | /** |
| 32 | * Queue Service |
| 33 | * |
| 34 | * @deprecated Use \oat\tao\model\taskQueue\Queue |
| 35 | * |
| 36 | * @author Gyula Szucs <gyula@taotesting.com> |
| 37 | */ |
| 38 | class Queue implements QueueInterface, TaskLogAwareInterface |
| 39 | { |
| 40 | use LoggerAwareTrait; |
| 41 | use ServiceLocatorAwareTrait; |
| 42 | use TaskLogAwareTrait; |
| 43 | |
| 44 | private $name; |
| 45 | |
| 46 | /** |
| 47 | * @var QueueBrokerInterface|ServiceLocatorAwareInterface |
| 48 | */ |
| 49 | private $broker; |
| 50 | |
| 51 | /** |
| 52 | * @var int |
| 53 | */ |
| 54 | private $weight; |
| 55 | |
| 56 | /** |
| 57 | * Queue constructor. |
| 58 | * |
| 59 | * @param string $name |
| 60 | * @param QueueBrokerInterface|null $broker Null option will be removed in version 1.0.0 |
| 61 | * @param int $weight |
| 62 | */ |
| 63 | public function __construct($name, QueueBrokerInterface $broker = null, $weight = 1) |
| 64 | { |
| 65 | /** |
| 66 | * this "if case" is because of backwards compatibility, will be removed in version 1.0.0 |
| 67 | * |
| 68 | * @deprecated |
| 69 | */ |
| 70 | if (is_array($name)) { |
| 71 | $oldConfig = $name; |
| 72 | $name = $oldConfig['queue_name']; |
| 73 | $broker = $oldConfig['queue_broker']; |
| 74 | } |
| 75 | |
| 76 | if (empty($name)) { |
| 77 | throw new \InvalidArgumentException("Queue name needs to be set."); |
| 78 | } |
| 79 | |
| 80 | if (!$broker instanceof QueueBrokerInterface) { |
| 81 | throw new \InvalidArgumentException("Queue Broker needs to be an instance of QueueBrokerInterface."); |
| 82 | } |
| 83 | |
| 84 | $this->name = $name; |
| 85 | $this->setWeight($weight); |
| 86 | |
| 87 | $this->setBroker($broker); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * @inheritdoc |
| 92 | */ |
| 93 | public function __toString() |
| 94 | { |
| 95 | return $this->getName(); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * @inheritdoc |
| 100 | */ |
| 101 | public function __toPhpCode() |
| 102 | { |
| 103 | return 'new ' . get_called_class() . '(' |
| 104 | . \common_Utils::toHumanReadablePhpString($this->getName()) |
| 105 | . ', ' |
| 106 | . \common_Utils::toHumanReadablePhpString($this->getBroker()) |
| 107 | . ', ' |
| 108 | . \common_Utils::toHumanReadablePhpString($this->getWeight()) |
| 109 | . ')'; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * @inheritdoc |
| 114 | */ |
| 115 | public function initialize() |
| 116 | { |
| 117 | $this->getBroker()->createQueue(); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * @inheritdoc |
| 122 | */ |
| 123 | public function getName() |
| 124 | { |
| 125 | return $this->name; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * @param int $weight |
| 130 | * @return Queue |
| 131 | */ |
| 132 | public function setWeight($weight) |
| 133 | { |
| 134 | $this->weight = abs($weight); |
| 135 | |
| 136 | return $this; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * @inheritdoc |
| 141 | */ |
| 142 | public function getWeight() |
| 143 | { |
| 144 | return $this->weight; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * @inheritdoc |
| 149 | */ |
| 150 | public function setBroker(QueueBrokerInterface $broker) |
| 151 | { |
| 152 | $this->broker = $broker; |
| 153 | |
| 154 | $this->broker->setQueueName($this->getName()); |
| 155 | |
| 156 | return $this; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Returns the queue broker service. |
| 161 | * |
| 162 | * @return QueueBrokerInterface |
| 163 | */ |
| 164 | public function getBroker() |
| 165 | { |
| 166 | $this->broker->setServiceLocator($this->getServiceLocator()); |
| 167 | |
| 168 | return $this->broker; |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * @inheritdoc |
| 173 | */ |
| 174 | public function enqueue(TaskInterface $task, $label = null) |
| 175 | { |
| 176 | try { |
| 177 | if (!is_null($label)) { |
| 178 | $task->setLabel($label); |
| 179 | } |
| 180 | |
| 181 | $isEnqueued = $this->getBroker()->push($task); |
| 182 | |
| 183 | if ($isEnqueued) { |
| 184 | $this->getTaskLog() |
| 185 | ->add($task, TaskLogInterface::STATUS_ENQUEUED, $label); |
| 186 | } |
| 187 | |
| 188 | return $isEnqueued; |
| 189 | } catch (\Exception $e) { |
| 190 | $this->logError('Enqueueing ' . $task . ' failed with MSG: ' . $e->getMessage()); |
| 191 | } |
| 192 | |
| 193 | return false; |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * @inheritdoc |
| 198 | */ |
| 199 | public function dequeue() |
| 200 | { |
| 201 | if ($task = $this->getBroker()->pop()) { |
| 202 | $this->getTaskLog() |
| 203 | ->setStatus($task->getId(), TaskLogInterface::STATUS_DEQUEUED); |
| 204 | |
| 205 | return $task; |
| 206 | } |
| 207 | |
| 208 | return null; |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * @inheritdoc |
| 213 | */ |
| 214 | public function acknowledge(TaskInterface $task) |
| 215 | { |
| 216 | $this->getBroker()->delete($task); |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Count of messages in the queue. |
| 221 | * |
| 222 | * @return int |
| 223 | */ |
| 224 | public function count() |
| 225 | { |
| 226 | return $this->getBroker()->count(); |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * @return bool |
| 231 | */ |
| 232 | public function isSync() |
| 233 | { |
| 234 | return $this->broker instanceof SyncQueueBrokerInterface; |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * @inheritdoc |
| 239 | */ |
| 240 | public function getNumberOfTasksToReceive() |
| 241 | { |
| 242 | return $this->getBroker()->getNumberOfTasksToReceive(); |
| 243 | } |
| 244 | } |