Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 59 |
|
0.00% |
0 / 13 |
CRAP | |
0.00% |
0 / 1 |
| AbstractQueue | |
0.00% |
0 / 59 |
|
0.00% |
0 / 13 |
756 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
42 | |||
| setRunner | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| setPersistence | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| getRunner | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| getPersistence | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| updateTaskStatus | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| updateTaskReport | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| getTask | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| runTask | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getPayload | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| getTaskResource | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| getReportByLinkedResource | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
20 | |||
| linkTask | |
0.00% |
0 / 10 |
|
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\oatbox\task; |
| 23 | |
| 24 | use oat\oatbox\service\ConfigurableService; |
| 25 | use oat\oatbox\task\Exception\BadTaskQueueOption; |
| 26 | use oat\oatbox\task\TaskInterface\TaskPayLoad; |
| 27 | use oat\oatbox\task\TaskInterface\TaskPersistenceInterface; |
| 28 | use oat\oatbox\task\TaskInterface\TaskQueue; |
| 29 | use oat\oatbox\task\TaskInterface\TaskRunner as TaskRunnerInterface; |
| 30 | use oat\generis\model\OntologyAwareTrait; |
| 31 | |
| 32 | /** |
| 33 | * Class AbstractQueue |
| 34 | * generic abstract queue object |
| 35 | * |
| 36 | * @package oat\oatbox\task |
| 37 | * |
| 38 | * @deprecated since version 7.10.0, to be removed in 8.0. Use \oat\tao\model\taskQueue\QueueDispatcher instead. |
| 39 | */ |
| 40 | abstract class AbstractQueue extends ConfigurableService implements TaskQueue |
| 41 | { |
| 42 | use OntologyAwareTrait; |
| 43 | |
| 44 | /** |
| 45 | * @var TaskRunner |
| 46 | */ |
| 47 | protected $runner; |
| 48 | |
| 49 | /** |
| 50 | * @var TaskPersistenceInterface |
| 51 | */ |
| 52 | protected $persistence; |
| 53 | |
| 54 | /** |
| 55 | * AbstractQueue constructor. |
| 56 | * |
| 57 | * config exemple : |
| 58 | * 'payload' => payload class name, |
| 59 | * 'runner' => task runner class name, |
| 60 | * 'persistence' => persistence class name, |
| 61 | * 'config' => [ |
| 62 | * persistence options array |
| 63 | * custom in function of needs |
| 64 | * ], |
| 65 | * |
| 66 | * @param array $options |
| 67 | * @throws BadTaskQueueOption |
| 68 | */ |
| 69 | public function __construct(array $options = []) |
| 70 | { |
| 71 | parent::__construct($options); |
| 72 | if ($this->hasOption('runner')) { |
| 73 | $classRunner = $this->getOption('runner'); |
| 74 | if (!is_a($classRunner, TaskRunnerInterface::class, true)) { |
| 75 | throw new BadTaskQueueOption('task runner must implement ' . TaskRunnerInterface::class); |
| 76 | } |
| 77 | $this->runner = new $classRunner(); |
| 78 | } |
| 79 | |
| 80 | if ($this->hasOption('persistence') && $this->hasOption('config')) { |
| 81 | $classPersistence = $this->getOption('persistence'); |
| 82 | if (!is_a($classPersistence, TaskPersistenceInterface::class, true)) { |
| 83 | throw new BadTaskQueueOption('task persistence must implement ' . TaskPersistenceInterface::class); |
| 84 | } |
| 85 | $configPersistence = $this->getOption('config'); |
| 86 | $this->persistence = new $classPersistence($configPersistence); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * set task runner |
| 92 | * |
| 93 | * @deprecated since version 7.10.0, to be removed in 8.0. |
| 94 | * |
| 95 | * @param TaskRunnerInterface $runner |
| 96 | * @return $this |
| 97 | */ |
| 98 | public function setRunner(TaskRunnerInterface $runner) |
| 99 | { |
| 100 | $this->runner = $runner; |
| 101 | return $this; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * set task persistence |
| 106 | * |
| 107 | * @deprecated since version 7.10.0, to be removed in 8.0. |
| 108 | * |
| 109 | * @param TaskPersistenceInterface $persistence |
| 110 | * @return $this |
| 111 | */ |
| 112 | public function setPersistence(TaskPersistenceInterface $persistence) |
| 113 | { |
| 114 | $this->persistence = $persistence; |
| 115 | return $this; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * return task runner |
| 120 | * |
| 121 | * @deprecated since version 7.10.0, to be removed in 8.0. |
| 122 | * |
| 123 | * @return TaskRunner |
| 124 | */ |
| 125 | public function getRunner() |
| 126 | { |
| 127 | $this->runner->setServiceLocator($this->getServiceLocator()); |
| 128 | return $this->runner; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * return task persistence |
| 133 | * |
| 134 | * @deprecated since version 7.10.0, to be removed in 8.0. |
| 135 | * |
| 136 | * @return TaskPersistenceInterface |
| 137 | */ |
| 138 | public function getPersistence() |
| 139 | { |
| 140 | $this->persistence->setServiceLocator($this->getServiceLocator()); |
| 141 | return $this->persistence; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * change task status using task persistence |
| 146 | * |
| 147 | * @deprecated since version 7.10.0, to be removed in 8.0. |
| 148 | * |
| 149 | * @param $taskId |
| 150 | * @param $status |
| 151 | * @return self |
| 152 | */ |
| 153 | public function updateTaskStatus($taskId, $status) |
| 154 | { |
| 155 | if ($this->getPersistence()->has($taskId)) { |
| 156 | $this->getPersistence()->update($taskId, $status); |
| 157 | } |
| 158 | return $this; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * set task report using task persistence |
| 163 | * |
| 164 | * @deprecated since version 7.10.0, to be removed in 8.0. |
| 165 | * |
| 166 | * @param $taskId |
| 167 | * @param $report |
| 168 | * @return self |
| 169 | */ |
| 170 | public function updateTaskReport($taskId, $report) |
| 171 | { |
| 172 | if ($this->getPersistence()->has($taskId)) { |
| 173 | $this->getPersistence()->setReport($taskId, $report); |
| 174 | } |
| 175 | return $this; |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * get task from persistence |
| 180 | * |
| 181 | * @deprecated since version 7.10.0, to be removed in 8.0. |
| 182 | * |
| 183 | * @param $taskId |
| 184 | * @return Task |
| 185 | */ |
| 186 | public function getTask($taskId) |
| 187 | { |
| 188 | return $this->getPersistence()->get($taskId); |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * execute task with task runner |
| 193 | * |
| 194 | * @deprecated since version 7.10.0, to be removed in 8.0. |
| 195 | * |
| 196 | * @param Task $task |
| 197 | * @return mixed |
| 198 | */ |
| 199 | public function runTask(Task $task) |
| 200 | { |
| 201 | return $this->getRunner()->run($task); |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * return a new instance of payload |
| 206 | * |
| 207 | * @deprecated since version 7.10.0, to be removed in 8.0. |
| 208 | * |
| 209 | * @param $currentUserId |
| 210 | * @return TaskPayLoad |
| 211 | * @throws BadTaskQueueOption |
| 212 | */ |
| 213 | public function getPayload($currentUserId = null) |
| 214 | { |
| 215 | $class = $this->getOption('payload'); |
| 216 | if (!is_a($class, TaskPayLoad::class, true)) { |
| 217 | throw new BadTaskQueueOption('task payload must implement ' . TaskPayLoad::class); |
| 218 | } |
| 219 | /** |
| 220 | * @var $payload TaskPayLoad |
| 221 | */ |
| 222 | $payload = new $class($this->getPersistence(), $currentUserId); |
| 223 | $payload->setServiceLocator($this->getServiceLocator()); |
| 224 | /** |
| 225 | * @var TaskPayLoad $payload |
| 226 | */ |
| 227 | return $payload; |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Get resource from rdf storage which represents task in the task queue by linked resource |
| 232 | * Returns null if there is no task linked to given resource |
| 233 | * |
| 234 | * @deprecated since version 7.10.0, to be removed in 8.0. |
| 235 | * |
| 236 | * @param \core_kernel_classes_Resource $resource |
| 237 | * @return null|\core_kernel_classes_Resource |
| 238 | */ |
| 239 | public function getTaskResource(\core_kernel_classes_Resource $resource) |
| 240 | { |
| 241 | $tasksRootClass = $this->getClass(Task::TASK_CLASS); |
| 242 | $task = $tasksRootClass->searchInstances([Task::PROPERTY_LINKED_RESOURCE => $resource->getUri()]); |
| 243 | return empty($task) ? null : current($task); |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * @deprecated since version 7.10.0, to be removed in 8.0. |
| 248 | * |
| 249 | * @param \core_kernel_classes_Resource $resource |
| 250 | * @return \common_report_Report |
| 251 | */ |
| 252 | public function getReportByLinkedResource(\core_kernel_classes_Resource $resource) |
| 253 | { |
| 254 | $taskResource = $this->getTaskResource($resource); |
| 255 | if ($taskResource !== null) { |
| 256 | $report = $taskResource->getOnePropertyValue($this->getProperty(Task::PROPERTY_REPORT)); |
| 257 | if ($report) { |
| 258 | $report = \common_report_Report::jsonUnserialize($report->literal); |
| 259 | } else { |
| 260 | $task = $this->getTask($taskResource->getUri()); |
| 261 | if ($task) { |
| 262 | $report = \common_report_Report::createInfo(__('Task is in \'%s\' state', $task->getStatus())); |
| 263 | } else { |
| 264 | //this is an assumption. |
| 265 | //in case if sync implementation is used task may not be found. |
| 266 | $report = \common_report_Report::createInfo(__('Task is in progress')); |
| 267 | } |
| 268 | } |
| 269 | } else { |
| 270 | $report = \common_report_Report::createFailure(__('Resource is not the task placeholder')); |
| 271 | } |
| 272 | return $report; |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * Create task resource in the rdf storage and link placeholder resource to it. |
| 277 | * |
| 278 | * @deprecated since version 7.10.0, to be removed in 8.0. |
| 279 | * |
| 280 | * @param Task $task |
| 281 | * @param \core_kernel_classes_Resource|null $resource - placeholder resource to be linked with task. |
| 282 | * @return \core_kernel_classes_Resource |
| 283 | */ |
| 284 | public function linkTask(Task $task, \core_kernel_classes_Resource $resource = null) |
| 285 | { |
| 286 | $taskResource = $this->getResource($task->getId()); |
| 287 | if (!$taskResource->exists()) { |
| 288 | $tasksRootClass = $this->getClass(Task::TASK_CLASS); |
| 289 | $taskResource = $tasksRootClass->createInstance('', '', $task->getId()); |
| 290 | } |
| 291 | if ($resource !== null) { |
| 292 | $taskResource->setPropertyValue( |
| 293 | $this->getProperty(Task::PROPERTY_LINKED_RESOURCE), |
| 294 | $resource->getUri() |
| 295 | ); |
| 296 | } |
| 297 | return $taskResource; |
| 298 | } |
| 299 | } |