Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
76.92% |
30 / 39 |
|
75.00% |
3 / 4 |
CRAP | |
0.00% |
0 / 1 |
| StuckTaskRepository | |
76.92% |
30 / 39 |
|
75.00% |
3 / 4 |
10.00 | |
0.00% |
0 / 1 |
| findAll | |
75.00% |
27 / 36 |
|
0.00% |
0 / 1 |
6.56 | |||
| isAgeConsistent | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 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\Repository; |
| 24 | |
| 25 | use InvalidArgumentException; |
| 26 | use oat\oatbox\service\ConfigurableService; |
| 27 | use oat\tao\model\taskQueue\QueueDispatcherInterface; |
| 28 | use oat\tao\model\taskQueue\TaskLog\Broker\TaskLogBrokerInterface; |
| 29 | use oat\tao\model\taskQueue\TaskLog\Entity\EntityInterface; |
| 30 | use oat\tao\model\taskQueue\TaskLog\TaskLogFilter; |
| 31 | use oat\tao\model\taskQueue\TaskLogInterface; |
| 32 | use oat\taoTaskQueue\model\QueueBroker\RdsQueueBroker; |
| 33 | use oat\taoTaskQueue\model\StuckTask; |
| 34 | |
| 35 | class StuckTaskRepository extends ConfigurableService |
| 36 | { |
| 37 | public const MIN_AGE = 180; |
| 38 | |
| 39 | public function findAll(StuckTaskQuery $query): StuckTaskCollection |
| 40 | { |
| 41 | $taskLog = $this->getTaskLog(); |
| 42 | $broker = $this->getQueueDispatcher() |
| 43 | ->getQueue($query->getQueryName()) |
| 44 | ->getBroker(); |
| 45 | |
| 46 | if (!$broker instanceof RdsQueueBroker) { |
| 47 | throw new InvalidArgumentException( |
| 48 | sprintf( |
| 49 | 'Broker %s for queue %s is not supported. Supported only %s', |
| 50 | $broker->getBrokerId(), |
| 51 | $query->getQueryName(), |
| 52 | RdsQueueBroker::class |
| 53 | ) |
| 54 | ); |
| 55 | } |
| 56 | |
| 57 | $filter = (new TaskLogFilter()) |
| 58 | ->addFilter(TaskLogBrokerInterface::COLUMN_TASK_NAME, 'IN', $query->getWhitelist()) |
| 59 | ->addFilter(TaskLogBrokerInterface::COLUMN_STATUS, 'IN', $query->getStatuses()) |
| 60 | ->addFilter( |
| 61 | TaskLogBrokerInterface::COLUMN_UPDATED_AT, |
| 62 | '<=', |
| 63 | $query->getAgeDateTime()->format('Y-m-d H:i:s') |
| 64 | ); |
| 65 | |
| 66 | $taskLogs = $taskLog->search($filter); |
| 67 | |
| 68 | $tasks = new StuckTaskCollection(...[]); |
| 69 | |
| 70 | foreach ($taskLogs as $taskLogEntity) { |
| 71 | if (!$this->isAgeConsistent($taskLogEntity, $query)) { |
| 72 | continue; |
| 73 | } |
| 74 | |
| 75 | $task = $broker->getTaskByTaskLogId($taskLogEntity->getId()); |
| 76 | |
| 77 | $tasks->add( |
| 78 | new StuckTask( |
| 79 | $taskLogEntity, |
| 80 | $query->getQueryName(), |
| 81 | $task ? $task->getTask() : null, |
| 82 | $task ? $task->getTaskId() : null |
| 83 | ) |
| 84 | ); |
| 85 | } |
| 86 | |
| 87 | return $tasks; |
| 88 | } |
| 89 | |
| 90 | private function isAgeConsistent(EntityInterface $taskLogEntity, StuckTaskQuery $query): bool |
| 91 | { |
| 92 | return ($query->getAgeDateTime()->getTimestamp() - $taskLogEntity->getCreatedAt()->getTimestamp()) >= 0; |
| 93 | } |
| 94 | |
| 95 | private function getTaskLog(): TaskLogInterface |
| 96 | { |
| 97 | return $this->getServiceLocator()->get(TaskLogInterface::SERVICE_ID); |
| 98 | } |
| 99 | |
| 100 | private function getQueueDispatcher(): QueueDispatcherInterface |
| 101 | { |
| 102 | return $this->getServiceLocator()->get(QueueDispatcherInterface::SERVICE_ID); |
| 103 | } |
| 104 | } |