Skip to content

Commit f2bf3a1

Browse files
marcelklehrbackportbot[bot]
authored andcommitted
fix(TaskProcessingApiController): Improve error handling
Signed-off-by: Marcel Klehr <mklehr@gmx.net> [skip ci]
1 parent bd691d1 commit f2bf3a1

File tree

1 file changed

+35
-8
lines changed

1 file changed

+35
-8
lines changed

core/Controller/TaskProcessingApiController.php

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -513,22 +513,49 @@ public function cancelTask(int $taskId): DataResponse {
513513
#[ApiRoute(verb: 'GET', url: '/tasks_provider/next', root: '/taskprocessing')]
514514
public function getNextScheduledTask(array $providerIds, array $taskTypeIds): DataResponse {
515515
try {
516+
$providerIdsBasedOnTaskTypesWithNull = array_unique(array_map(function ($taskTypeId) {
517+
try {
518+
return $this->taskProcessingManager->getPreferredProvider($taskTypeId)->getId();
519+
} catch (Exception) {
520+
return null;
521+
}
522+
}, $taskTypeIds));
523+
524+
$providerIdsBasedOnTaskTypes = array_filter($providerIdsBasedOnTaskTypesWithNull, fn ($providerId) => $providerId !== null);
525+
516526
// restrict $providerIds to providers that are configured as preferred for the passed task types
517-
$providerIds = array_values(array_intersect(array_unique(array_map(fn ($taskTypeId) => $this->taskProcessingManager->getPreferredProvider($taskTypeId)->getId(), $taskTypeIds)), $providerIds));
527+
$possibleProviderIds = array_values(array_intersect($providerIdsBasedOnTaskTypes, $providerIds));
528+
518529
// restrict $taskTypeIds to task types that can actually be run by one of the now restricted providers
519-
$taskTypeIds = array_values(array_filter($taskTypeIds, fn ($taskTypeId) => in_array($this->taskProcessingManager->getPreferredProvider($taskTypeId)->getId(), $providerIds, true)));
520-
if (count($providerIds) === 0 || count($taskTypeIds) === 0) {
530+
$possibleTaskTypeIds = array_values(array_filter($taskTypeIds, function ($taskTypeId) use ($possibleProviderIds) {
531+
try {
532+
$providerForTaskType = $this->taskProcessingManager->getPreferredProvider($taskTypeId)->getId();
533+
} catch (Exception) {
534+
// no provider found for task type
535+
return false;
536+
}
537+
return in_array($providerForTaskType, $possibleProviderIds, true);
538+
}));
539+
540+
if (count($possibleProviderIds) === 0 || count($possibleTaskTypeIds) === 0) {
521541
throw new NotFoundException();
522542
}
523543

524544
$taskIdsToIgnore = [];
525545
while (true) {
526-
$task = $this->taskProcessingManager->getNextScheduledTask($taskTypeIds, $taskIdsToIgnore);
527-
$provider = $this->taskProcessingManager->getPreferredProvider($task->getTaskTypeId());
528-
if (in_array($provider->getId(), $providerIds, true)) {
529-
if ($this->taskProcessingManager->lockTask($task)) {
530-
break;
546+
// Until we find a task whose task type is set to be provided by the providers requested with this request
547+
// Or no scheduled task is found anymore (given the taskIds to ignore)
548+
$task = $this->taskProcessingManager->getNextScheduledTask($possibleTaskTypeIds, $taskIdsToIgnore);
549+
try {
550+
$provider = $this->taskProcessingManager->getPreferredProvider($task->getTaskTypeId());
551+
if (in_array($provider->getId(), $possibleProviderIds, true)) {
552+
if ($this->taskProcessingManager->lockTask($task)) {
553+
break;
554+
}
531555
}
556+
} catch (Exception) {
557+
// There is no provider set for the task type of this task
558+
// proceed to ignore this task
532559
}
533560
$taskIdsToIgnore[] = (int) $task->getId();
534561
}

0 commit comments

Comments
 (0)