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 16, 2023
1 parent 61234a2 commit a7e6d14
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 25 deletions.
15 changes: 5 additions & 10 deletions src/ProcessManagerBundle/Command/CleanupProcessDataCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,8 @@

class CleanupProcessDataCommand extends AbstractCommand
{
private CleanupService $cleanupService;
private string $logDirectory;

public function __construct(CleanupService $cleanupService, string $logDirectory) {
public function __construct(private CleanupService $cleanupService, private string $logDirectory) {
parent::__construct();
$this->cleanupService = $cleanupService;
$this->logDirectory = $logDirectory;
}

protected function configure(): void
Expand Down Expand Up @@ -75,14 +70,14 @@ public function execute(InputInterface $input, OutputInterface $output): int
}

// start deleting database entries older than x seconds
$this->output->writeln('start cleaning database entries older than ' . $seconds . ' seconds');
$output->writeln('start cleaning database entries older than ' . $seconds . ' seconds');
$this->cleanupService->cleanupDbEntries($seconds);
$this->output->writeln('finish cleaning database entries older than ' . $seconds . ' seconds');
$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');
$output->writeln('start cleaning log files older than ' . $seconds . ' seconds');
$this->cleanupService->cleanupLogFiles($this->logDirectory, $seconds, $keepLogs);
$this->output->writeln('finish cleaning logfile entries older than ' . $seconds . ' seconds');
$output->writeln('finish cleaning logfile entries older than ' . $seconds . ' seconds');
return Command::SUCCESS;
}
}
22 changes: 21 additions & 1 deletion src/ProcessManagerBundle/Controller/ProcessController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,16 @@

namespace ProcessManagerBundle\Controller;

use CoreShop\Bundle\ResourceBundle\Controller\EventDispatcherInterface;
use CoreShop\Bundle\ResourceBundle\Controller\ResourceController;
use CoreShop\Bundle\ResourceBundle\Controller\ResourceFormFactoryInterface;
use CoreShop\Bundle\ResourceBundle\Controller\ViewHandler;
use CoreShop\Bundle\ResourceBundle\Form\Helper\ErrorSerializer;
use CoreShop\Component\Resource\Factory\FactoryInterface;
use CoreShop\Component\Resource\Metadata\MetadataInterface;
use CoreShop\Component\Resource\Repository\RepositoryInterface;
use Doctrine\Persistence\ObjectManager;
use Pimcore\Db;
use ProcessManagerBundle\Model\Process;
use ProcessManagerBundle\Model\ProcessInterface;
use ProcessManagerBundle\Service\CleanupService;
Expand All @@ -34,6 +43,17 @@ public function listAction(Request $request): JsonResponse
* @var Process\Listing $list
*/
$list = new $listingClass();
if ($filterString = $request->get('filter')) {
$db = Db::get();
$filters = json_decode($filterString);
$conditionParts = [];
foreach ($filters as $f) {
$fieldname = $f->property;
$conditionParts[] = $db->quoteIdentifier($fieldname) . ' LIKE ' . $db->quote('%' . $f->value . '%');
}
$condition = implode(' AND ', $conditionParts);
$list->setCondition($condition);
}
if ($sort = $request->get('sort')) {
$sort = json_decode($sort)[0];
$list->setOrderKey($sort->property);
Expand Down Expand Up @@ -113,7 +133,7 @@ public function clearAction(Request $request): JsonResponse
$keepLogs = $this->container->getParameter('process_manager.keep_logs');

/** @var CleanupService $cleanupService */
$cleanupService = $this->get('process_manager.cleanup_service');
$cleanupService = $this->container->get('process_manager.cleanup_service');
$cleanupService->cleanupDbEntries($seconds);
$cleanupService->cleanupLogFiles($logDirectory, $seconds, $keepLogs);
return $this->json(['success' => true]);
Expand Down
15 changes: 3 additions & 12 deletions src/ProcessManagerBundle/Maintenance/CleanupTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,14 @@

use Pimcore\Maintenance\TaskInterface;
use ProcessManagerBundle\Service\CleanupService;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;

class CleanupTask implements TaskInterface
{
private CleanupService $cleanupService;
private ParameterBagInterface $parameterBag;

public function __construct(CleanupService $cleanupService, ParameterBagInterface $parameterBag) {
$this->cleanupService = $cleanupService;
$this->parameterBag = $parameterBag;
public function __construct(private CleanupService $cleanupService, private string $logDirectory, private int $seconds, private bool $keepLogs) {
}
public function execute(): void
{
$seconds = $this->parameterBag->get('process_manager.seconds');
$logDirectory = $this->parameterBag->get('process_manager.log_directory');
$keepLogs = $this->parameterBag->get('process_manager.keep_logs');
$this->cleanupService->cleanupDbEntries($seconds);
$this->cleanupService->cleanupLogFiles($logDirectory, $seconds, $keepLogs);
$this->cleanupService->cleanupDbEntries($this->seconds);
$this->cleanupService->cleanupLogFiles($this->logDirectory, $this->seconds, $this->keepLogs);
}
}
6 changes: 4 additions & 2 deletions src/ProcessManagerBundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ services:

ProcessManagerBundle\Maintenance\CleanupTask:
arguments:
- '@process_manager.cleanup_service'
- '@parameter_bag'
- '@ProcessManagerBundle\Service\CleanupService'
- '%process_manager.log_directory%'
- '%process_manager.seconds%'
- '%process_manager.keep_logs%'
tags:
- { name: pimcore.maintenance.task, type: process_manager.maintenance.cleanup }

Expand Down

0 comments on commit a7e6d14

Please sign in to comment.