|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * @copyright Copyright (c) 2018 Arthur Schiwon <[email protected]> |
| 7 | + * |
| 8 | + * @author Arthur Schiwon <[email protected]> |
| 9 | + * @author Christoph Wurst <[email protected]> |
| 10 | + * @author Roeland Jago Douma <[email protected]> |
| 11 | + * |
| 12 | + * @license GNU AGPL version 3 or any later version |
| 13 | + * |
| 14 | + * This program is free software: you can redistribute it and/or modify |
| 15 | + * it under the terms of the GNU Affero General Public License as |
| 16 | + * published by the Free Software Foundation, either version 3 of the |
| 17 | + * License, or (at your option) any later version. |
| 18 | + * |
| 19 | + * This program is distributed in the hope that it will be useful, |
| 20 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | + * GNU Affero General Public License for more details. |
| 23 | + * |
| 24 | + * You should have received a copy of the GNU Affero General Public License |
| 25 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 26 | + * |
| 27 | + */ |
| 28 | +namespace OCA\LogReader\BackgroundJobs; |
| 29 | + |
| 30 | +use OCP\AppFramework\Utility\ITimeFactory; |
| 31 | +use OCP\BackgroundJob\TimedJob; |
| 32 | +use OCP\Files\IAppData; |
| 33 | +use OCP\Files\NotFoundException; |
| 34 | +use OCP\IConfig; |
| 35 | + |
| 36 | +class Rotate extends TimedJob { |
| 37 | + |
| 38 | + public function __construct( |
| 39 | + ITimeFactory $time, |
| 40 | + private IConfig $config, |
| 41 | + private IAppData $appData, |
| 42 | + ) { |
| 43 | + parent::__construct($time); |
| 44 | + |
| 45 | + $this->setInterval(60 * 60 * 3); |
| 46 | + } |
| 47 | + |
| 48 | + protected function run($argument): void { |
| 49 | + try { |
| 50 | + $folder = $this->appData->getFolder('logreader'); |
| 51 | + $file = $folder->getFile('nextcloud.log'); |
| 52 | + } catch (NotFoundException $e) { |
| 53 | + // Nothing to do if there is no log |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + $maxSize = $this->config->getSystemValue('log_rotate_size', 100 * 1024 * 1024); |
| 58 | + if ($file->getSize() < $maxSize) { |
| 59 | + // nothing to do |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + if ($folder->fileExists('nextcloud.1.log')) { |
| 64 | + $rotatedFile = $folder->getFile('nextcloud.1.log'); |
| 65 | + } else { |
| 66 | + $rotatedFile = $folder->newFile('nextcloud.1.log'); |
| 67 | + } |
| 68 | + $rotatedFile->putContent($file->getContent()); |
| 69 | + $file->putContent(''); |
| 70 | + } |
| 71 | +} |
0 commit comments