Skip to content

Commit

Permalink
Add command and cron job for task cleanup
Browse files Browse the repository at this point in the history
Signed-off-by: MB-Finski <[email protected]>
  • Loading branch information
MB-Finski committed Jan 29, 2024
1 parent 7364add commit 37f6cd6
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 4 deletions.
2 changes: 2 additions & 0 deletions appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,11 @@ include text processing providers to:
<screenshot>https://github.com/nextcloud/assistant/raw/main/img/screenshot3.jpg</screenshot>
<background-jobs>
<job>OCA\TpAssistant\Cron\CleanupImageGenerations</job>
<job>OCA\TpAssistant\Cron\CleanupAssistantTasks</job>
</background-jobs>
<commands>
<command>OCA\TpAssistant\Command\CleanupImageGenerations</command>
<command>OCA\TpAssistant\Command\CleanupAssistantTasks</command>
</commands>
<dependencies>
<nextcloud min-version="28" max-version="29"/>
Expand Down
1 change: 1 addition & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
class Application extends App implements IBootstrap {

public const APP_ID = 'assistant';
public const DEFAULT_ASSISTANT_TASK_IDLE_TIME = 60 * 60 * 24 * 14; // 14 days

public const MAX_STORED_IMAGE_PROMPTS_PER_USER = 5;
public const MAX_STORED_TEXT_PROMPTS_PER_USER = 5;
Expand Down
54 changes: 54 additions & 0 deletions lib/Command/CleanupAssistantTasks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

// SPDX-FileCopyrightText: Sami Finnilä <[email protected]>
// SPDX-License-Identifier: AGPL-3.0-or-later

namespace OCA\TpAssistant\Command;

use Exception;
use OC\Core\Command\Base;
use OCA\TpAssistant\AppInfo\Application;
use OCA\TpAssistant\Db\TaskMapper;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class CleanupAssistantTasks extends Base {
public function __construct(
private TaskMapper $taskMapper,
) {
parent::__construct();
}

protected function configure() {
$maxIdleTimeSetting = Application::DEFAULT_ASSISTANT_TASK_IDLE_TIME;
$this->setName('assistant:task_cleanup')
->setDescription('Cleanup assistant tasks')
->addArgument(
'max_age',
InputArgument::OPTIONAL,
'The max idle time (in seconds)',
$maxIdleTimeSetting
);
}

protected function execute(InputInterface $input, OutputInterface $output) {
$maxAge = intval($input->getArgument('max_age'));

if ($maxAge < 1) {
$output->writeln('Invalid value for max_age: ' . $maxAge);
return 1;
}

$output->writeln('Cleanning up assistant tasks older than ' . $maxAge . ' seconds.');
try {
$cleanedUp = $this->taskMapper->cleanupOldTasks($maxAge);
} catch (Exception $e) {
$output->writeln('Error: ' . $e->getMessage());
return 1;
}

$output->writeln('Deleted ' . $cleanedUp . ' idle tasks.');
return 0;
}
}
38 changes: 38 additions & 0 deletions lib/Cron/CleanupAssistantTasks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);
// SPDX-FileCopyrightText: Sami Finnilä <[email protected]>
// SPDX-License-Identifier: AGPL-3.0-or-later

namespace OCA\TpAssistant\Cron;

use Exception;
use OCA\TpAssistant\AppInfo\Application;
use OCA\TpAssistant\Db\TaskMapper;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\TimedJob;
use Psr\Log\LoggerInterface;
use RuntimeException;

class CleanupImageGenerations extends TimedJob {
public function __construct(
ITimeFactory $time,
private LoggerInterface $logger,
private TaskMapper $taskMapper,
) {
parent::__construct($time);
$this->setInterval(60 * 60 * 24);
}

protected function run($argument): void {
$this->logger->debug('Run cleanup job for assistant tasks');

try {
$this->taskMapper->cleanupOldTasks(Application::DEFAULT_ASSISTANT_TASK_IDLE_TIME);
} catch (\OCP\Db\Exception | RuntimeException | Exception $e) {
$this->logger->debug('Cleanup job for assistant tasks failed: ' . $e->getMessage());
}

return;
}
}
2 changes: 1 addition & 1 deletion lib/Cron/CleanupImageGenerations.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function __construct(
ITimeFactory $time,
private ImageGenerationMapper $imageGenerationMapper,
private LoggerInterface $logger,
private CleanUpService $cleanUpService
private CleanUpService $cleanUpService,
) {
parent::__construct($time);
$this->setInterval(60 * 60 * 24);
Expand Down
34 changes: 31 additions & 3 deletions lib/Db/TaskMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace OCA\TpAssistant\Db;

use DateTime;
use Doctrine\DBAL\Exception\InvalidArgumentException;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
use OCP\AppFramework\Db\QBMapper;
Expand Down Expand Up @@ -67,6 +68,16 @@ public function getTaskByOcpTaskIdAndCategory(int $ocpTaskId, int $category): Ta

/** @var Task $retVal */
$retVal = $this->findEntity($qb);

// Touch the timestamp to prevent the task from being cleaned up:
$retVal->setTimestamp((new DateTime())->getTimestamp());
try {
$retVal = $this->update($retVal);
} catch (\InvalidArgumentException $e) {
// This should never happen
throw new Exception('Failed to touch timestamp of task', 0, $e);
}

return $retVal;
}

Expand All @@ -90,6 +101,19 @@ public function getTasksByOcpTaskIdAndCategory(int $ocpTaskId, int $category): a

/** @var array<Task> $retVal */
$retVal = $this->findEntities($qb);

// Touch the timestamps to prevent the task from being cleaned up:
foreach ($retVal as &$task) {
$task->setTimestamp((new DateTime())->getTimestamp());
try {
$task = $this->update($task);
} catch (\InvalidArgumentException $e) {
// This should never happen
throw new Exception('Failed to touch timestamp of task', 0 , $e);
}
}
unset($task);

return $retVal;
}

Expand Down Expand Up @@ -196,16 +220,20 @@ public function deleteUserTasks(string $userId): void {
}

/**
* Clean up tasks older than 14 days
* Clean up tasks older than specified (14 days by default)
* @param ?int $olderThanSeconds
* @return int number of deleted rows
* @throws Exception
* @throws \RuntimeException
*/
public function cleanupOldTasks(): int {
public function cleanupOldTasks($olderThanSeconds): int {
if ($olderThanSeconds === null) {
$olderThanSeconds = 14 * 24 * 60 * 60;
}
$qb = $this->db->getQueryBuilder();
$qb->delete($this->getTableName())
->where(
$qb->expr()->lt('timestamp', $qb->createNamedParameter(time() - 14 * 24 * 60 * 60, IQueryBuilder::PARAM_INT))
$qb->expr()->lt('timestamp', $qb->createNamedParameter((new DateTime())->getTimestamp() - $olderThanSeconds, IQueryBuilder::PARAM_INT))
);
return $qb->executeStatement();
}
Expand Down

0 comments on commit 37f6cd6

Please sign in to comment.