From 79023b9f3ec18cf72003822c1e837bc97ae0788a Mon Sep 17 00:00:00 2001 From: Marcel Klehr Date: Thu, 12 Dec 2024 11:50:39 +0100 Subject: fix(taskrpocessing): Cache result of getAvailableTaskTypes Signed-off-by: Marcel Klehr --- lib/private/TaskProcessing/Manager.php | 70 +++++++++++++++++++--------------- 1 file changed, 39 insertions(+), 31 deletions(-) diff --git a/lib/private/TaskProcessing/Manager.php b/lib/private/TaskProcessing/Manager.php index 2a7bcfab8b8..e263195d35e 100644 --- a/lib/private/TaskProcessing/Manager.php +++ b/lib/private/TaskProcessing/Manager.php @@ -733,41 +733,49 @@ class Manager implements IManager { } public function getAvailableTaskTypes(bool $showDisabled = false): array { - $taskTypes = $this->_getTaskTypes(); - $taskTypeSettings = $this->_getTaskTypeSettings(); - - $availableTaskTypes = []; - foreach ($taskTypes as $taskType) { - if ((!$showDisabled) && isset($taskTypeSettings[$taskType->getId()]) && !$taskTypeSettings[$taskType->getId()]) { - continue; - } - try { - $provider = $this->getPreferredProvider($taskType->getId()); - } catch (\OCP\TaskProcessing\Exception\Exception $e) { - continue; + // Either we have no cache or showDisabled is turned on, which we don't want to cache, ever. + if ($this->availableTaskTypes === null || $showDisabled) { + $taskTypes = $this->_getTaskTypes(); + $taskTypeSettings = $this->_getTaskTypeSettings(); + + $availableTaskTypes = []; + foreach ($taskTypes as $taskType) { + if ((!$showDisabled) && isset($taskTypeSettings[$taskType->getId()]) && !$taskTypeSettings[$taskType->getId()]) { + continue; + } + try { + $provider = $this->getPreferredProvider($taskType->getId()); + } catch (\OCP\TaskProcessing\Exception\Exception $e) { + continue; + } + try { + $availableTaskTypes[$provider->getTaskTypeId()] = [ + 'name' => $taskType->getName(), + 'description' => $taskType->getDescription(), + 'optionalInputShape' => $provider->getOptionalInputShape(), + 'inputShapeEnumValues' => $provider->getInputShapeEnumValues(), + 'inputShapeDefaults' => $provider->getInputShapeDefaults(), + 'inputShape' => $taskType->getInputShape(), + 'optionalInputShapeEnumValues' => $provider->getOptionalInputShapeEnumValues(), + 'optionalInputShapeDefaults' => $provider->getOptionalInputShapeDefaults(), + 'outputShape' => $taskType->getOutputShape(), + 'outputShapeEnumValues' => $provider->getOutputShapeEnumValues(), + 'optionalOutputShape' => $provider->getOptionalOutputShape(), + 'optionalOutputShapeEnumValues' => $provider->getOptionalOutputShapeEnumValues(), + ]; + } catch (\Throwable $e) { + $this->logger->error('Failed to set up TaskProcessing provider ' . $provider::class, ['exception' => $e]); + } } - try { - $availableTaskTypes[$provider->getTaskTypeId()] = [ - 'name' => $taskType->getName(), - 'description' => $taskType->getDescription(), - 'optionalInputShape' => $provider->getOptionalInputShape(), - 'inputShapeEnumValues' => $provider->getInputShapeEnumValues(), - 'inputShapeDefaults' => $provider->getInputShapeDefaults(), - 'inputShape' => $taskType->getInputShape(), - 'optionalInputShapeEnumValues' => $provider->getOptionalInputShapeEnumValues(), - 'optionalInputShapeDefaults' => $provider->getOptionalInputShapeDefaults(), - 'outputShape' => $taskType->getOutputShape(), - 'outputShapeEnumValues' => $provider->getOutputShapeEnumValues(), - 'optionalOutputShape' => $provider->getOptionalOutputShape(), - 'optionalOutputShapeEnumValues' => $provider->getOptionalOutputShapeEnumValues(), - ]; - } catch (\Throwable $e) { - $this->logger->error('Failed to set up TaskProcessing provider ' . $provider::class, ['exception' => $e]); + + if ($showDisabled) { + // Do not cache showDisabled, ever. + return $availableTaskTypes; } + + $this->availableTaskTypes = $availableTaskTypes; } - $this->availableTaskTypes = $availableTaskTypes; - return $this->availableTaskTypes; } -- cgit v1.2.3