-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #91 from CandoImage/cleanup-maintenance-task
Cleanup maintenance task
- Loading branch information
Showing
11 changed files
with
260 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
src/ProcessManagerBundle/Command/CleanupProcessDataCommand.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?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 ProcessManagerBundle\Service\CleanupService; | ||
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 | ||
{ | ||
public function __construct(private CleanupService $cleanupService, private string $logDirectory) { | ||
parent::__construct(); | ||
} | ||
|
||
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( | ||
'keeplogs', | ||
'k', | ||
InputOption::VALUE_NONE, | ||
'Keep log files', | ||
) | ||
->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 | ||
{ | ||
$keepLogs = $input->getOption('keeplogs'); | ||
if ($input->getOption('seconds')) { | ||
$seconds = (int)$input->getOption('seconds'); | ||
} | ||
|
||
// start deleting database entries older than x seconds | ||
$output->writeln('start cleaning database entries older than ' . $seconds . ' seconds'); | ||
$this->cleanupService->cleanupDbEntries($seconds); | ||
$output->writeln('finish cleaning database entries older than ' . $seconds . ' seconds'); | ||
|
||
// start deleting log files older than x seconds | ||
$output->writeln('start cleaning log files older than ' . $seconds . ' seconds'); | ||
$this->cleanupService->cleanupLogFiles($this->logDirectory, $seconds, $keepLogs); | ||
$output->writeln('finish cleaning logfile entries older than ' . $seconds . ' seconds'); | ||
return Command::SUCCESS; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?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) 2018 Jakub Płaskonka (jplaskonka@divante.pl) | ||
* @license https://github.com/dpfaffenbauer/ProcessManager/blob/master/gpl-3.0.txt GNU General Public License version 3 (GPLv3) | ||
*/ | ||
|
||
namespace ProcessManagerBundle\Maintenance; | ||
|
||
use Pimcore\Maintenance\TaskInterface; | ||
use ProcessManagerBundle\Service\CleanupService; | ||
|
||
class CleanupTask implements TaskInterface | ||
{ | ||
public function __construct(private CleanupService $cleanupService, private string $logDirectory, private int $seconds, private bool $keepLogs) { | ||
} | ||
public function execute(): void | ||
{ | ||
$this->cleanupService->cleanupDbEntries($this->seconds); | ||
$this->cleanupService->cleanupLogFiles($this->logDirectory, $this->seconds, $this->keepLogs); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
src/ProcessManagerBundle/Resources/config/services/commands.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
services: | ||
ProcessManagerBundle\Command\CleanupProcessDataCommand: | ||
arguments: | ||
- '@ProcessManagerBundle\Service\CleanupService' | ||
- '%process_manager.log_directory%' | ||
tags: | ||
- { name: 'console.command', command: 'process-manager:cleanup-process-data' } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?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 Dominik Pfaffenbauer (https://www.pfaffenbauer.at) | ||
* @license https://github.com/dpfaffenbauer/ProcessManager/blob/master/gpl-3.0.txt GNU General Public License version 3 (GPLv3) | ||
*/ | ||
|
||
namespace ProcessManagerBundle\Service; | ||
|
||
use Doctrine\DBAL\Exception; | ||
use Pimcore\Db; | ||
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; | ||
|
||
class CleanupService | ||
{ | ||
public function __construct(protected ParameterBagInterface $parameterBag) | ||
{ | ||
} | ||
|
||
/** | ||
* Cleanup process db entries from the database | ||
* | ||
* @param int|null $seconds Only entries older than x seconds will be deleted | ||
* None or empty value will delete all entries | ||
* @throws Exception | ||
*/ | ||
public function cleanupDbEntries(?int $seconds): void | ||
{ | ||
// delete all entries if there is no time passed | ||
if (empty($seconds)) { | ||
$seconds = 0; | ||
} | ||
$connection = Db::get(); | ||
$connection->executeStatement('DELETE FROM process_manager_processes WHERE started < UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL ? SECOND))', [$seconds]); | ||
} | ||
|
||
/** | ||
* Cleanup all log files | ||
* | ||
* @param string $logDirectory Path to the log files. If not specified, default path from config will be used | ||
* @param int|null $seconds Only entries older than x seconds will be deleted | ||
* None or empty value will delete all entries | ||
* @param bool $keepLogs Whether to keep the log files or not | ||
* true - Keep the log files | ||
* false - Cleanup the logiles | ||
* @return void | ||
*/ | ||
public function cleanupLogFiles(string $logDirectory, ?int $seconds, bool $keepLogs = true): void | ||
{ | ||
if (empty($logDirectory)) { | ||
$logDirectory = $this->parameterBag->get('process_manager.log_directory'); | ||
} | ||
// delete all entries if there is no time passed | ||
if (empty($seconds)) { | ||
$seconds = 0; | ||
} | ||
if (!$keepLogs && is_dir($logDirectory)) { | ||
$files = scandir($logDirectory); | ||
foreach ($files as $file) { | ||
$filePath = $logDirectory . '/' . $file; | ||
if ( | ||
file_exists($filePath) && | ||
str_contains($file, 'process_manager_') && | ||
filemtime($filePath) < time() - $seconds | ||
) { | ||
unlink($filePath); | ||
} | ||
} | ||
} | ||
} | ||
} |