Merge pull request #46359 from nextcloud/enh/task-processing-more-datetimes

[TaskProcessing] Add start, stop and schedule time to tasks
This commit is contained in:
Julien Veyssier 2024-07-23 18:54:58 +02:00 committed by GitHub
commit f9d4becf60
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 521 additions and 3 deletions

View file

@ -0,0 +1,91 @@
<?php
/**
* SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OC\Core\Command\TaskProcessing;
use OC\Core\Command\Base;
use OCP\TaskProcessing\IManager;
use OCP\TaskProcessing\Task;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class ListCommand extends Base {
public function __construct(
protected IManager $taskProcessingManager,
) {
parent::__construct();
}
protected function configure() {
$this
->setName('taskprocessing:task:list')
->setDescription('list tasks')
->addOption(
'userIdFilter',
'u',
InputOption::VALUE_OPTIONAL,
'only get the tasks for one user ID'
)
->addOption(
'type',
't',
InputOption::VALUE_OPTIONAL,
'only get the tasks for one task type'
)
->addOption(
'appId',
null,
InputOption::VALUE_OPTIONAL,
'only get the tasks for one app ID'
)
->addOption(
'customId',
null,
InputOption::VALUE_OPTIONAL,
'only get the tasks for one custom ID'
)
->addOption(
'status',
's',
InputOption::VALUE_OPTIONAL,
'only get the tasks that have a specific status (STATUS_UNKNOWN=0, STATUS_SCHEDULED=1, STATUS_RUNNING=2, STATUS_SUCCESSFUL=3, STATUS_FAILED=4, STATUS_CANCELLED=5)'
)
->addOption(
'scheduledAfter',
null,
InputOption::VALUE_OPTIONAL,
'only get the tasks that were scheduled after a specific date (Unix timestamp)'
)
->addOption(
'endedBefore',
null,
InputOption::VALUE_OPTIONAL,
'only get the tasks that ended before a specific date (Unix timestamp)'
);
parent::configure();
}
protected function execute(InputInterface $input, OutputInterface $output): int {
$userIdFilter = $input->getOption('userIdFilter');
if ($userIdFilter === null) {
$userIdFilter = '';
} elseif ($userIdFilter === '') {
$userIdFilter = null;
}
$type = $input->getOption('type');
$appId = $input->getOption('appId');
$customId = $input->getOption('customId');
$status = $input->getOption('status');
$scheduledAfter = $input->getOption('scheduledAfter');
$endedBefore = $input->getOption('endedBefore');
$tasks = $this->taskProcessingManager->getTasks($userIdFilter, $type, $appId, $customId, $status, $scheduledAfter, $endedBefore);
$arrayTasks = array_map(fn (Task $task): array => $task->jsonSerialize(), $tasks);
$this->writeArrayInOutputFormat($input, $output, $arrayTasks);
return 0;
}
}

View file

@ -0,0 +1,193 @@
<?php
/**
* SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OC\Core\Command\TaskProcessing;
use OC\Core\Command\Base;
use OCP\TaskProcessing\IManager;
use OCP\TaskProcessing\Task;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class Statistics extends Base {
public function __construct(
protected IManager $taskProcessingManager,
) {
parent::__construct();
}
protected function configure() {
$this
->setName('taskprocessing:task:stats')
->setDescription('get statistics for tasks')
->addOption(
'userIdFilter',
'u',
InputOption::VALUE_OPTIONAL,
'only get the tasks for one user ID'
)
->addOption(
'type',
't',
InputOption::VALUE_OPTIONAL,
'only get the tasks for one task type'
)
->addOption(
'appId',
null,
InputOption::VALUE_OPTIONAL,
'only get the tasks for one app ID'
)
->addOption(
'customId',
null,
InputOption::VALUE_OPTIONAL,
'only get the tasks for one custom ID'
)
->addOption(
'status',
's',
InputOption::VALUE_OPTIONAL,
'only get the tasks that have a specific status (STATUS_UNKNOWN=0, STATUS_SCHEDULED=1, STATUS_RUNNING=2, STATUS_SUCCESSFUL=3, STATUS_FAILED=4, STATUS_CANCELLED=5)'
)
->addOption(
'scheduledAfter',
null,
InputOption::VALUE_OPTIONAL,
'only get the tasks that were scheduled after a specific date (Unix timestamp)'
)
->addOption(
'endedBefore',
null,
InputOption::VALUE_OPTIONAL,
'only get the tasks that ended before a specific date (Unix timestamp)'
);
parent::configure();
}
protected function execute(InputInterface $input, OutputInterface $output): int {
$userIdFilter = $input->getOption('userIdFilter');
if ($userIdFilter === null) {
$userIdFilter = '';
} elseif ($userIdFilter === '') {
$userIdFilter = null;
}
$type = $input->getOption('type');
$appId = $input->getOption('appId');
$customId = $input->getOption('customId');
$status = $input->getOption('status');
$scheduledAfter = $input->getOption('scheduledAfter');
$endedBefore = $input->getOption('endedBefore');
$tasks = $this->taskProcessingManager->getTasks($userIdFilter, $type, $appId, $customId, $status, $scheduledAfter, $endedBefore);
$stats = ['Number of tasks' => count($tasks)];
$maxRunningTime = 0;
$totalRunningTime = 0;
$runningTimeCount = 0;
$maxQueuingTime = 0;
$totalQueuingTime = 0;
$queuingTimeCount = 0;
$maxUserWaitingTime = 0;
$totalUserWaitingTime = 0;
$userWaitingTimeCount = 0;
$maxInputSize = 0;
$maxOutputSize = 0;
$inputCount = 0;
$inputSum = 0;
$outputCount = 0;
$outputSum = 0;
foreach ($tasks as $task) {
// running time
if ($task->getStartedAt() !== null && $task->getEndedAt() !== null) {
$taskRunningTime = $task->getEndedAt() - $task->getStartedAt();
$totalRunningTime += $taskRunningTime;
$runningTimeCount++;
if ($taskRunningTime >= $maxRunningTime) {
$maxRunningTime = $taskRunningTime;
}
}
// queuing time
if ($task->getScheduledAt() !== null && $task->getStartedAt() !== null) {
$taskQueuingTime = $task->getStartedAt() - $task->getScheduledAt();
$totalQueuingTime += $taskQueuingTime;
$queuingTimeCount++;
if ($taskQueuingTime >= $maxQueuingTime) {
$maxQueuingTime = $taskQueuingTime;
}
}
// user waiting time
if ($task->getScheduledAt() !== null && $task->getEndedAt() !== null) {
$taskUserWaitingTime = $task->getEndedAt() - $task->getScheduledAt();
$totalUserWaitingTime += $taskUserWaitingTime;
$userWaitingTimeCount++;
if ($taskUserWaitingTime >= $maxUserWaitingTime) {
$maxUserWaitingTime = $taskUserWaitingTime;
}
}
// input/output sizes
if ($task->getStatus() === Task::STATUS_SUCCESSFUL) {
$outputString = json_encode($task->getOutput());
if ($outputString !== false) {
$outputCount++;
$outputLength = strlen($outputString);
$outputSum += $outputLength;
if ($outputLength > $maxOutputSize) {
$maxOutputSize = $outputLength;
}
}
}
$inputString = json_encode($task->getInput());
if ($inputString !== false) {
$inputCount++;
$inputLength = strlen($inputString);
$inputSum += $inputLength;
if ($inputLength > $maxInputSize) {
$maxInputSize = $inputLength;
}
}
}
if ($runningTimeCount > 0) {
$stats['Max running time'] = $maxRunningTime;
$averageRunningTime = $totalRunningTime / $runningTimeCount;
$stats['Average running time'] = (int)$averageRunningTime;
$stats['Running time count'] = $runningTimeCount;
}
if ($queuingTimeCount > 0) {
$stats['Max queuing time'] = $maxQueuingTime;
$averageQueuingTime = $totalQueuingTime / $queuingTimeCount;
$stats['Average queuing time'] = (int)$averageQueuingTime;
$stats['Queuing time count'] = $queuingTimeCount;
}
if ($userWaitingTimeCount > 0) {
$stats['Max user waiting time'] = $maxUserWaitingTime;
$averageUserWaitingTime = $totalUserWaitingTime / $userWaitingTimeCount;
$stats['Average user waiting time'] = (int)$averageUserWaitingTime;
$stats['User waiting time count'] = $userWaitingTimeCount;
}
if ($outputCount > 0) {
$stats['Max output size (bytes)'] = $maxOutputSize;
$averageOutputSize = $outputSum / $outputCount;
$stats['Average output size (bytes)'] = (int)$averageOutputSize;
$stats['Number of tasks with output'] = $outputCount;
}
if ($inputCount > 0) {
$stats['Max input size (bytes)'] = $maxInputSize;
$averageInputSize = $inputSum / $inputCount;
$stats['Average input size (bytes)'] = (int)$averageInputSize;
$stats['Number of tasks with input'] = $inputCount;
}
$this->writeArrayInOutputFormat($input, $output, $stats);
return 0;
}
}

View file

@ -0,0 +1,56 @@
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OC\Core\Migrations;
use Closure;
use OCP\DB\ISchemaWrapper;
use OCP\DB\Types;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;
/**
*
*/
class Version30000Date20240708160048 extends SimpleMigrationStep {
/**
* @param IOutput $output
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
* @param array $options
* @return null|ISchemaWrapper
*/
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
/** @var ISchemaWrapper $schema */
$schema = $schemaClosure();
if ($schema->hasTable('taskprocessing_tasks')) {
$table = $schema->getTable('taskprocessing_tasks');
$table->addColumn('scheduled_at', Types::INTEGER, [
'notnull' => false,
'default' => null,
'unsigned' => true,
]);
$table->addColumn('started_at', Types::INTEGER, [
'notnull' => false,
'default' => null,
'unsigned' => true,
]);
$table->addColumn('ended_at', Types::INTEGER, [
'notnull' => false,
'default' => null,
'unsigned' => true,
]);
return $schema;
}
return null;
}
}

View file

@ -140,6 +140,9 @@ if ($config->getSystemValueBool('installed', false)) {
$application->add(Server::get(Command\Security\BruteforceResetAttempts::class));
$application->add(Server::get(Command\SetupChecks::class));
$application->add(Server::get(Command\FilesMetadata\Get::class));
$application->add(Server::get(Command\TaskProcessing\ListCommand::class));
$application->add(Server::get(Command\TaskProcessing\Statistics::class));
} else {
$application->add(Server::get(Command\Maintenance\Install::class));
}

View file

@ -1180,6 +1180,8 @@ return array(
'OC\\Core\\Command\\SystemTag\\Delete' => $baseDir . '/core/Command/SystemTag/Delete.php',
'OC\\Core\\Command\\SystemTag\\Edit' => $baseDir . '/core/Command/SystemTag/Edit.php',
'OC\\Core\\Command\\SystemTag\\ListCommand' => $baseDir . '/core/Command/SystemTag/ListCommand.php',
'OC\\Core\\Command\\TaskProcessing\\ListCommand' => $baseDir . '/core/Command/TaskProcessing/ListCommand.php',
'OC\\Core\\Command\\TaskProcessing\\Statistics' => $baseDir . '/core/Command/TaskProcessing/Statistics.php',
'OC\\Core\\Command\\TwoFactorAuth\\Base' => $baseDir . '/core/Command/TwoFactorAuth/Base.php',
'OC\\Core\\Command\\TwoFactorAuth\\Cleanup' => $baseDir . '/core/Command/TwoFactorAuth/Cleanup.php',
'OC\\Core\\Command\\TwoFactorAuth\\Disable' => $baseDir . '/core/Command/TwoFactorAuth/Disable.php',
@ -1326,6 +1328,7 @@ return array(
'OC\\Core\\Migrations\\Version29000Date20240124132202' => $baseDir . '/core/Migrations/Version29000Date20240124132202.php',
'OC\\Core\\Migrations\\Version29000Date20240131122720' => $baseDir . '/core/Migrations/Version29000Date20240131122720.php',
'OC\\Core\\Migrations\\Version30000Date20240429122720' => $baseDir . '/core/Migrations/Version30000Date20240429122720.php',
'OC\\Core\\Migrations\\Version30000Date20240708160048' => $baseDir . '/core/Migrations/Version30000Date20240708160048.php',
'OC\\Core\\Migrations\\Version30000Date20240717111406' => $baseDir . '/core/Migrations/Version30000Date20240717111406.php',
'OC\\Core\\Notification\\CoreNotifier' => $baseDir . '/core/Notification/CoreNotifier.php',
'OC\\Core\\ResponseDefinitions' => $baseDir . '/core/ResponseDefinitions.php',

View file

@ -1213,6 +1213,8 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OC\\Core\\Command\\SystemTag\\Delete' => __DIR__ . '/../../..' . '/core/Command/SystemTag/Delete.php',
'OC\\Core\\Command\\SystemTag\\Edit' => __DIR__ . '/../../..' . '/core/Command/SystemTag/Edit.php',
'OC\\Core\\Command\\SystemTag\\ListCommand' => __DIR__ . '/../../..' . '/core/Command/SystemTag/ListCommand.php',
'OC\\Core\\Command\\TaskProcessing\\ListCommand' => __DIR__ . '/../../..' . '/core/Command/TaskProcessing/ListCommand.php',
'OC\\Core\\Command\\TaskProcessing\\Statistics' => __DIR__ . '/../../..' . '/core/Command/TaskProcessing/Statistics.php',
'OC\\Core\\Command\\TwoFactorAuth\\Base' => __DIR__ . '/../../..' . '/core/Command/TwoFactorAuth/Base.php',
'OC\\Core\\Command\\TwoFactorAuth\\Cleanup' => __DIR__ . '/../../..' . '/core/Command/TwoFactorAuth/Cleanup.php',
'OC\\Core\\Command\\TwoFactorAuth\\Disable' => __DIR__ . '/../../..' . '/core/Command/TwoFactorAuth/Disable.php',
@ -1359,6 +1361,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OC\\Core\\Migrations\\Version29000Date20240124132202' => __DIR__ . '/../../..' . '/core/Migrations/Version29000Date20240124132202.php',
'OC\\Core\\Migrations\\Version29000Date20240131122720' => __DIR__ . '/../../..' . '/core/Migrations/Version29000Date20240131122720.php',
'OC\\Core\\Migrations\\Version30000Date20240429122720' => __DIR__ . '/../../..' . '/core/Migrations/Version30000Date20240429122720.php',
'OC\\Core\\Migrations\\Version30000Date20240708160048' => __DIR__ . '/../../..' . '/core/Migrations/Version30000Date20240708160048.php',
'OC\\Core\\Migrations\\Version30000Date20240717111406' => __DIR__ . '/../../..' . '/core/Migrations/Version30000Date20240717111406.php',
'OC\\Core\\Notification\\CoreNotifier' => __DIR__ . '/../../..' . '/core/Notification/CoreNotifier.php',
'OC\\Core\\ResponseDefinitions' => __DIR__ . '/../../..' . '/core/ResponseDefinitions.php',

View file

@ -39,6 +39,12 @@ use OCP\TaskProcessing\Task as OCPTask;
* @method string getWebhookUri()
* @method setWebhookMethod(string $webhookMethod)
* @method string getWebhookMethod()
* @method setScheduledAt(int $scheduledAt)
* @method int getScheduledAt()
* @method setStartedAt(int $startedAt)
* @method int getStartedAt()
* @method setEndedAt(int $endedAt)
* @method int getEndedAt()
*/
class Task extends Entity {
protected $lastUpdated;
@ -54,16 +60,19 @@ class Task extends Entity {
protected $progress;
protected $webhookUri;
protected $webhookMethod;
protected $scheduledAt;
protected $startedAt;
protected $endedAt;
/**
* @var string[]
*/
public static array $columns = ['id', 'last_updated', 'type', 'input', 'output', 'status', 'user_id', 'app_id', 'custom_id', 'completion_expected_at', 'error_message', 'progress', 'webhook_uri', 'webhook_method'];
public static array $columns = ['id', 'last_updated', 'type', 'input', 'output', 'status', 'user_id', 'app_id', 'custom_id', 'completion_expected_at', 'error_message', 'progress', 'webhook_uri', 'webhook_method', 'scheduled_at', 'started_at', 'ended_at'];
/**
* @var string[]
*/
public static array $fields = ['id', 'lastUpdated', 'type', 'input', 'output', 'status', 'userId', 'appId', 'customId', 'completionExpectedAt', 'errorMessage', 'progress', 'webhookUri', 'webhookMethod'];
public static array $fields = ['id', 'lastUpdated', 'type', 'input', 'output', 'status', 'userId', 'appId', 'customId', 'completionExpectedAt', 'errorMessage', 'progress', 'webhookUri', 'webhookMethod', 'scheduledAt', 'startedAt', 'endedAt'];
public function __construct() {
@ -82,6 +91,9 @@ class Task extends Entity {
$this->addType('progress', 'float');
$this->addType('webhookUri', 'string');
$this->addType('webhookMethod', 'string');
$this->addType('scheduledAt', 'integer');
$this->addType('startedAt', 'integer');
$this->addType('endedAt', 'integer');
}
public function toRow(): array {
@ -107,6 +119,9 @@ class Task extends Entity {
'progress' => $task->getProgress(),
'webhookUri' => $task->getWebhookUri(),
'webhookMethod' => $task->getWebhookMethod(),
'scheduledAt' => $task->getScheduledAt(),
'startedAt' => $task->getStartedAt(),
'endedAt' => $task->getEndedAt(),
]);
return $taskEntity;
}
@ -126,6 +141,9 @@ class Task extends Entity {
$task->setProgress($this->getProgress());
$task->setWebhookUri($this->getWebhookUri());
$task->setWebhookMethod($this->getWebhookMethod());
$task->setScheduledAt($this->getScheduledAt());
$task->setStartedAt($this->getStartedAt());
$task->setEndedAt($this->getEndedAt());
return $task;
}
}

View file

@ -136,6 +136,51 @@ class TaskMapper extends QBMapper {
return array_values($this->findEntities($qb));
}
/**
* @param string|null $userId
* @param string|null $taskType
* @param string|null $appId
* @param string|null $customId
* @param int|null $status
* @param int|null $scheduleAfter
* @param int|null $endedBefore
* @return list<Task>
* @throws Exception
*/
public function findTasks(
?string $userId, ?string $taskType = null, ?string $appId = null, ?string $customId = null,
?int $status = null, ?int $scheduleAfter = null, ?int $endedBefore = null): array {
$qb = $this->db->getQueryBuilder();
$qb->select(Task::$columns)
->from($this->tableName);
// empty string: no userId filter
if ($userId !== '') {
$qb->where($qb->expr()->eq('user_id', $qb->createPositionalParameter($userId)));
}
if ($taskType !== null) {
$qb->andWhere($qb->expr()->eq('type', $qb->createPositionalParameter($taskType)));
}
if ($appId !== null) {
$qb->andWhere($qb->expr()->eq('app_id', $qb->createPositionalParameter($appId)));
}
if ($customId !== null) {
$qb->andWhere($qb->expr()->eq('custom_id', $qb->createPositionalParameter($customId)));
}
if ($status !== null) {
$qb->andWhere($qb->expr()->eq('status', $qb->createPositionalParameter($status, IQueryBuilder::PARAM_INT)));
}
if ($scheduleAfter !== null) {
$qb->andWhere($qb->expr()->isNotNull('scheduled_at'));
$qb->andWhere($qb->expr()->gt('scheduled_at', $qb->createPositionalParameter($scheduleAfter, IQueryBuilder::PARAM_INT)));
}
if ($endedBefore !== null) {
$qb->andWhere($qb->expr()->isNotNull('ended_at'));
$qb->andWhere($qb->expr()->lt('ended_at', $qb->createPositionalParameter($endedBefore, IQueryBuilder::PARAM_INT)));
}
return array_values($this->findEntities($qb));
}
/**
* @param int $timeout
* @return int the number of deleted tasks

View file

@ -616,6 +616,7 @@ class Manager implements IManager {
// remove superfluous keys and set input
$task->setInput($this->removeSuperfluousArrayKeys($task->getInput(), $inputShape, $optionalInputShape));
$task->setStatus(Task::STATUS_SCHEDULED);
$task->setScheduledAt(time());
$provider = $this->getPreferredProvider($task->getTaskTypeId());
// calculate expected completion time
$completionExpectedAt = new \DateTime('now');
@ -656,6 +657,7 @@ class Manager implements IManager {
return;
}
$task->setStatus(Task::STATUS_CANCELLED);
$task->setEndedAt(time());
$taskEntity = \OC\TaskProcessing\Db\Task::fromPublicTask($task);
try {
$this->taskMapper->update($taskEntity);
@ -671,6 +673,10 @@ class Manager implements IManager {
if ($task->getStatus() === Task::STATUS_CANCELLED) {
return false;
}
// only set the start time if the task is going from scheduled to running
if ($task->getstatus() === Task::STATUS_SCHEDULED) {
$task->setStartedAt(time());
}
$task->setStatus(Task::STATUS_RUNNING);
$task->setProgress($progress);
$taskEntity = \OC\TaskProcessing\Db\Task::fromPublicTask($task);
@ -691,6 +697,7 @@ class Manager implements IManager {
}
if ($error !== null) {
$task->setStatus(Task::STATUS_FAILED);
$task->setEndedAt(time());
$task->setErrorMessage($error);
$this->logger->warning('A TaskProcessing ' . $task->getTaskTypeId() . ' task with id ' . $id . ' failed with the following message: ' . $error);
} elseif ($result !== null) {
@ -725,21 +732,25 @@ class Manager implements IManager {
$task->setOutput($output);
$task->setProgress(1);
$task->setStatus(Task::STATUS_SUCCESSFUL);
$task->setEndedAt(time());
} catch (ValidationException $e) {
$task->setProgress(1);
$task->setStatus(Task::STATUS_FAILED);
$task->setEndedAt(time());
$error = 'The task was processed successfully but the provider\'s output doesn\'t pass validation against the task type\'s outputShape spec and/or the provider\'s own optionalOutputShape spec';
$task->setErrorMessage($error);
$this->logger->error($error, ['exception' => $e]);
} catch (NotPermittedException $e) {
$task->setProgress(1);
$task->setStatus(Task::STATUS_FAILED);
$task->setEndedAt(time());
$error = 'The task was processed successfully but storing the output in a file failed';
$task->setErrorMessage($error);
$this->logger->error($error, ['exception' => $e]);
} catch (InvalidPathException|\OCP\Files\NotFoundException $e) {
$task->setProgress(1);
$task->setStatus(Task::STATUS_FAILED);
$task->setEndedAt(time());
$error = 'The task was processed successfully but the result file could not be found';
$task->setErrorMessage($error);
$this->logger->error($error, ['exception' => $e]);
@ -839,6 +850,20 @@ class Manager implements IManager {
}
}
public function getTasks(
?string $userId, ?string $taskTypeId = null, ?string $appId = null, ?string $customId = null,
?int $status = null, ?int $scheduleAfter = null, ?int $endedBefore = null
): array {
try {
$taskEntities = $this->taskMapper->findTasks($userId, $taskTypeId, $appId, $customId, $status, $scheduleAfter, $endedBefore);
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 the tasks', 0, $e);
} catch (\JsonException $e) {
throw new \OCP\TaskProcessing\Exception\Exception('There was a problem parsing JSON after finding the tasks', 0, $e);
}
}
public function getUserTasksByApp(?string $userId, string $appId, ?string $customId = null): array {
try {
$taskEntities = $this->taskMapper->findUserTasksByApp($userId, $appId, $customId);
@ -926,6 +951,14 @@ class Manager implements IManager {
* @throws Exception
*/
public function setTaskStatus(Task $task, int $status): void {
$currentTaskStatus = $task->getStatus();
if ($currentTaskStatus === Task::STATUS_SCHEDULED && $status === Task::STATUS_RUNNING) {
$task->setStartedAt(time());
} elseif ($currentTaskStatus === Task::STATUS_RUNNING && ($status === Task::STATUS_FAILED || $status === Task::STATUS_CANCELLED)) {
$task->setEndedAt(time());
} elseif ($currentTaskStatus === Task::STATUS_UNKNOWN && $status === Task::STATUS_SCHEDULED) {
$task->setScheduledAt(time());
}
$task->setStatus($status);
$taskEntity = \OC\TaskProcessing\Db\Task::fromPublicTask($task);
$this->taskMapper->update($taskEntity);

View file

@ -140,6 +140,24 @@ interface IManager {
*/
public function getUserTasks(?string $userId, ?string $taskTypeId = null, ?string $customId = null): array;
/**
* @param string|null $userId The user id that scheduled the task
* @param string|null $taskTypeId The task type id to filter by
* @param string|null $appId The app ID of the app that submitted the task
* @param string|null $customId The custom task ID
* @param int|null $status The task status
* @param int|null $scheduleAfter Minimum schedule time filter
* @param int|null $endedBefore Maximum ending time filter
* @return list<Task>
* @throws Exception If the query failed
* @throws NotFoundException If the task could not be found
* @since 30.0.0
*/
public function getTasks(
?string $userId, ?string $taskTypeId = null, ?string $appId = null, ?string $customId = null,
?int $status = null, ?int $scheduleAfter = null, ?int $endedBefore = null
): array;
/**
* @param string|null $userId
* @param string $appId

View file

@ -63,6 +63,10 @@ final class Task implements \JsonSerializable {
*/
protected int $status = self::STATUS_UNKNOWN;
protected ?int $scheduledAt = null;
protected ?int $startedAt = null;
protected ?int $endedAt = null;
/**
* @param string $taskTypeId
* @param array<string,list<numeric|string>|numeric|string> $input
@ -201,7 +205,55 @@ final class Task implements \JsonSerializable {
}
/**
* @psalm-return array{id: ?int, lastUpdated: 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>, customId: ?string, completionExpectedAt: ?int, progress: ?float}
* @return int|null
* @since 30.0.0
*/
final public function getScheduledAt(): ?int {
return $this->scheduledAt;
}
/**
* @param int|null $scheduledAt
* @since 30.0.0
*/
final public function setScheduledAt(?int $scheduledAt): void {
$this->scheduledAt = $scheduledAt;
}
/**
* @return int|null
* @since 30.0.0
*/
final public function getStartedAt(): ?int {
return $this->startedAt;
}
/**
* @param int|null $startedAt
* @since 30.0.0
*/
final public function setStartedAt(?int $startedAt): void {
$this->startedAt = $startedAt;
}
/**
* @return int|null
* @since 30.0.0
*/
final public function getEndedAt(): ?int {
return $this->endedAt;
}
/**
* @param int|null $endedAt
* @since 30.0.0
*/
final public function setEndedAt(?int $endedAt): void {
$this->endedAt = $endedAt;
}
/**
* @psalm-return array{id: ?int, lastUpdated: 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>, customId: ?string, completionExpectedAt: ?int, progress: ?float, scheduledAt: ?int, startedAt: ?int, endedAt: ?int}
* @since 30.0.0
*/
final public function jsonSerialize(): array {
@ -217,6 +269,9 @@ final class Task implements \JsonSerializable {
'customId' => $this->getCustomId(),
'completionExpectedAt' => $this->getCompletionExpectedAt()?->getTimestamp(),
'progress' => $this->getProgress(),
'scheduledAt' => $this->getScheduledAt(),
'startedAt' => $this->getStartedAt(),
'endedAt' => $this->getEndedAt(),
];
}