Add a janitor job to tidy log and watch tables from old entries

Signed-off-by: dartcafe <github@dartcafe.de>
This commit is contained in:
dartcafe 2021-05-02 18:48:03 +02:00
Родитель c53aabf354
Коммит 29a7a51136
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: CCE73CEF3035D3C8
4 изменённых файлов: 95 добавлений и 0 удалений

Просмотреть файл

@ -27,6 +27,7 @@
</dependencies>
<background-jobs>
<job>OCA\Polls\Cron\NotificationCron</job>
<job>OCA\Polls\Cron\JanitorCron</job>
</background-jobs>
<commands>
<command>OCA\Polls\Command\Share\Add</command>

55
lib/Cron/JanitorCron.php Normal file
Просмотреть файл

@ -0,0 +1,55 @@
<?php
/**
* @copyright Copyright (c) 2017 Vinzenz Rosenkranz <vinzenz.rosenkranz@gmail.com>
*
* @author René Gieling <github@dartcafe.de>
*
* @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\Polls\Cron;
use OCP\BackgroundJob\TimedJob;
use OCP\AppFramework\Utility\ITimeFactory;
use OCA\Polls\Db\LogMapper;
use OCA\Polls\Db\WatchMapper;
class JanitorCron extends TimedJob {
/** @var LogMapper */
private $logMapper;
/** @var WatchMapper */
private $watchMapper;
public function __construct(
ITimeFactory $time,
LogMapper $logMapper,
WatchMapper $watchMapper
) {
parent::__construct($time);
$this->logMapper = $logMapper;
$this->watchMapper = $watchMapper;
parent::setInterval(86400); // run once a day
}
protected function run($arguments) {
$this->logMapper->deleteProcessedEntries(); // delete processed log entries
$this->logMapper->deleteOldEntries(time() - (86400 * 7)); // delete entries older than 7 days
$this->watchMapper->deleteOldEntries(time() - 86400); // delete entries older than 1 day
}
}

Просмотреть файл

@ -158,4 +158,30 @@ class LogMapper extends QBMapper {
->setParameter('userId', $userId);
$query->execute();
}
/**
* Delete entries per timestamp
* @return void
*/
public function deleteOldEntries(int $offset): void {
$query = $this->db->getQueryBuilder();
$query->delete($this->getTableName())
->where(
$query->expr()->lt('created', $query->createNamedParameter($offset))
);
$query->execute();
}
/**
* Delete processed entries
* @return void
*/
public function deleteProcessedEntries(): void {
$query = $this->db->getQueryBuilder();
$query->delete($this->getTableName())
->where(
$query->expr()->gt('processed', $query->createNamedParameter(0))
);
$query->execute();
}
}

Просмотреть файл

@ -70,4 +70,17 @@ class WatchMapper extends QBMapper {
return $this->findEntity($qb);
}
/**
* Delete entries per timestamp
* @return void
*/
public function deleteOldEntries(int $offset): void {
$query = $this->db->getQueryBuilder();
$query->delete($this->getTableName())
->where(
$query->expr()->lt('updated', $query->createNamedParameter($offset))
);
$query->execute();
}
}