Skip to content

Commit 20ebc57

Browse files
Merge pull request #91 from CandoImage/cleanup-maintenance-task
Cleanup maintenance task
2 parents 75b76b0 + c274939 commit 20ebc57

File tree

11 files changed

+260
-7
lines changed

11 files changed

+260
-7
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,20 @@ if ($process->getStatus() == ProcessManagerBundle::STATUS_STOPPING) {
125125
$process->save();
126126
}
127127
```
128+
## Cleanup command
129+
130+
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.
131+
132+
```bash
133+
# delete all process entries from the database and log files older than 604800 seconds (7 days)
134+
$ ./bin/console process-manager:cleanup-process-data
135+
136+
# delete all process entries from the database and log files older than 86400 seconds (1 days)
137+
$ ./bin/console process-manager:cleanup-process-data --seconds=86400
138+
139+
# delete only process entries from the database older than 604800 seconds (7 days) and keep the log files
140+
$ ./bin/console process-manager:cleanup-process-data --keeplogs
141+
```
128142

129143
## Copyright and license
130144
Copyright: [lineofcode.at](http://www.lineofcode.at)
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+
}

src/ProcessManagerBundle/Controller/ProcessController.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Pimcore\Db;
1919
use ProcessManagerBundle\Model\Process;
2020
use ProcessManagerBundle\Model\ProcessInterface;
21+
use ProcessManagerBundle\Service\CleanupService;
2122
use Symfony\Component\HttpFoundation\JsonResponse;
2223
use Symfony\Component\HttpFoundation\Request;
2324
use Symfony\Component\HttpFoundation\Response;
@@ -34,6 +35,17 @@ public function listAction(Request $request): JsonResponse
3435
* @var Process\Listing $list
3536
*/
3637
$list = new $listingClass();
38+
if ($filterString = $request->get('filter')) {
39+
$db = Db::get();
40+
$filters = json_decode($filterString);
41+
$conditionParts = [];
42+
foreach ($filters as $f) {
43+
$fieldname = $f->property;
44+
$conditionParts[] = $db->quoteIdentifier($fieldname) . ' LIKE ' . $db->quote('%' . $f->value . '%');
45+
}
46+
$condition = implode(' AND ', $conditionParts);
47+
$list->setCondition($condition);
48+
}
3749
if ($sort = $request->get('sort')) {
3850
$sort = json_decode($sort)[0];
3951
$list->setOrderKey($sort->property);
@@ -109,9 +121,13 @@ public function stopAction(Request $request): JsonResponse
109121
public function clearAction(Request $request): JsonResponse
110122
{
111123
$seconds = (int)$request->get('seconds', 604_800);
112-
$connection = Db::get();
113-
$connection->executeStatement('DELETE FROM process_manager_processes WHERE started < UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL ? SECOND))', [$seconds]);
124+
$logDirectory = $this->container->getParameter('process_manager.log_directory');
125+
$keepLogs = $this->container->getParameter('process_manager.keep_logs');
114126

127+
/** @var CleanupService $cleanupService */
128+
$cleanupService = $this->container->get(CleanupService::class);
129+
$cleanupService->cleanupDbEntries($seconds);
130+
$cleanupService->cleanupLogFiles($logDirectory, $seconds, $keepLogs);
115131
return $this->json(['success' => true]);
116132
}
117133

src/ProcessManagerBundle/DependencyInjection/Configuration.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ public function getConfigTreeBuilder(): TreeBuilder
4242
->children()
4343
->scalarNode('driver')->defaultValue(CoreShopResourceBundle::DRIVER_PIMCORE)->end()
4444
->scalarNode('log_directory')->defaultValue('%kernel.logs_dir%')->end()
45+
->booleanNode('keep_logs')->defaultValue(true)->end()
46+
->integerNode('seconds')->defaultValue(604800)->end()
4547
->end()
4648
;
4749

src/ProcessManagerBundle/DependencyInjection/ProcessManagerExtension.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ public function load(array $configs, ContainerBuilder $container): void
3030
$this->registerPimcoreResources('process_manager', $config['pimcore_admin'], $container);
3131

3232
$container->setParameter('process_manager.log_directory', $config['log_directory']);
33+
$container->setParameter('process_manager.keep_logs', $config['keep_logs']);
34+
$container->setParameter('process_manager.seconds', $config['seconds']);
3335

3436
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
3537
$loader->load('services.yml');

src/ProcessManagerBundle/Logger/DefaultHandlerFactory.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@
2020
class DefaultHandlerFactory implements HandlerFactoryInterface
2121
{
2222
private string $logDirectory;
23+
private bool $keepLogs;
2324

24-
public function __construct(string $logDirectory)
25+
public function __construct(string $logDirectory, bool $keepLogs)
2526
{
2627
$this->logDirectory = $logDirectory;
28+
$this->keepLogs = $keepLogs;
2729
}
2830

2931
public function getLogHandler(ProcessInterface $process): StreamHandler
@@ -46,7 +48,7 @@ public function cleanup(ProcessInterface $process): void
4648
{
4749
$path = sprintf('%s/process_manager_%s.log', $this->logDirectory, $process->getId());
4850

49-
if (file_exists($path)) {
51+
if (!$this->keepLogs && file_exists($path)) {
5052
unlink($path);
5153
}
5254
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
/**
3+
* Process Manager.
4+
*
5+
* LICENSE
6+
*
7+
* This source file is subject to the GNU General Public License version 3 (GPLv3)
8+
* For the full copyright and license information, please view the LICENSE.md and gpl-3.0.txt
9+
* files that are distributed with this source code.
10+
*
11+
* @copyright Copyright (c) 2018 Jakub Płaskonka (jplaskonka@divante.pl)
12+
* @license https://github.com/dpfaffenbauer/ProcessManager/blob/master/gpl-3.0.txt GNU General Public License version 3 (GPLv3)
13+
*/
14+
15+
namespace ProcessManagerBundle\Maintenance;
16+
17+
use Pimcore\Maintenance\TaskInterface;
18+
use ProcessManagerBundle\Service\CleanupService;
19+
20+
class CleanupTask implements TaskInterface
21+
{
22+
public function __construct(private CleanupService $cleanupService, private string $logDirectory, private int $seconds, private bool $keepLogs) {
23+
}
24+
public function execute(): void
25+
{
26+
$this->cleanupService->cleanupDbEntries($this->seconds);
27+
$this->cleanupService->cleanupLogFiles($this->logDirectory, $this->seconds, $this->keepLogs);
28+
}
29+
}

src/ProcessManagerBundle/Resources/config/services.yml

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
imports:
22
- { resource: "services/forms.yml" }
33
- { resource: "services/installer.yml" }
4+
- { resource: "services/commands.yml" }
45

56
services:
67
_defaults:
@@ -44,14 +45,23 @@ services:
4445
tags:
4546
- { name: kernel.event_listener, event: pimcore.asset.postDelete, method: onArtifactAssetDelete }
4647

47-
### Maintenance task
48+
### Maintenance tasks
4849
ProcessManagerBundle\Maintenance\CronTask:
4950
arguments:
5051
- '@process_manager.registry.processes'
5152
- '@messenger.default_bus'
5253
tags:
5354
- { name: pimcore.maintenance.task, type: process_manager.maintenance.cron }
5455

56+
ProcessManagerBundle\Maintenance\CleanupTask:
57+
arguments:
58+
- '@ProcessManagerBundle\Service\CleanupService'
59+
- '%process_manager.log_directory%'
60+
- '%process_manager.seconds%'
61+
- '%process_manager.keep_logs%'
62+
tags:
63+
- { name: pimcore.maintenance.task, type: process_manager.maintenance.cleanup }
64+
5565
ProcessManagerBundle\Logger\ProcessLogger:
5666
arguments:
5767
- '@logger'
@@ -69,6 +79,7 @@ services:
6979
ProcessManagerBundle\Logger\DefaultHandlerFactory:
7080
arguments:
7181
- '%process_manager.log_directory%'
82+
- '%process_manager.keep_logs%'
7283

7384
ProcessManagerBundle\Report\DefaultReport: ~
7485

@@ -95,3 +106,8 @@ services:
95106
- '@process_manager.registry.processes'
96107
tags:
97108
- { name: messenger.message_handler }
109+
110+
### Cleanup service
111+
ProcessManagerBundle\Service\CleanupService:
112+
arguments:
113+
- '@parameter_bag'
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
services:
2+
ProcessManagerBundle\Command\CleanupProcessDataCommand:
3+
arguments:
4+
- '@ProcessManagerBundle\Service\CleanupService'
5+
- '%process_manager.log_directory%'
6+
tags:
7+
- { name: 'console.command', command: 'process-manager:cleanup-process-data' }

src/ProcessManagerBundle/Resources/public/pimcore/js/processes.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ pimcore.plugin.processmanager.processes = Class.create({
145145
xtype: 'grid',
146146
store: store,
147147
bbar: pimcore.helpers.grid.buildDefaultPagingToolbar(store),
148+
plugins: 'gridfilters',
148149
columns: [
149150
{
150151
text: t('id'),
@@ -154,12 +155,14 @@ pimcore.plugin.processmanager.processes = Class.create({
154155
{
155156
text: t('name'),
156157
dataIndex: 'name',
157-
width: 300
158+
width: 400,
159+
filter: 'string'
158160
},
159161
{
160162
text: t('processmanager_message'),
161163
dataIndex: 'message',
162-
flex : 1
164+
flex : 1,
165+
filter: 'string'
163166
},
164167
{
165168
text: t('processmanager_started'),
@@ -273,6 +276,7 @@ pimcore.plugin.processmanager.processes = Class.create({
273276
text : t('processmanager_status'),
274277
width: 100,
275278
dataIndex: 'status',
279+
filter: 'string',
276280
renderer: function (value, metadata, record) {
277281
if (record.data.status != '' && record.data.status != null) {
278282
return t('processmanager_' + record.data.status);

0 commit comments

Comments
 (0)