diff --git a/README.md b/README.md index 3fac68b..99e275a 100644 --- a/README.md +++ b/README.md @@ -125,6 +125,20 @@ if ($process->getStatus() == ProcessManagerBundle::STATUS_STOPPING) { $process->save(); } ``` +## Cleanup command + +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) +$ ./bin/console process-manager:cleanup-process-data + +# delete all process lo 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 +``` ## Copyright and license Copyright: [lineofcode.at](http://www.lineofcode.at) diff --git a/src/ProcessManagerBundle/Command/CleanupProcessDataCommand.php b/src/ProcessManagerBundle/Command/CleanupProcessDataCommand.php new file mode 100644 index 0000000..f2af5bf --- /dev/null +++ b/src/ProcessManagerBundle/Command/CleanupProcessDataCommand.php @@ -0,0 +1,100 @@ +logDirectory = $logDirectory; + } + protected function configure(): void + { + $this + ->setName('process-manager:cleanup-process-data') + ->setDescription('Cleanup process data from the database and from log file directory') + ->setHelp( + <<%command.name% cleanup process data from the database and from log file directory. +EOT + ) + ->addOption( + 'logfiles', + 'l', + InputOption::VALUE_OPTIONAL, + 'Cleanup log files (default "true")', + true + ) + ->addOption( + 'seconds', + 's', + InputOption::VALUE_OPTIONAL, + 'Cleanup process data older than this number of seconds (default "604800" - 7 days)', + 604800 + ); + } + + /** + * + * @param InputInterface $input + * @param OutputInterface $output + * + * @return int + * @throws Exception + */ + public function execute(InputInterface $input, OutputInterface $output): int + { + if ($input->getOption('logfiles')) { + $included = (bool)$input->getOption('logfiles'); + } + if ($input->getOption('seconds')) { + $seconds = (int)$input->getOption('seconds'); + } + + // start deleting database entries older than x seconds + $this->output->writeln('start cleaning database entries older than ' . $seconds . ' seconds'); + $connection = Db::get(); + $connection->executeStatement('DELETE FROM process_manager_processes WHERE started < UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL ? SECOND))', [$seconds]); + $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)) { + $files = scandir($this->logDirectory); + foreach ($files as $file) { + if ( + file_exists($file) && + str_contains($file, 'process_manager_') && + filemtime($file) < time() - $seconds + ) { + unlink($file); + } + } + } + $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 382ebb1..9aeea3e 100644 --- a/src/ProcessManagerBundle/Controller/ProcessController.php +++ b/src/ProcessManagerBundle/Controller/ProcessController.php @@ -112,6 +112,21 @@ public function clearAction(Request $request): JsonResponse $connection = Db::get(); $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)) { + $files = scandir($logDirectory); + foreach ($files as $file) { + if ( + file_exists($file) && + str_contains($file, 'process_manager_') && + filemtime($file) < time() - $seconds + ) { + unlink($file); + } + } + } + return $this->json(['success' => true]); } diff --git a/src/ProcessManagerBundle/DependencyInjection/Configuration.php b/src/ProcessManagerBundle/DependencyInjection/Configuration.php index 21f77f2..65c154f 100644 --- a/src/ProcessManagerBundle/DependencyInjection/Configuration.php +++ b/src/ProcessManagerBundle/DependencyInjection/Configuration.php @@ -42,6 +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() ->end() ; diff --git a/src/ProcessManagerBundle/DependencyInjection/ProcessManagerExtension.php b/src/ProcessManagerBundle/DependencyInjection/ProcessManagerExtension.php index 45b68b4..e8b2aa9 100644 --- a/src/ProcessManagerBundle/DependencyInjection/ProcessManagerExtension.php +++ b/src/ProcessManagerBundle/DependencyInjection/ProcessManagerExtension.php @@ -30,6 +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']); $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 47badbf..01b527c 100644 --- a/src/ProcessManagerBundle/Logger/DefaultHandlerFactory.php +++ b/src/ProcessManagerBundle/Logger/DefaultHandlerFactory.php @@ -20,10 +20,12 @@ class DefaultHandlerFactory implements HandlerFactoryInterface { private string $logDirectory; + private bool $cleanup_log_directory; - public function __construct(string $logDirectory) + public function __construct(string $logDirectory, bool $cleanup_log_directory) { $this->logDirectory = $logDirectory; + $this->cleanup_log_directory = $cleanup_log_directory; } public function getLogHandler(ProcessInterface $process): StreamHandler @@ -46,7 +48,7 @@ public function cleanup(ProcessInterface $process): void { $path = sprintf('%s/process_manager_%s.log', $this->logDirectory, $process->getId()); - if (file_exists($path)) { + if ($this->cleanup_log_directory && file_exists($path)) { unlink($path); } } diff --git a/src/ProcessManagerBundle/Resources/config/services.yml b/src/ProcessManagerBundle/Resources/config/services.yml index eda60fb..6639754 100644 --- a/src/ProcessManagerBundle/Resources/config/services.yml +++ b/src/ProcessManagerBundle/Resources/config/services.yml @@ -1,6 +1,7 @@ imports: - { resource: "services/forms.yml" } - { resource: "services/installer.yml" } + - { resource: "services/commands.yml" } services: _defaults: @@ -69,6 +70,7 @@ services: ProcessManagerBundle\Logger\DefaultHandlerFactory: arguments: - '%process_manager.log_directory%' + - '%process_manager.cleanup_log_directory%' ProcessManagerBundle\Report\DefaultReport: ~ diff --git a/src/ProcessManagerBundle/Resources/config/services/commands.yml b/src/ProcessManagerBundle/Resources/config/services/commands.yml new file mode 100644 index 0000000..edf077f --- /dev/null +++ b/src/ProcessManagerBundle/Resources/config/services/commands.yml @@ -0,0 +1,6 @@ +services: + ProcessManagerBundle\Command\CleanupProcessDataCommand: + arguments: + - '%process_manager.log_directory%' + tags: + - { name: 'console.command', command: 'process-manager:cleanup-process-data' }