diff options
author | Marcel Klehr <mklehr@gmx.net> | 2024-05-06 16:36:35 +0200 |
---|---|---|
committer | Marcel Klehr <mklehr@gmx.net> | 2024-05-14 11:38:41 +0200 |
commit | ec27c538b531108ab2cf26fe3264d001f9230aa2 (patch) | |
tree | 6618f6771a34642eece57f89b0f2a224880108ea /lib | |
parent | ef61c50f4bcd5ea5911370c682aff19618a4d355 (diff) | |
download | nextcloud-server-ec27c538b531108ab2cf26fe3264d001f9230aa2.tar.gz nextcloud-server-ec27c538b531108ab2cf26fe3264d001f9230aa2.zip |
fix: address review comments
Signed-off-by: Marcel Klehr <mklehr@gmx.net>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/private/AppFramework/Bootstrap/RegistrationContext.php | 4 | ||||
-rw-r--r-- | lib/private/TaskProcessing/Manager.php | 69 | ||||
-rw-r--r-- | lib/public/TaskProcessing/IManager.php | 3 | ||||
-rw-r--r-- | lib/public/TaskProcessing/ISynchronousProvider.php | 7 | ||||
-rw-r--r-- | lib/public/TaskProcessing/Task.php | 42 |
5 files changed, 77 insertions, 48 deletions
diff --git a/lib/private/AppFramework/Bootstrap/RegistrationContext.php b/lib/private/AppFramework/Bootstrap/RegistrationContext.php index 31f3dd7e4d2..9750933854c 100644 --- a/lib/private/AppFramework/Bootstrap/RegistrationContext.php +++ b/lib/private/AppFramework/Bootstrap/RegistrationContext.php @@ -164,10 +164,10 @@ class RegistrationContext { private array $teamResourceProviders = []; /** @var ServiceRegistration<\OCP\TaskProcessing\IProvider>[] */ - private $taskProcessingProviders = []; + private array $taskProcessingProviders = []; /** @var ServiceRegistration<\OCP\TaskProcessing\ITaskType>[] */ - private $taskProcessingTaskTypes = []; + private array $taskProcessingTaskTypes = []; public function __construct(LoggerInterface $logger) { $this->logger = $logger; diff --git a/lib/private/TaskProcessing/Manager.php b/lib/private/TaskProcessing/Manager.php index 058437db388..7b0d3104736 100644 --- a/lib/private/TaskProcessing/Manager.php +++ b/lib/private/TaskProcessing/Manager.php @@ -505,9 +505,10 @@ class Manager implements IManager { } /** - * @param array<string,mixed> $array The array to filter - * @param array<string, mixed> ...$specs the specs that define which keys to keep - * @return array<string, mixed> + * @param array<array-key, T> $array The array to filter + * @param ShapeDescriptor[] ...$specs the specs that define which keys to keep + * @return array<array-key, T> + * @psalm-template T */ private function removeSuperfluousArrayKeys(array $array, ...$specs): array { $keys = array_unique(array_reduce($specs, fn ($carry, $spec) => $carry + array_keys($spec), [])); @@ -679,7 +680,7 @@ class Manager implements IManager { $this->validateOutput($outputShape, $result); $this->validateOutput($optionalOutputShape, $result, true); $output = $this->removeSuperfluousArrayKeys($result, $outputShape, $optionalOutputShape); - // extract base64 data and put it in files, replace it with file ids + // extract raw data and put it in files, replace it with file ids $output = $this->encapsulateOutputFileData($output, $outputShape, $optionalOutputShape); $task->setOutput($output); $task->setProgress(1); @@ -726,36 +727,12 @@ class Manager implements IManager { } } - public function getUserTask(int $id, ?string $userId): Task { - try { - $taskEntity = $this->taskMapper->findByIdAndUser($id, $userId); - return $taskEntity->toPublicTask(); - } catch (DoesNotExistException $e) { - throw new \OCP\TaskProcessing\Exception\NotFoundException('Could not find the task', 0, $e); - } catch (MultipleObjectsReturnedException|\OCP\DB\Exception $e) { - throw new \OCP\TaskProcessing\Exception\Exception('There was a problem finding the task', 0, $e); - } catch (\JsonException $e) { - throw new \OCP\TaskProcessing\Exception\Exception('There was a problem parsing JSON after finding the task', 0, $e); - } - } - - public function getUserTasksByApp(?string $userId, string $appId, ?string $identifier = null): array { - try { - $taskEntities = $this->taskMapper->findUserTasksByApp($userId, $appId, $identifier); - return array_map(fn ($taskEntity): Task => $taskEntity->toPublicTask(), $taskEntities); - } catch (\OCP\DB\Exception $e) { - throw new \OCP\TaskProcessing\Exception\Exception('There was a problem finding a task', 0, $e); - } catch (\JsonException $e) { - throw new \OCP\TaskProcessing\Exception\Exception('There was a problem parsing JSON after finding a task', 0, $e); - } - } - /** * Takes task input or output data and replaces fileIds with base64 data * + * @param array<array-key, list<numeric|string>|numeric|string> $input * @param ShapeDescriptor[] ...$specs the specs - * @param array $input - * @return array + * @return array<array-key, list<File|numeric|string>|numeric|string|File> * @throws GenericFileException * @throws LockedException * @throws NotPermittedException @@ -805,6 +782,30 @@ class Manager implements IManager { return $newInputOutput; } + public function getUserTask(int $id, ?string $userId): Task { + try { + $taskEntity = $this->taskMapper->findByIdAndUser($id, $userId); + return $taskEntity->toPublicTask(); + } catch (DoesNotExistException $e) { + throw new \OCP\TaskProcessing\Exception\NotFoundException('Could not find the task', 0, $e); + } catch (MultipleObjectsReturnedException|\OCP\DB\Exception $e) { + throw new \OCP\TaskProcessing\Exception\Exception('There was a problem finding the task', 0, $e); + } catch (\JsonException $e) { + throw new \OCP\TaskProcessing\Exception\Exception('There was a problem parsing JSON after finding the task', 0, $e); + } + } + + public function getUserTasksByApp(?string $userId, string $appId, ?string $identifier = null): array { + try { + $taskEntities = $this->taskMapper->findUserTasksByApp($userId, $appId, $identifier); + return array_map(fn ($taskEntity): Task => $taskEntity->toPublicTask(), $taskEntities); + } catch (\OCP\DB\Exception $e) { + throw new \OCP\TaskProcessing\Exception\Exception('There was a problem finding a task', 0, $e); + } catch (\JsonException $e) { + throw new \OCP\TaskProcessing\Exception\Exception('There was a problem parsing JSON after finding a task', 0, $e); + } + } + /** *Takes task input or output and replaces base64 data with file ids * @@ -846,6 +847,14 @@ class Manager implements IManager { return $newOutput; } + /** + * @param Task $task + * @return array<array-key, list<numeric|string|File>|numeric|string|File> + * @throws GenericFileException + * @throws LockedException + * @throws NotPermittedException + * @throws ValidationException + */ public function prepareInputData(Task $task): array { $taskTypes = $this->getAvailableTaskTypes(); $inputShape = $taskTypes[$task->getTaskTypeId()]['inputShape']; diff --git a/lib/public/TaskProcessing/IManager.php b/lib/public/TaskProcessing/IManager.php index 8589a8a1721..71c04d009ef 100644 --- a/lib/public/TaskProcessing/IManager.php +++ b/lib/public/TaskProcessing/IManager.php @@ -26,6 +26,7 @@ declare(strict_types=1); namespace OCP\TaskProcessing; +use OCP\Files\File; use OCP\Files\GenericFileException; use OCP\Files\NotPermittedException; use OCP\Lock\LockedException; @@ -150,7 +151,7 @@ interface IManager { * ie. this replaces file ids with base64 data * * @param Task $task - * @return array<string, mixed> + * @return array<array-key, list<numeric|string|File>|numeric|string|File> * @throws NotPermittedException * @throws GenericFileException * @throws LockedException diff --git a/lib/public/TaskProcessing/ISynchronousProvider.php b/lib/public/TaskProcessing/ISynchronousProvider.php index 0b17c6b6d86..16d66414b64 100644 --- a/lib/public/TaskProcessing/ISynchronousProvider.php +++ b/lib/public/TaskProcessing/ISynchronousProvider.php @@ -26,6 +26,7 @@ declare(strict_types=1); namespace OCP\TaskProcessing; +use OCP\Files\File; use OCP\TaskProcessing\Exception\ProcessingException; /** @@ -38,11 +39,11 @@ interface ISynchronousProvider extends IProvider { /** * Returns the shape of optional output parameters * - * @since 30.0.0 * @param null|string $userId The user that created the current task - * @param array<string, mixed> $input The task input - * @psalm-return array<string, mixed> + * @param array<string, list<numeric|string|File>|numeric|string|File> $input The task input + * @psalm-return array<string, list<numeric|string>|numeric|string> * @throws ProcessingException + *@since 30.0.0 */ public function process(?string $userId, array $input): array; } diff --git a/lib/public/TaskProcessing/Task.php b/lib/public/TaskProcessing/Task.php index eb4cbe345e1..3645970e4b3 100644 --- a/lib/public/TaskProcessing/Task.php +++ b/lib/public/TaskProcessing/Task.php @@ -75,7 +75,8 @@ final class Task implements \JsonSerializable { protected int $status = self::STATUS_UNKNOWN; /** - * @param array<string,mixed> $input + * @param string $taskTypeId + * @param array<string,list<numeric|string>|numeric|string> $input * @param string $appId * @param string|null $userId * @param null|string $identifier An arbitrary identifier for this task. max length: 255 chars @@ -146,6 +147,7 @@ final class Task implements \JsonSerializable { } /** + * @param null|array<array-key, list<numeric|string>|numeric|string> $output * @since 30.0.0 */ final public function setOutput(?array $output): void { @@ -153,7 +155,7 @@ final class Task implements \JsonSerializable { } /** - * @return array<array-key, mixed>|null + * @return array<array-key, list<numeric|string>|numeric|string>|null * @since 30.0.0 */ final public function getOutput(): ?array { @@ -161,7 +163,7 @@ final class Task implements \JsonSerializable { } /** - * @return array<array-key, mixed> + * @return array<array-key, list<numeric|string>|numeric|string> * @since 30.0.0 */ final public function getInput(): array { @@ -193,20 +195,20 @@ final class Task implements \JsonSerializable { } /** - * @psalm-return array{id: ?int, type: string, status: self::STATUS_*, userId: ?string, appId: string, input: ?array<array-key, mixed>, output: ?array<array-key, mixed>, identifier: ?string, completionExpectedAt: ?int, progress: ?float} + * @psalm-return array{id: ?int, type: string, status: 'STATUS_CANCELLED'|'STATUS_FAILED'|'STATUS_SUCCESSFUL'|'STATUS_RUNNING'|'STATUS_SCHEDULED'|'STATUS_UNKNOWN', userId: ?string, appId: string, input: array<array-key, list<numeric|string>|numeric|string>, output: ?array<array-key, list<numeric|string>|numeric|string>, identifier: ?string, completionExpectedAt: ?int, progress: ?float} * @since 30.0.0 */ - public function jsonSerialize(): array { + final public function jsonSerialize(): array { return [ 'id' => $this->getId(), 'type' => $this->getTaskTypeId(), - 'status' => $this->getStatus(), + 'status' => self::statusToString($this->getStatus()), 'userId' => $this->getUserId(), 'appId' => $this->getAppId(), 'input' => $this->getInput(), 'output' => $this->getOutput(), 'identifier' => $this->getIdentifier(), - 'completionExpectedAt' => $this->getCompletionExpectedAt()->getTimestamp(), + 'completionExpectedAt' => $this->getCompletionExpectedAt()?->getTimestamp(), 'progress' => $this->getProgress(), ]; } @@ -216,7 +218,7 @@ final class Task implements \JsonSerializable { * @return void * @since 30.0.0 */ - public function setErrorMessage(?string $error) { + final public function setErrorMessage(?string $error) { $this->errorMessage = $error; } @@ -224,7 +226,7 @@ final class Task implements \JsonSerializable { * @return string|null * @since 30.0.0 */ - public function getErrorMessage(): ?string { + final public function getErrorMessage(): ?string { return $this->errorMessage; } @@ -233,7 +235,7 @@ final class Task implements \JsonSerializable { * @return void * @since 30.0.0 */ - public function setInput(array $input): void { + final public function setInput(array $input): void { $this->input = $input; } @@ -243,7 +245,7 @@ final class Task implements \JsonSerializable { * @throws ValidationException * @since 30.0.0 */ - public function setProgress(?float $progress): void { + final public function setProgress(?float $progress): void { if ($progress < 0 || $progress > 1.0) { throw new ValidationException('Progress must be between 0.0 and 1.0 inclusively; ' . $progress . ' given'); } @@ -254,7 +256,23 @@ final class Task implements \JsonSerializable { * @return float|null * @since 30.0.0 */ - public function getProgress(): ?float { + final public function getProgress(): ?float { return $this->progress; } + + /** + * @param int $status + * @return 'STATUS_CANCELLED'|'STATUS_FAILED'|'STATUS_SUCCESSFUL'|'STATUS_RUNNING'|'STATUS_SCHEDULED'|'STATUS_UNKNOWN' + * @since 30.0.0 + */ + final public static function statusToString(int $status): string { + return match ($status) { + self::STATUS_CANCELLED => 'STATUS_CANCELLED', + self::STATUS_FAILED => 'STATUS_FAILED', + self::STATUS_SUCCESSFUL => 'STATUS_SUCCESSFUL', + self::STATUS_RUNNING => 'STATUS_RUNNING', + self::STATUS_SCHEDULED => 'STATUS_SCHEDULED', + default => 'STATUS_UNKNOWN', + }; + } } |