Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 45 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
tao_actions_TaskQueue | |
0.00% |
0 / 45 |
|
0.00% |
0 / 4 |
210 | |
0.00% |
0 / 1 |
get | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
56 | |||
getAll | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
12 | |||
getStatus | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 | |||
getTaskLogEntityDecorateProcessor | |
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 | */ |
19 | |
20 | use oat\generis\model\fileReference\FileReferenceSerializer; |
21 | use oat\oatbox\filesystem\FileSystemService; |
22 | use oat\tao\model\taskQueue\TaskLog\Broker\TaskLogBrokerInterface; |
23 | use oat\tao\model\taskQueue\TaskLog\Decorator\SimpleManagementCollectionDecorator; |
24 | use oat\tao\model\taskQueue\TaskLog\Decorator\TaskLogEntityDecorateProcessor; |
25 | use oat\tao\model\taskQueue\TaskLog\TaskLogFilter; |
26 | use oat\tao\model\taskQueue\TaskLogInterface; |
27 | use oat\tao\model\TaskQueueActionTrait; |
28 | |
29 | /** |
30 | * Rest API controller for task queue |
31 | * |
32 | * @package oat\tao\controller\api |
33 | * @author Aleh Hutnikau, <hutnikau@1pt.com> |
34 | */ |
35 | class tao_actions_TaskQueue extends \tao_actions_RestController |
36 | { |
37 | use TaskQueueActionTrait; |
38 | |
39 | public const TASK_ID_PARAM = 'id'; |
40 | public const PARAMETER_LIMIT = 'limit'; |
41 | public const PARAMETER_OFFSET = 'offset'; |
42 | |
43 | /** |
44 | * Get task data by identifier |
45 | */ |
46 | public function get() |
47 | { |
48 | try { |
49 | if (!$this->hasRequestParameter(self::TASK_ID_PARAM)) { |
50 | throw new \common_exception_MissingParameter(self::TASK_ID_PARAM, $this->getRequestURI()); |
51 | } |
52 | |
53 | $taskId = $this->getRequestParameter(self::TASK_ID_PARAM); |
54 | |
55 | /** @var TaskLogInterface $taskLog */ |
56 | $taskLog = $this->getServiceLocator()->get(TaskLogInterface::SERVICE_ID); |
57 | |
58 | $filter = (new TaskLogFilter()) |
59 | ->eq(TaskLogBrokerInterface::COLUMN_ID, $taskId); |
60 | |
61 | // trying to get data from the new task queue first |
62 | $collection = $taskLog->search($filter); |
63 | |
64 | if ($collection->isEmpty()) { |
65 | // if we don't have the task in the new queue, |
66 | // loading the data from the old one |
67 | $data = $this->getTaskData($taskId); |
68 | } else { |
69 | // we have the task in the new queue |
70 | $entity = $collection->first(); |
71 | $status = (string) $entity->getStatus(); |
72 | |
73 | if ($entity->getStatus()->isInProgress()) { |
74 | $status = \oat\oatbox\task\Task::STATUS_RUNNING; |
75 | } elseif ($entity->getStatus()->isCompleted() || $entity->getStatus()->isFailed()) { |
76 | $status = \oat\oatbox\task\Task::STATUS_FINISHED; |
77 | } |
78 | |
79 | $data['id'] = $entity->getId(); |
80 | $data['status'] = $status; |
81 | //convert to array for xml response. |
82 | $data['report'] = json_decode(json_encode($entity->getReport())); |
83 | } |
84 | |
85 | $this->returnSuccess($data); |
86 | } catch (\Exception $e) { |
87 | $this->returnFailure($e); |
88 | } |
89 | } |
90 | |
91 | /** |
92 | * @throws \common_exception_NotImplemented |
93 | */ |
94 | public function getAll() |
95 | { |
96 | /** @var TaskLogInterface $taskLogService */ |
97 | $taskLogService = $this->getServiceLocator()->get(TaskLogInterface::SERVICE_ID); |
98 | $limit = $offset = null; |
99 | |
100 | if ($this->hasRequestParameter(self::PARAMETER_LIMIT)) { |
101 | $limit = (int) $this->getRequestParameter(self::PARAMETER_LIMIT); |
102 | } |
103 | |
104 | if ($this->hasRequestParameter(self::PARAMETER_OFFSET)) { |
105 | $offset = (int) $this->getRequestParameter(self::PARAMETER_OFFSET); |
106 | } |
107 | |
108 | $userId = $this->getSession()->getUser()->getIdentifier(); |
109 | $collection = new SimpleManagementCollectionDecorator( |
110 | $taskLogService->findAvailableByUser($userId, $limit, $offset), |
111 | $taskLogService, |
112 | $this->getServiceLocator()->get(FileSystemService::SERVICE_ID), |
113 | $this->getServiceLocator()->get(FileReferenceSerializer::SERVICE_ID), |
114 | $this->getTaskLogEntityDecorateProcessor(), |
115 | false |
116 | ); |
117 | |
118 | $this->returnSuccess($collection->toArray()); |
119 | } |
120 | |
121 | /** |
122 | * Returns only the status of a task, independently from the owner. |
123 | * |
124 | * NOTE: only works for tasks handled by the new task queue. |
125 | */ |
126 | public function getStatus() |
127 | { |
128 | try { |
129 | if (!$this->hasRequestParameter(self::TASK_ID_PARAM)) { |
130 | throw new \common_exception_MissingParameter(self::TASK_ID_PARAM, $this->getRequestURI()); |
131 | } |
132 | |
133 | /** @var TaskLogInterface $taskLogService */ |
134 | $taskLogService = $this->getServiceLocator()->get(TaskLogInterface::SERVICE_ID); |
135 | |
136 | $entity = $taskLogService->getById((string) $this->getRequestParameter(self::TASK_ID_PARAM)); |
137 | |
138 | $this->returnSuccess((string) $entity->getStatus()); |
139 | } catch (\Exception $e) { |
140 | $this->returnFailure($e); |
141 | } |
142 | } |
143 | |
144 | protected function getTaskLogEntityDecorateProcessor(): TaskLogEntityDecorateProcessor |
145 | { |
146 | return $this->getServiceManager()->getContainer()->get(TaskLogEntityDecorateProcessor::class); |
147 | } |
148 | } |