diff options
author | Julius Härtl <jus@bitgrid.net> | 2019-06-18 00:10:26 +0300 |
---|---|---|
committer | Julius Härtl <jus@bitgrid.net> | 2019-06-18 00:10:26 +0300 |
commit | da4be5e6d5c872e7dd2e29e5a838f3f2c4520630 (patch) | |
tree | 0c8ca5c151412f53e7870e7908cf2339a3ca5598 /lib | |
parent | 7e74e6d41cfb8efe25a7f666ecb592eac9784060 (diff) |
Add background job to cleanup
Signed-off-by: Julius Härtl <jus@bitgrid.net>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Cron/Cleanup.php | 78 | ||||
-rw-r--r-- | lib/Db/SessionMapper.php | 16 | ||||
-rw-r--r-- | lib/Service/SessionService.php | 6 |
3 files changed, 93 insertions, 7 deletions
diff --git a/lib/Cron/Cleanup.php b/lib/Cron/Cleanup.php new file mode 100644 index 000000000..e32118388 --- /dev/null +++ b/lib/Cron/Cleanup.php @@ -0,0 +1,78 @@ +<?php +declare(strict_types=1); + +/** + * @copyright Copyright (c) 2019 Julius Härtl <jus@bitgrid.net> + * + * @author Julius Härtl <jus@bitgrid.net> + * + * @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\Text\Cron; + +use OC\BackgroundJob\TimedJob; +use OCA\Text\Db\Session; +use OCA\Text\DocumentHasUnsavedChangesException; +use OCA\Text\Service\DocumentService; +use OCA\Text\Service\SessionService; +use OCP\AppFramework\Utility\ITimeFactory; +use OCP\BackgroundJob\Job; +use OCP\ILogger; + +/** + * Class Cache + * + * @package OCA\Social\Cron + */ +class Cleanup extends TimedJob { + + private $sessionService; + private $documentService; + private $logger; + + + public function __construct(ITimeFactory $time, SessionService $sessionService, DocumentService $documentService, ILogger $logger) { + parent::__construct($time); + $this->sessionService = $sessionService; + $this->documentService = $documentService; + $this->logger = $logger; + $this->setInterval(5 * 60); + } + + protected function run($argument) { + $this->logger->debug('Run cleanup job for text sessions'); + $inactive = $this->sessionService->findAllInactive(); + /** @var Session $session */ + foreach ($inactive as $session) { + try { + $this->logger->debug('Resetting document ' . $session->getDocumentId() . ''); + $this->documentService->resetDocument($session->getDocumentId()); + $this->logger->debug('Resetting document ' . $session->getDocumentId() . ''); + } catch (DocumentHasUnsavedChangesException $e) { + $this->logger->info('Document ' . $session->getDocumentId() . ' has not been reset, as it has unsaved changes'); + } + } + $removedSessions = $this->sessionService->removeInactiveSessions(null); + $this->logger->info('Removed ' . $removedSessions . ' inactive sessions'); + } + + + +} diff --git a/lib/Db/SessionMapper.php b/lib/Db/SessionMapper.php index fc1b7fa79..0bef02a2e 100644 --- a/lib/Db/SessionMapper.php +++ b/lib/Db/SessionMapper.php @@ -82,19 +82,23 @@ class SessionMapper extends QBMapper { $qb = $this->db->getQueryBuilder(); $qb->select('id','color','document_id', 'last_contact','user_id','guest_name') ->from($this->getTableName()) - ->where($qb->expr()->gt('last_contact', $qb->createNamedParameter(time()-SessionService::SESSION_VALID_TIME))) + ->where($qb->expr()->lt('last_contact', $qb->createNamedParameter(time()-SessionService::SESSION_VALID_TIME))) ->execute(); return $this->findEntities($qb); } - public function deleteInactive($documentId) { + public function deleteInactive($documentId = -1) { /* @var $qb IQueryBuilder */ $qb = $this->db->getQueryBuilder(); - return $qb->delete($this->getTableName()) - ->where($qb->expr()->eq('document_id', $qb->createNamedParameter($documentId))) - ->andWhere($qb->expr()->lt('last_contact', $qb->createNamedParameter(time()-SessionService::SESSION_VALID_TIME))) - ->execute(); + $qb->delete($this->getTableName()); + if ($documentId === null) { + $qb->where($qb->expr()->lt('last_contact', $qb->createNamedParameter(time()-SessionService::SESSION_VALID_TIME))); + } else { + $qb->where($qb->expr()->eq('document_id', $qb->createNamedParameter($documentId))) + ->andWhere($qb->expr()->lt('last_contact', $qb->createNamedParameter(time()-SessionService::SESSION_VALID_TIME))); + } + return $qb->execute(); } } diff --git a/lib/Service/SessionService.php b/lib/Service/SessionService.php index b4eb393dd..b7be301ef 100644 --- a/lib/Service/SessionService.php +++ b/lib/Service/SessionService.php @@ -88,7 +88,11 @@ class SessionService { }, $sessions); } - public function removeInactiveSessions($documentId) { + public function findAllInactive() { + return $this->sessionMapper->findAllInactive(); + } + + public function removeInactiveSessions($documentId = -1) { return $this->sessionMapper->deleteInactive($documentId); } |