feat(taskprocessing): add manager method to get the list of available task type IDs

Signed-off-by: Julien Veyssier <julien-nc@posteo.net>
pull/54848/head
Julien Veyssier 4 months ago
parent 05b4403354
commit 8c447eaba6
No known key found for this signature in database
GPG Key ID: 4141FEE162030638
  1. 51
      lib/private/TaskProcessing/Manager.php
  2. 10
      lib/public/TaskProcessing/IManager.php

@ -81,6 +81,9 @@ class Manager implements IManager {
public const MAX_TASK_AGE_SECONDS = 60 * 60 * 24 * 30 * 4; // 4 months
private const TASK_TYPES_CACHE_KEY = 'available_task_types_v2';
private const TASK_TYPE_IDS_CACHE_KEY = 'available_task_type_ids';
/** @var list<IProvider>|null */
private ?array $providers = null;
@ -89,6 +92,9 @@ class Manager implements IManager {
*/
private ?array $availableTaskTypes = null;
/** @var list<string>|null */
private ?array $availableTaskTypeIds = null;
private IAppData $appData;
private ?array $preferences = null;
private ?array $providersById = null;
@ -834,7 +840,7 @@ class Manager implements IManager {
return [];
}
if ($this->availableTaskTypes === null) {
$cachedValue = $this->distributedCache->get('available_task_types_v2');
$cachedValue = $this->distributedCache->get(self::TASK_TYPES_CACHE_KEY);
if ($cachedValue !== null) {
$this->availableTaskTypes = unserialize($cachedValue);
}
@ -880,12 +886,53 @@ class Manager implements IManager {
}
$this->availableTaskTypes = $availableTaskTypes;
$this->distributedCache->set('available_task_types_v2', serialize($this->availableTaskTypes), 60);
$this->distributedCache->set(self::TASK_TYPES_CACHE_KEY, serialize($this->availableTaskTypes), 60);
}
return $this->availableTaskTypes;
}
public function getAvailableTaskTypeIds(bool $showDisabled = false, ?string $userId = null): array {
// userId will be obtained from the session if left to null
if (!$this->checkGuestAccess($userId)) {
return [];
}
if ($this->availableTaskTypeIds === null) {
$cachedValue = $this->distributedCache->get(self::TASK_TYPE_IDS_CACHE_KEY);
if ($cachedValue !== null) {
$this->availableTaskTypeIds = $cachedValue;
}
}
// Either we have no cache or showDisabled is turned on, which we don't want to cache, ever.
if ($this->availableTaskTypeIds === null || $showDisabled) {
$taskTypes = $this->_getTaskTypes();
$taskTypeSettings = $this->_getTaskTypeSettings();
$availableTaskTypeIds = [];
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;
}
$availableTaskTypeIds[] = $taskType->getId();
}
if ($showDisabled) {
// Do not cache showDisabled, ever.
return $availableTaskTypeIds;
}
$this->availableTaskTypeIds = $availableTaskTypeIds;
$this->distributedCache->set(self::TASK_TYPE_IDS_CACHE_KEY, $this->availableTaskTypeIds, 60);
}
return $this->availableTaskTypeIds;
}
public function canHandleTask(Task $task): bool {
return isset($this->getAvailableTaskTypes()[$task->getTaskTypeId()]);

@ -47,7 +47,7 @@ interface IManager {
public function getPreferredProvider(string $taskTypeId);
/**
* @param bool $showDisabled if false, disabled task types will be filtered
* @param bool $showDisabled if false, disabled task types will be filtered out
* @param ?string $userId to check if the user is a guest. Will be obtained from session if left to default
* @return array<string, array{name: string, description: string, inputShape: ShapeDescriptor[], inputShapeEnumValues: ShapeEnumValue[][], inputShapeDefaults: array<array-key, numeric|string>, optionalInputShape: ShapeDescriptor[], optionalInputShapeEnumValues: ShapeEnumValue[][], optionalInputShapeDefaults: array<array-key, numeric|string>, outputShape: ShapeDescriptor[], outputShapeEnumValues: ShapeEnumValue[][], optionalOutputShape: ShapeDescriptor[], optionalOutputShapeEnumValues: ShapeEnumValue[][]}>
* @since 30.0.0
@ -56,6 +56,14 @@ interface IManager {
*/
public function getAvailableTaskTypes(bool $showDisabled = false, ?string $userId = null): array;
/**
* @param bool $showDisabled if false, disabled task types will be filtered out
* @param ?string $userId to check if the user is a guest. Will be obtained from session if left to default
* @return list<string>
* @since 32.0.0
*/
public function getAvailableTaskTypeIds(bool $showDisabled = false, ?string $userId = null): array;
/**
* @param Task $task The task to run
* @throws PreConditionNotMetException If no or not the requested provider was registered but this method was still called

Loading…
Cancel
Save