diff --git a/appinfo/info.xml b/appinfo/info.xml index 297db3ef1..fa5432a08 100644 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -40,6 +40,7 @@ OCA\Deck\Cron\DeleteCron OCA\Deck\Cron\ScheduledNotifications OCA\Deck\Cron\CardDescriptionActivity + OCA\Deck\Cron\SessionsCleanup diff --git a/lib/Cron/SessionsCleanup.php b/lib/Cron/SessionsCleanup.php new file mode 100644 index 000000000..cbdf18713 --- /dev/null +++ b/lib/Cron/SessionsCleanup.php @@ -0,0 +1,56 @@ +. + * + */ + + + +namespace OCA\Deck\Cron; + +use OCA\Deck\Service\SessionService; +use OCP\AppFramework\Utility\ITimeFactory; +use OCP\BackgroundJob\TimedJob; +use OCP\ILogger; + +class SessionsCleanup extends TimedJob { + private $sessionService; + private $documentService; + private $logger; + private $imageService; + + + public function __construct(ITimeFactory $time, + SessionService $sessionService, + ILogger $logger) { + parent::__construct($time); + $this->sessionService = $sessionService; + $this->logger = $logger; + $this->setInterval(SessionService::SESSION_VALID_TIME); + } + + protected function run($argument) { + $this->logger->debug('Run cleanup job for deck sessions'); + + $removedSessions = $this->sessionService->removeInactiveSessions(); + $this->logger->debug('Removed ' . $removedSessions . ' inactive sessions'); + } +} diff --git a/lib/Db/SessionMapper.php b/lib/Db/SessionMapper.php index b01281738..b4424a499 100644 --- a/lib/Db/SessionMapper.php +++ b/lib/Db/SessionMapper.php @@ -64,4 +64,10 @@ class SessionMapper extends QBMapper { return $this->findEntities($qb); } + public function deleteInactive(): int { + $qb = $this->db->getQueryBuilder(); + $qb->delete($this->getTableName()) + ->where($qb->expr()->lt('last_contact', $qb->createNamedParameter(time() - SessionService::SESSION_VALID_TIME))); + return $qb->executeStatement(); + } } diff --git a/lib/Migration/Version10900Date202206151724222.php b/lib/Migration/Version10900Date202206151724222.php index fd036de9a..037fe56af 100644 --- a/lib/Migration/Version10900Date202206151724222.php +++ b/lib/Migration/Version10900Date202206151724222.php @@ -60,6 +60,7 @@ class Version10900Date202206151724222 extends SimpleMigrationStep { $table->setPrimaryKey(['id']); $table->addIndex(['board_id'], 'deck_session_board_id_idx'); $table->addIndex(['token'], 'deck_session_token_idx'); + $table->addIndex(['last_contact'], 'deck_session_last_contact_idx'); } return $schema; } diff --git a/lib/Service/SessionService.php b/lib/Service/SessionService.php index 8a2ad233b..2132f6e4f 100644 --- a/lib/Service/SessionService.php +++ b/lib/Service/SessionService.php @@ -86,6 +86,10 @@ class SessionService { } $this->eventDispatcher->dispatchTyped(new SessionClosedEvent($boardId, $this->userId)); } + + public function removeInactiveSessions(): int { + return $this->sessionMapper->deleteInactive(); + } public function notifyAllSessions(IQueue $queue, int $boardId, $event, $excludeUserId, $body) { $activeSessions = $this->sessionMapper->findAllActive($boardId);