Skip to content

Commit f241edf

Browse files
authored
Merge pull request #54046 from nextcloud/backport/54037/stable31
[stable31] fix(TaskProcessingApiController): Improve error handling
2 parents dbf81d7 + 1042d14 commit f241edf

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
@@ -573,23 +573,51 @@ public function cancelTask(int $taskId): DataResponse {
573573
#[ApiRoute(verb: 'GET', url: '/tasks_provider/next', root: '/taskprocessing')]
574574
public function getNextScheduledTask(array $providerIds, array $taskTypeIds): DataResponse {
575575
try {
576+
$providerIdsBasedOnTaskTypesWithNull = array_unique(array_map(function ($taskTypeId) {
577+
try {
578+
return $this->taskProcessingManager->getPreferredProvider($taskTypeId)->getId();
579+
} catch (Exception) {
580+
return null;
581+
}
582+
}, $taskTypeIds));
583+
584+
$providerIdsBasedOnTaskTypes = array_filter($providerIdsBasedOnTaskTypesWithNull, fn ($providerId) => $providerId !== null);
585+
576586
// restrict $providerIds to providers that are configured as preferred for the passed task types
577-
$providerIds = array_values(array_intersect(array_unique(array_map(fn ($taskTypeId) => $this->taskProcessingManager->getPreferredProvider($taskTypeId)->getId(), $taskTypeIds)), $providerIds));
587+
$possibleProviderIds = array_values(array_intersect($providerIdsBasedOnTaskTypes, $providerIds));
588+
578589
// restrict $taskTypeIds to task types that can actually be run by one of the now restricted providers
579-
$taskTypeIds = array_values(array_filter($taskTypeIds, fn ($taskTypeId) => in_array($this->taskProcessingManager->getPreferredProvider($taskTypeId)->getId(), $providerIds, true)));
580-
if (count($providerIds) === 0 || count($taskTypeIds) === 0) {
590+
$possibleTaskTypeIds = array_values(array_filter($taskTypeIds, function ($taskTypeId) use ($possibleProviderIds) {
591+
try {
592+
$providerForTaskType = $this->taskProcessingManager->getPreferredProvider($taskTypeId)->getId();
593+
} catch (Exception) {
594+
// no provider found for task type
595+
return false;
596+
}
597+
return in_array($providerForTaskType, $possibleProviderIds, true);
598+
}));
599+
600+
if (count($possibleProviderIds) === 0 || count($possibleTaskTypeIds) === 0) {
581601
throw new NotFoundException();
582602
}
583603

584604
$taskIdsToIgnore = [];
585605
while (true) {
586-
$task = $this->taskProcessingManager->getNextScheduledTask($taskTypeIds, $taskIdsToIgnore);
587-
$provider = $this->taskProcessingManager->getPreferredProvider($task->getTaskTypeId());
588-
if (in_array($provider->getId(), $providerIds, true)) {
589-
if ($this->taskProcessingManager->lockTask($task)) {
590-
break;
606+
// Until we find a task whose task type is set to be provided by the providers requested with this request
607+
// Or no scheduled task is found anymore (given the taskIds to ignore)
608+
$task = $this->taskProcessingManager->getNextScheduledTask($possibleTaskTypeIds, $taskIdsToIgnore);
609+
try {
610+
$provider = $this->taskProcessingManager->getPreferredProvider($task->getTaskTypeId());
611+
if (in_array($provider->getId(), $possibleProviderIds, true)) {
612+
if ($this->taskProcessingManager->lockTask($task)) {
613+
break;
614+
}
591615
}
616+
} catch (Exception) {
617+
// There is no provider set for the task type of this task
618+
// proceed to ignore this task
592619
}
620+
593621
$taskIdsToIgnore[] = (int)$task->getId();
594622
}
595623

0 commit comments

Comments
 (0)