diff options
author | Marcel Klehr <mklehr@gmx.net> | 2023-10-17 11:54:01 +0200 |
---|---|---|
committer | Marcel Klehr <mklehr@gmx.net> | 2023-10-18 13:21:50 +0200 |
commit | c59861a9fa1f0bc6fe6958abe99da9240af0d092 (patch) | |
tree | 913665643bd7a48ed1ef4bf7280bff28d48c96ec /lib/private/TextToImage | |
parent | 207c95838f16216382c9c4fc0e094c1db5a49299 (diff) | |
download | nextcloud-server-c59861a9fa1f0bc6fe6958abe99da9240af0d092.tar.gz nextcloud-server-c59861a9fa1f0bc6fe6958abe99da9240af0d092.zip |
enh(TextToImage): Implement removal of stale images and change Task#last_updated to DATETIME
Signed-off-by: Marcel Klehr <mklehr@gmx.net>
Diffstat (limited to 'lib/private/TextToImage')
-rw-r--r-- | lib/private/TextToImage/Db/Task.php | 7 | ||||
-rw-r--r-- | lib/private/TextToImage/Db/TaskMapper.php | 20 | ||||
-rw-r--r-- | lib/private/TextToImage/RemoveOldTasksBackgroundJob.php | 23 |
3 files changed, 40 insertions, 10 deletions
diff --git a/lib/private/TextToImage/Db/Task.php b/lib/private/TextToImage/Db/Task.php index 84d43ab6fd9..e545f459d6c 100644 --- a/lib/private/TextToImage/Db/Task.php +++ b/lib/private/TextToImage/Db/Task.php @@ -25,6 +25,7 @@ declare(strict_types=1); namespace OC\TextToImage\Db; +use DateTime; use OCP\AppFramework\Db\Entity; use OCP\Files\AppData\IAppDataFactory; use OCP\Files\NotFoundException; @@ -33,8 +34,8 @@ use OCP\Image; use OCP\TextToImage\Task as OCPTask; /** - * @method setLastUpdated(int $lastUpdated) - * @method int getLastUpdated() + * @method setLastUpdated(DateTime $lastUpdated) + * @method DateTime getLastUpdated() * @method setInput(string $type) * @method string getInput() * @method setResultPath(string $resultPath) @@ -71,7 +72,7 @@ class Task extends Entity { public function __construct() { // add types in constructor $this->addType('id', 'integer'); - $this->addType('lastUpdated', 'integer'); + $this->addType('lastUpdated', 'datetime'); $this->addType('input', 'string'); $this->addType('status', 'integer'); $this->addType('userId', 'string'); diff --git a/lib/private/TextToImage/Db/TaskMapper.php b/lib/private/TextToImage/Db/TaskMapper.php index 44d8aea24eb..f65d5a1d17b 100644 --- a/lib/private/TextToImage/Db/TaskMapper.php +++ b/lib/private/TextToImage/Db/TaskMapper.php @@ -25,12 +25,14 @@ declare(strict_types=1); namespace OC\TextToImage\Db; +use DateTime; use OCP\AppFramework\Db\DoesNotExistException; use OCP\AppFramework\Db\Entity; use OCP\AppFramework\Db\MultipleObjectsReturnedException; use OCP\AppFramework\Db\QBMapper; use OCP\AppFramework\Utility\ITimeFactory; use OCP\DB\Exception; +use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\IDBConnection; /** @@ -101,18 +103,26 @@ class TaskMapper extends QBMapper { /** * @param int $timeout - * @return int the number of deleted tasks + * @return Task[] the deleted tasks * @throws Exception */ - public function deleteOlderThan(int $timeout): int { + public function deleteOlderThan(int $timeout): array { + $datetime = new DateTime(); + $datetime->sub(new \DateInterval('PT'.$timeout.'S')); + $qb = $this->db->getQueryBuilder(); + $qb->select('*') + ->from($this->tableName) + ->where($qb->expr()->lt('last_updated', $qb->createPositionalParameter($datetime, IQueryBuilder::PARAM_DATE))); + $deletedTasks = $this->findEntities($qb); $qb = $this->db->getQueryBuilder(); $qb->delete($this->tableName) - ->where($qb->expr()->lt('last_updated', $qb->createPositionalParameter(time() - $timeout))); - return $qb->executeStatement(); + ->where($qb->expr()->lt('last_updated', $qb->createPositionalParameter($datetime, IQueryBuilder::PARAM_DATE))); + $qb->executeStatement(); + return $deletedTasks; } public function update(Entity $entity): Entity { - $entity->setLastUpdated($this->timeFactory->now()->getTimestamp()); + $entity->setLastUpdated(DateTime::createFromImmutable($this->timeFactory->now())); return parent::update($entity); } } diff --git a/lib/private/TextToImage/RemoveOldTasksBackgroundJob.php b/lib/private/TextToImage/RemoveOldTasksBackgroundJob.php index fe6c77cb790..82677fa635c 100644 --- a/lib/private/TextToImage/RemoveOldTasksBackgroundJob.php +++ b/lib/private/TextToImage/RemoveOldTasksBackgroundJob.php @@ -30,18 +30,25 @@ use OC\TextToImage\Db\TaskMapper; use OCP\AppFramework\Utility\ITimeFactory; use OCP\BackgroundJob\TimedJob; use OCP\DB\Exception; +use OCP\Files\AppData\IAppDataFactory; +use OCP\Files\IAppData; +use OCP\Files\NotFoundException; +use OCP\Files\NotPermittedException; use Psr\Log\LoggerInterface; class RemoveOldTasksBackgroundJob extends TimedJob { public const MAX_TASK_AGE_SECONDS = 60 * 50 * 24 * 7; // 1 week + private IAppData $appData; + public function __construct( ITimeFactory $timeFactory, private TaskMapper $taskMapper, private LoggerInterface $logger, - + private IAppDataFactory $appDataFactory, ) { parent::__construct($timeFactory); + $this->appData = $this->appDataFactory->get('core'); $this->setInterval(60 * 60 * 24); } @@ -51,9 +58,21 @@ class RemoveOldTasksBackgroundJob extends TimedJob { */ protected function run($argument) { try { - $this->taskMapper->deleteOlderThan(self::MAX_TASK_AGE_SECONDS); + $deletedTasks = $this->taskMapper->deleteOlderThan(self::MAX_TASK_AGE_SECONDS); + $folder = $this->appData->getFolder('text2image'); + foreach ($deletedTasks as $deletedTask) { + try { + $folder->getFile((string)$deletedTask->getId())->delete(); + } catch (NotFoundException) { + // noop + } catch (NotPermittedException $e) { + $this->logger->warning('Failed to delete stale text to image task', ['exception' => $e]); + } + } } catch (Exception $e) { $this->logger->warning('Failed to delete stale text to image tasks', ['exception' => $e]); + } catch(NotFoundException) { + // noop } } } |