Signed-off-by: Marcel Klehr <mklehr@gmx.net>tags/v28.0.0beta1
@@ -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'); |
@@ -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); | |||
} | |||
} |
@@ -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 | |||
} | |||
} | |||
} |