Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
34.48% |
10 / 29 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
| RestartStuckTaskService | |
34.48% |
10 / 29 |
|
66.67% |
2 / 3 |
12.03 | |
0.00% |
0 / 1 |
| restart | |
29.63% |
8 / 27 |
|
0.00% |
0 / 1 |
6.14 | |||
| getTaskLog | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getQueueDispatcher | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 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) 2021 (original work) Open Assessment Technologies SA; |
| 19 | */ |
| 20 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\taoTaskQueue\model\Service; |
| 24 | |
| 25 | use InvalidArgumentException; |
| 26 | use oat\oatbox\service\ConfigurableService; |
| 27 | use oat\tao\model\taskQueue\QueueDispatcherInterface; |
| 28 | use oat\tao\model\taskQueue\TaskLogInterface; |
| 29 | use oat\taoTaskQueue\model\QueueBroker\RdsQueueBroker; |
| 30 | use oat\taoTaskQueue\model\StuckTask; |
| 31 | |
| 32 | class RestartStuckTaskService extends ConfigurableService |
| 33 | { |
| 34 | public function restart(StuckTask $stuckTask): void |
| 35 | { |
| 36 | $taskLogEntity = $stuckTask->getTaskLog(); |
| 37 | $broker = $this->getQueueDispatcher() |
| 38 | ->getQueue($stuckTask->getQueueName()) |
| 39 | ->getBroker(); |
| 40 | |
| 41 | if (!$broker instanceof RdsQueueBroker) { |
| 42 | throw new InvalidArgumentException( |
| 43 | sprintf( |
| 44 | 'Broker %s for queue %s is not supported. Supported only %s', |
| 45 | $broker->getBrokerId(), |
| 46 | $stuckTask->getQueueName(), |
| 47 | RdsQueueBroker::class |
| 48 | ) |
| 49 | ); |
| 50 | } |
| 51 | |
| 52 | if ($stuckTask->isOrphan()) { |
| 53 | $callback = $taskLogEntity->getTaskName(); |
| 54 | |
| 55 | $this->getTaskLog()->getBroker()->updateStatus( |
| 56 | $taskLogEntity->getId(), |
| 57 | TaskLogInterface::STATUS_CANCELLED |
| 58 | ); |
| 59 | |
| 60 | $this->getQueueDispatcher()->createTask( |
| 61 | new $callback(), |
| 62 | $taskLogEntity->getParameters(), |
| 63 | $taskLogEntity->getLabel() |
| 64 | ); |
| 65 | |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | $broker->changeTaskVisibility($stuckTask->getTaskId(), true); |
| 70 | |
| 71 | $this->getTaskLog()->setStatus($stuckTask->getTaskId(), TaskLogInterface::STATUS_ENQUEUED); |
| 72 | } |
| 73 | |
| 74 | private function getTaskLog(): TaskLogInterface |
| 75 | { |
| 76 | return $this->getServiceLocator()->get(TaskLogInterface::SERVICE_ID); |
| 77 | } |
| 78 | |
| 79 | private function getQueueDispatcher(): QueueDispatcherInterface |
| 80 | { |
| 81 | return $this->getServiceLocator()->get(QueueDispatcherInterface::SERVICE_ID); |
| 82 | } |
| 83 | } |