webcron/src/Command/CleanupCommand.php

47 lines
1.5 KiB
PHP
Raw Normal View History

2021-07-16 14:31:18 +02:00
<?php
2022-04-27 14:24:48 +02:00
namespace App\Command;
2021-07-16 14:31:18 +02:00
2022-04-27 14:24:48 +02:00
use App\Entity\Run;
2021-07-16 14:31:18 +02:00
use Doctrine\DBAL\Exception;
2022-04-27 14:24:48 +02:00
use App\Repository\RunRepository;
use Doctrine\Persistence\ManagerRegistry;
2022-10-04 12:17:28 +02:00
use Symfony\Component\Console\Attribute\AsCommand;
2021-07-16 14:31:18 +02:00
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
2022-04-27 14:24:48 +02:00
use Symfony\Component\HttpKernel\KernelInterface;
2021-07-16 14:31:18 +02:00
2022-10-04 12:17:28 +02:00
#[AsCommand(name: 'webcron:cleanup', description: 'Cleanup runs')]
2021-07-16 14:31:18 +02:00
class CleanupCommand extends Command
{
protected $kernel;
2022-04-27 14:24:48 +02:00
protected $doctrine;
2021-07-16 14:31:18 +02:00
2022-04-27 14:24:48 +02:00
public function __construct(KernelInterface $kernel, ManagerRegistry $doctrine)
2021-07-16 14:31:18 +02:00
{
$this->kernel = $kernel;
2022-04-27 14:24:48 +02:00
$this->doctrine = $doctrine;
2021-07-16 14:31:18 +02:00
parent::__construct();
}
protected function configure()
{
$this
->addOption('jobid', 'j', InputOption::VALUE_IS_ARRAY + InputOption::VALUE_REQUIRED, 'The ids of the jobs to clean')
->addOption('maxage', 'm', InputOption::VALUE_REQUIRED, 'The maximum age of the oldest runs');
}
2022-10-04 13:29:27 +02:00
protected function execute(InputInterface $input, OutputInterface $output) : int
2021-07-16 14:31:18 +02:00
{
$maxage = $input->getOption('maxage');
$jobs = $input->getOption('jobid');
2022-04-27 14:24:48 +02:00
$runRepo = $this->doctrine->getRepository(Run::class);
2022-10-04 13:29:27 +02:00
$deleted = $runRepo->cleanupRuns($jobs, $maxage);
$output->writeln('Deleted ' . $deleted . ' runs');
2021-07-16 14:31:18 +02:00
return Command::SUCCESS;
}
}