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 75b76b0 commit fe8b226
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 2 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
100 changes: 100 additions & 0 deletions src/ProcessManagerBundle/Command/CleanupProcessDataCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

/**
* Process Manager.
*
* LICENSE
*
* This source file is subject to the GNU General Public License version 3 (GPLv3)
* For the full copyright and license information, please view the LICENSE.md and gpl-3.0.txt
* files that are distributed with this source code.
*
* @copyright Copyright (c) 2015-2020 Wojciech Peisert (http://divante.co/)
* @license https://github.com/dpfaffenbauer/ProcessManager/blob/master/gpl-3.0.txt GNU General Public License version 3 (GPLv3)
*/

namespace ProcessManagerBundle\Command;

use Doctrine\DBAL\Exception;
use Pimcore\Console\AbstractCommand;
use Pimcore\Db;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class CleanupProcessDataCommand extends AbstractCommand
{
private string $logDirectory;

public function __construct(string $logDirectory) {
parent::__construct();
$this->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(
<<<EOT
The <info>%command.name%</info> 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;
}
}
15 changes: 15 additions & 0 deletions src/ProcessManagerBundle/Controller/ProcessController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
6 changes: 4 additions & 2 deletions src/ProcessManagerBundle/Logger/DefaultHandlerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/ProcessManagerBundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
imports:
- { resource: "services/forms.yml" }
- { resource: "services/installer.yml" }
- { resource: "services/commands.yml" }

services:
_defaults:
Expand Down Expand Up @@ -69,6 +70,7 @@ services:
ProcessManagerBundle\Logger\DefaultHandlerFactory:
arguments:
- '%process_manager.log_directory%'
- '%process_manager.cleanup_log_directory%'

ProcessManagerBundle\Report\DefaultReport: ~

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
services:
ProcessManagerBundle\Command\CleanupProcessDataCommand:
arguments:
- '%process_manager.log_directory%'
tags:
- { name: 'console.command', command: 'process-manager:cleanup-process-data' }

0 comments on commit fe8b226

Please sign in to comment.