Skip to content

Commit

Permalink
feat: Add cleanup maintenance command
Browse files Browse the repository at this point in the history
  • Loading branch information
mugge6 committed Jun 2, 2023
1 parent fe8b226 commit 65cc869
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 30 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 12 additions & 14 deletions src/ProcessManagerBundle/Command/CleanupProcessDataCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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');
}
Expand All @@ -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;
}
}
11 changes: 6 additions & 5 deletions src/ProcessManagerBundle/Controller/ProcessController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
8 changes: 4 additions & 4 deletions src/ProcessManagerBundle/Logger/DefaultHandlerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/ProcessManagerBundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: ~

Expand Down

0 comments on commit 65cc869

Please sign in to comment.