session cleanup via background job
Signed-off-by: chandi Langecker <git@chandi.it>
This commit is contained in:
@@ -40,6 +40,7 @@
|
|||||||
<job>OCA\Deck\Cron\DeleteCron</job>
|
<job>OCA\Deck\Cron\DeleteCron</job>
|
||||||
<job>OCA\Deck\Cron\ScheduledNotifications</job>
|
<job>OCA\Deck\Cron\ScheduledNotifications</job>
|
||||||
<job>OCA\Deck\Cron\CardDescriptionActivity</job>
|
<job>OCA\Deck\Cron\CardDescriptionActivity</job>
|
||||||
|
<job>OCA\Deck\Cron\SessionsCleanup</job>
|
||||||
</background-jobs>
|
</background-jobs>
|
||||||
<repair-steps>
|
<repair-steps>
|
||||||
<live-migration>
|
<live-migration>
|
||||||
|
|||||||
56
lib/Cron/SessionsCleanup.php
Normal file
56
lib/Cron/SessionsCleanup.php
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @copyright Copyright (c) 2022, chandi Langecker (git@chandi.it)
|
||||||
|
*
|
||||||
|
* @license GNU AGPL version 3 or any later version
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Affero General Public License as
|
||||||
|
* published by the Free Software Foundation, either version 3 of the
|
||||||
|
* License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU Affero General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
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');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -64,4 +64,10 @@ class SessionMapper extends QBMapper {
|
|||||||
|
|
||||||
return $this->findEntities($qb);
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -60,6 +60,7 @@ class Version10900Date202206151724222 extends SimpleMigrationStep {
|
|||||||
$table->setPrimaryKey(['id']);
|
$table->setPrimaryKey(['id']);
|
||||||
$table->addIndex(['board_id'], 'deck_session_board_id_idx');
|
$table->addIndex(['board_id'], 'deck_session_board_id_idx');
|
||||||
$table->addIndex(['token'], 'deck_session_token_idx');
|
$table->addIndex(['token'], 'deck_session_token_idx');
|
||||||
|
$table->addIndex(['last_contact'], 'deck_session_last_contact_idx');
|
||||||
}
|
}
|
||||||
return $schema;
|
return $schema;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -87,6 +87,10 @@ class SessionService {
|
|||||||
$this->eventDispatcher->dispatchTyped(new SessionClosedEvent($boardId, $this->userId));
|
$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) {
|
public function notifyAllSessions(IQueue $queue, int $boardId, $event, $excludeUserId, $body) {
|
||||||
$activeSessions = $this->sessionMapper->findAllActive($boardId);
|
$activeSessions = $this->sessionMapper->findAllActive($boardId);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user