-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add command and cron job for task cleanup
Signed-off-by: MB-Finski <[email protected]>
- Loading branch information
Showing
6 changed files
with
127 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters