Skip to content

Commit

Permalink
feat: Remove stale files in AppData as well
Browse files Browse the repository at this point in the history
Signed-off-by: Marcel Klehr <mklehr@gmx.net>
  • Loading branch information
marcelklehr committed May 6, 2024
1 parent a1baf47 commit dadb8b2
Showing 1 changed file with 34 additions and 2 deletions.
36 changes: 34 additions & 2 deletions lib/private/TaskProcessing/RemoveOldTasksBackgroundJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,63 @@
use OC\TaskProcessing\Db\TaskMapper;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\TimedJob;
use OCP\Files\AppData\IAppDataFactory;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\Files\SimpleFS\ISimpleFolder;
use Psr\Log\LoggerInterface;

class RemoveOldTasksBackgroundJob extends TimedJob {
public const MAX_TASK_AGE_SECONDS = 60 * 50 * 24 * 7 * 4; // 4 weeks
private \OCP\Files\IAppData $appData;

public function __construct(
ITimeFactory $timeFactory,
private TaskMapper $taskMapper,
private LoggerInterface $logger,
IAppDataFactory $appDataFactory,
) {
parent::__construct($timeFactory);
$this->setInterval(60 * 60 * 24);
// can be deferred to maintenance window
$this->setTimeSensitivity(TimedJob::TIME_INSENSITIVE);
$this->appData = $appDataFactory->get('core');
}


/**
* @inheritDoc
*/
protected function run($argument) {
protected function run($argument): void {
try {
$this->taskMapper->deleteOlderThan(self::MAX_TASK_AGE_SECONDS);
} catch (\OCP\DB\Exception $e) {
$this->logger->warning('Failed to delete stale language model tasks', ['exception' => $e]);
$this->logger->warning('Failed to delete stale task processing tasks', ['exception' => $e]);
}
try {
$this->clearFilesOlderThan($this->appData->getFolder('text2image'), self::MAX_TASK_AGE_SECONDS);
$this->clearFilesOlderThan($this->appData->getFolder('audio2text'), self::MAX_TASK_AGE_SECONDS);
$this->clearFilesOlderThan($this->appData->getFolder('TaskProcessing'), self::MAX_TASK_AGE_SECONDS);
} catch (NotFoundException $e) {
// noop
}
}

/**
* @param ISimpleFolder $folder
* @param int $ageInSeconds
* @return void
*/
private function clearFilesOlderThan(ISimpleFolder $folder, int $ageInSeconds): void {
foreach($folder->getDirectoryListing() as $file) {
if ($file->getMTime() < time() - $ageInSeconds) {
try {
$file->delete();
} catch (NotPermittedException $e) {
$this->logger->warning('Failed to delete a stale task processing file', ['exception' => $e]);
}
}
}
}

}

0 comments on commit dadb8b2

Please sign in to comment.