Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TaskProcessing] Add start, stop and schedule time to tasks #46359

Merged
merged 5 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions core/Command/TaskProcessing/ListCommand.php
Original file line number Diff line number Diff line change
@@ -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() {

Check notice

Code scanning / Psalm

MissingReturnType Note

Method OC\Core\Command\TaskProcessing\ListCommand::configure does not have a return type, expecting void
$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;
}
}
193 changes: 193 additions & 0 deletions core/Command/TaskProcessing/Statistics.php
Original file line number Diff line number Diff line change
@@ -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() {

Check notice

Code scanning / Psalm

MissingReturnType Note

Method OC\Core\Command\TaskProcessing\Statistics::configure does not have a return type, expecting void
$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;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mmh, using SQL directly may be faster and a bit more idomatic, perhaps. Not sure if it's possible, though.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bah, we can do that if it becomes relevant, I guess. This is probably good enough for now

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;
}
}
56 changes: 56 additions & 0 deletions core/Migrations/Version30000Date20240708160048.php
Original file line number Diff line number Diff line change
@@ -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;
}
}
3 changes: 3 additions & 0 deletions core/register_command.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@
$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));
}
3 changes: 3 additions & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -1180,6 +1180,8 @@
'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',
Expand Down Expand Up @@ -1326,6 +1328,7 @@
'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',
Expand Down
3 changes: 3 additions & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down
Loading
Loading