|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Process Manager. |
| 5 | + * |
| 6 | + * LICENSE |
| 7 | + * |
| 8 | + * This source file is subject to the GNU General Public License version 3 (GPLv3) |
| 9 | + * For the full copyright and license information, please view the LICENSE.md and gpl-3.0.txt |
| 10 | + * files that are distributed with this source code. |
| 11 | + * |
| 12 | + * @copyright Copyright (c) 2015-2020 Wojciech Peisert (http://divante.co/) |
| 13 | + * @license https://github.com/dpfaffenbauer/ProcessManager/blob/master/gpl-3.0.txt GNU General Public License version 3 (GPLv3) |
| 14 | + */ |
| 15 | + |
| 16 | +namespace ProcessManagerBundle\Command; |
| 17 | + |
| 18 | +use Doctrine\DBAL\Exception; |
| 19 | +use Pimcore\Console\AbstractCommand; |
| 20 | +use ProcessManagerBundle\Service\CleanupService; |
| 21 | +use Symfony\Component\Console\Command\Command; |
| 22 | +use Symfony\Component\Console\Input\InputInterface; |
| 23 | +use Symfony\Component\Console\Input\InputOption; |
| 24 | +use Symfony\Component\Console\Output\OutputInterface; |
| 25 | + |
| 26 | +class CleanupProcessDataCommand extends AbstractCommand |
| 27 | +{ |
| 28 | + public function __construct(private CleanupService $cleanupService, private string $logDirectory) { |
| 29 | + parent::__construct(); |
| 30 | + } |
| 31 | + |
| 32 | + protected function configure(): void |
| 33 | + { |
| 34 | + $this |
| 35 | + ->setName('process-manager:cleanup-process-data') |
| 36 | + ->setDescription('Cleanup process data from the database and from log file directory') |
| 37 | + ->setHelp( |
| 38 | + <<<EOT |
| 39 | +The <info>%command.name%</info> cleanup process data from the database and from log file directory. |
| 40 | +EOT |
| 41 | + ) |
| 42 | + ->addOption( |
| 43 | + 'keeplogs', |
| 44 | + 'k', |
| 45 | + InputOption::VALUE_NONE, |
| 46 | + 'Keep log files', |
| 47 | + ) |
| 48 | + ->addOption( |
| 49 | + 'seconds', |
| 50 | + 's', |
| 51 | + InputOption::VALUE_OPTIONAL, |
| 52 | + 'Cleanup process data older than this number of seconds (default "604800" - 7 days)', |
| 53 | + 604800 |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * |
| 59 | + * @param InputInterface $input |
| 60 | + * @param OutputInterface $output |
| 61 | + * |
| 62 | + * @return int |
| 63 | + * @throws Exception |
| 64 | + */ |
| 65 | + public function execute(InputInterface $input, OutputInterface $output): int |
| 66 | + { |
| 67 | + $keepLogs = $input->getOption('keeplogs'); |
| 68 | + if ($input->getOption('seconds')) { |
| 69 | + $seconds = (int)$input->getOption('seconds'); |
| 70 | + } |
| 71 | + |
| 72 | + // start deleting database entries older than x seconds |
| 73 | + $output->writeln('start cleaning database entries older than ' . $seconds . ' seconds'); |
| 74 | + $this->cleanupService->cleanupDbEntries($seconds); |
| 75 | + $output->writeln('finish cleaning database entries older than ' . $seconds . ' seconds'); |
| 76 | + |
| 77 | + // start deleting log files older than x seconds |
| 78 | + $output->writeln('start cleaning log files older than ' . $seconds . ' seconds'); |
| 79 | + $this->cleanupService->cleanupLogFiles($this->logDirectory, $seconds, $keepLogs); |
| 80 | + $output->writeln('finish cleaning logfile entries older than ' . $seconds . ' seconds'); |
| 81 | + return Command::SUCCESS; |
| 82 | + } |
| 83 | +} |
0 commit comments