diff --git a/README.md b/README.md index 99e275a..23e585c 100644 --- a/README.md +++ b/README.md @@ -130,14 +130,14 @@ if ($process->getStatus() == ProcessManagerBundle::STATUS_STOPPING) { You can execute a cleanup command from the console to delete old process entries and log files. To do this on a regular basis, you can add it as a cronjob. ```bash -# delete all process lo entries from the database and log files older than 604800 seconds (7 days) +# delete all process entries from the database and log files older than 604800 seconds (7 days) $ ./bin/console process-manager:cleanup-process-data -# delete all process lo entries from the database and log files older than 86400 seconds (1 days) +# delete all process entries from the database and log files older than 86400 seconds (1 days) $ ./bin/console process-manager:cleanup-process-data --seconds=86400 -# delete only process lo entries from the database older than 604800 seconds (7 days) and keep the log files -$ ./bin/console process-manager:cleanup-process-data --logfiles=false +# delete only process entries from the database older than 604800 seconds (7 days) and keep the log files +$ ./bin/console process-manager:cleanup-process-data --keeplogs ``` ## Copyright and license diff --git a/src/ProcessManagerBundle/Command/CleanupProcessDataCommand.php b/src/ProcessManagerBundle/Command/CleanupProcessDataCommand.php index f2af5bf..f40fba9 100644 --- a/src/ProcessManagerBundle/Command/CleanupProcessDataCommand.php +++ b/src/ProcessManagerBundle/Command/CleanupProcessDataCommand.php @@ -42,11 +42,10 @@ protected function configure(): void EOT ) ->addOption( - 'logfiles', - 'l', - InputOption::VALUE_OPTIONAL, - 'Cleanup log files (default "true")', - true + 'keeplogs', + 'k', + InputOption::VALUE_NONE, + 'Keep log files', ) ->addOption( 'seconds', @@ -67,9 +66,7 @@ protected function configure(): void */ public function execute(InputInterface $input, OutputInterface $output): int { - if ($input->getOption('logfiles')) { - $included = (bool)$input->getOption('logfiles'); - } + $keepLogs = $input->getOption('keeplogs'); if ($input->getOption('seconds')) { $seconds = (int)$input->getOption('seconds'); } @@ -81,20 +78,21 @@ public function execute(InputInterface $input, OutputInterface $output): int $this->output->writeln('finish cleaning database entries older than ' . $seconds . ' seconds'); // start deleting log files older than x seconds - $this->output->writeln('start cleaning log files older than ' . $seconds . ' seconds'); - if (is_dir($this->logDirectory)) { + if (!$keepLogs && is_dir($this->logDirectory)) { + $this->output->writeln('start cleaning log files older than ' . $seconds . ' seconds'); $files = scandir($this->logDirectory); foreach ($files as $file) { + $filePath = $this->logDirectory . '/' . $file; if ( - file_exists($file) && + file_exists($filePath) && str_contains($file, 'process_manager_') && - filemtime($file) < time() - $seconds + filemtime($filePath) < time() - $seconds ) { - unlink($file); + unlink($filePath); } } + $this->output->writeln('finish cleaning logfile entries older than ' . $seconds . ' seconds'); } - $this->output->writeln('finish cleaning logfile entries older than ' . $seconds . ' seconds'); return Command::SUCCESS; } } diff --git a/src/ProcessManagerBundle/Controller/ProcessController.php b/src/ProcessManagerBundle/Controller/ProcessController.php index 9aeea3e..dc4282d 100644 --- a/src/ProcessManagerBundle/Controller/ProcessController.php +++ b/src/ProcessManagerBundle/Controller/ProcessController.php @@ -113,16 +113,17 @@ public function clearAction(Request $request): JsonResponse $connection->executeStatement('DELETE FROM process_manager_processes WHERE started < UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL ? SECOND))', [$seconds]); $logDirectory = \Pimcore::getContainer()->getParameter('process_manager.log_directory'); - $cleanupLogDirectory = \Pimcore::getContainer()->getParameter('process_manager.cleanup_log_directory'); - if ($cleanupLogDirectory && is_dir($logDirectory)) { + $keepLogs = \Pimcore::getContainer()->getParameter('process_manager.keep_logs'); + if (!$keepLogs && is_dir($logDirectory)) { $files = scandir($logDirectory); foreach ($files as $file) { + $filePath = $logDirectory . '/' . $file; if ( - file_exists($file) && + file_exists($filePath) && str_contains($file, 'process_manager_') && - filemtime($file) < time() - $seconds + filemtime($filePath) < time() - $seconds ) { - unlink($file); + unlink($filePath); } } } diff --git a/src/ProcessManagerBundle/DependencyInjection/Configuration.php b/src/ProcessManagerBundle/DependencyInjection/Configuration.php index 65c154f..d7eb121 100644 --- a/src/ProcessManagerBundle/DependencyInjection/Configuration.php +++ b/src/ProcessManagerBundle/DependencyInjection/Configuration.php @@ -42,7 +42,7 @@ public function getConfigTreeBuilder(): TreeBuilder ->children() ->scalarNode('driver')->defaultValue(CoreShopResourceBundle::DRIVER_PIMCORE)->end() ->scalarNode('log_directory')->defaultValue('%kernel.logs_dir%')->end() - ->booleanNode('cleanup_log_directory')->defaultValue(true)->end() + ->booleanNode('keep_logs')->defaultValue(false)->end() ->end() ; diff --git a/src/ProcessManagerBundle/DependencyInjection/ProcessManagerExtension.php b/src/ProcessManagerBundle/DependencyInjection/ProcessManagerExtension.php index e8b2aa9..2536347 100644 --- a/src/ProcessManagerBundle/DependencyInjection/ProcessManagerExtension.php +++ b/src/ProcessManagerBundle/DependencyInjection/ProcessManagerExtension.php @@ -30,7 +30,7 @@ public function load(array $configs, ContainerBuilder $container): void $this->registerPimcoreResources('process_manager', $config['pimcore_admin'], $container); $container->setParameter('process_manager.log_directory', $config['log_directory']); - $container->setParameter('process_manager.cleanup_log_directory', $config['cleanup_log_directory']); + $container->setParameter('process_manager.keep_logs', $config['keep_logs']); $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); $loader->load('services.yml'); diff --git a/src/ProcessManagerBundle/Logger/DefaultHandlerFactory.php b/src/ProcessManagerBundle/Logger/DefaultHandlerFactory.php index 01b527c..727ddf0 100644 --- a/src/ProcessManagerBundle/Logger/DefaultHandlerFactory.php +++ b/src/ProcessManagerBundle/Logger/DefaultHandlerFactory.php @@ -20,12 +20,12 @@ class DefaultHandlerFactory implements HandlerFactoryInterface { private string $logDirectory; - private bool $cleanup_log_directory; + private bool $keepLogs; - public function __construct(string $logDirectory, bool $cleanup_log_directory) + public function __construct(string $logDirectory, bool $keepLogs) { $this->logDirectory = $logDirectory; - $this->cleanup_log_directory = $cleanup_log_directory; + $this->keepLogs = $keepLogs; } public function getLogHandler(ProcessInterface $process): StreamHandler @@ -48,7 +48,7 @@ public function cleanup(ProcessInterface $process): void { $path = sprintf('%s/process_manager_%s.log', $this->logDirectory, $process->getId()); - if ($this->cleanup_log_directory && file_exists($path)) { + if (!$this->keepLogs && file_exists($path)) { unlink($path); } } diff --git a/src/ProcessManagerBundle/Resources/config/services.yml b/src/ProcessManagerBundle/Resources/config/services.yml index 6639754..0777eb1 100644 --- a/src/ProcessManagerBundle/Resources/config/services.yml +++ b/src/ProcessManagerBundle/Resources/config/services.yml @@ -70,7 +70,7 @@ services: ProcessManagerBundle\Logger\DefaultHandlerFactory: arguments: - '%process_manager.log_directory%' - - '%process_manager.cleanup_log_directory%' + - '%process_manager.keep_logs%' ProcessManagerBundle\Report\DefaultReport: ~