Skip to content

Commit c666b81

Browse files
committed
fix(TaskProcessingApiController): Improve error handling
Signed-off-by: Marcel Klehr <mklehr@gmx.net>
1 parent 12685d6 commit c666b81

File tree

1 file changed

+36
-8
lines changed

1 file changed

+36
-8
lines changed

core/Controller/TaskProcessingApiController.php

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -575,23 +575,51 @@ public function cancelTask(int $taskId): DataResponse {
575575
#[ApiRoute(verb: 'GET', url: '/tasks_provider/next', root: '/taskprocessing')]
576576
public function getNextScheduledTask(array $providerIds, array $taskTypeIds): DataResponse {
577577
try {
578+
$providerIdsBasedOnTaskTypesWithNull = array_unique(array_map(function ($taskTypeId) {
579+
try {
580+
return $this->taskProcessingManager->getPreferredProvider($taskTypeId)->getId();
581+
} catch (Exception) {
582+
return null;
583+
}
584+
}, $taskTypeIds));
585+
586+
$providerIdsBasedOnTaskTypes = array_filter($providerIdsBasedOnTaskTypesWithNull, fn($providerId) => $providerId !== null);
587+
578588
// restrict $providerIds to providers that are configured as preferred for the passed task types
579-
$providerIds = array_values(array_intersect(array_unique(array_map(fn ($taskTypeId) => $this->taskProcessingManager->getPreferredProvider($taskTypeId)->getId(), $taskTypeIds)), $providerIds));
589+
$possibleProviderIds = array_values(array_intersect($providerIdsBasedOnTaskTypes, $providerIds));
590+
580591
// restrict $taskTypeIds to task types that can actually be run by one of the now restricted providers
581-
$taskTypeIds = array_values(array_filter($taskTypeIds, fn ($taskTypeId) => in_array($this->taskProcessingManager->getPreferredProvider($taskTypeId)->getId(), $providerIds, true)));
582-
if (count($providerIds) === 0 || count($taskTypeIds) === 0) {
592+
$possibleTaskTypeIds = array_values(array_filter($taskTypeIds, function ($taskTypeId) use ($possibleProviderIds) {
593+
try {
594+
$providerForTaskType = $this->taskProcessingManager->getPreferredProvider($taskTypeId)->getId();
595+
} catch (Exception) {
596+
// no provider found for task type
597+
return false;
598+
}
599+
return in_array($providerForTaskType, $possibleProviderIds, true);
600+
}));
601+
602+
if (count($possibleProviderIds) === 0 || count($possibleTaskTypeIds) === 0) {
583603
throw new NotFoundException();
584604
}
585605

586606
$taskIdsToIgnore = [];
587607
while (true) {
588-
$task = $this->taskProcessingManager->getNextScheduledTask($taskTypeIds, $taskIdsToIgnore);
589-
$provider = $this->taskProcessingManager->getPreferredProvider($task->getTaskTypeId());
590-
if (in_array($provider->getId(), $providerIds, true)) {
591-
if ($this->taskProcessingManager->lockTask($task)) {
592-
break;
608+
// Until we find a task whose task type is set to be provided by the providers requested with this request
609+
// Or no scheduled task is found anymore (given the taskIds to ignore)
610+
$task = $this->taskProcessingManager->getNextScheduledTask($possibleTaskTypeIds, $taskIdsToIgnore);
611+
try {
612+
$provider = $this->taskProcessingManager->getPreferredProvider($task->getTaskTypeId());
613+
if (in_array($provider->getId(), $possibleProviderIds, true)) {
614+
if ($this->taskProcessingManager->lockTask($task)) {
615+
break;
616+
}
593617
}
618+
} catch (Exception) {
619+
// There is no provider set for the task type of this task
620+
// proceed to ignore this task
594621
}
622+
595623
$taskIdsToIgnore[] = (int)$task->getId();
596624
}
597625

0 commit comments

Comments
 (0)