NEW FEATURE: added cleanup command
This commit is contained in:
parent
f49e9e06a7
commit
f075e8db43
|
@ -0,0 +1,50 @@
|
|||
<?php
|
||||
|
||||
|
||||
namespace JeroenED\Webcron\Command;
|
||||
|
||||
|
||||
use Doctrine\DBAL\Exception;
|
||||
use JeroenED\Framework\Kernel;
|
||||
use JeroenED\Webcron\Repository\Run;
|
||||
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 CleanupCommand extends Command
|
||||
{
|
||||
protected static $defaultName = 'cleanup';
|
||||
protected $kernel;
|
||||
|
||||
public function __construct(Kernel $kernel)
|
||||
{
|
||||
$this->kernel = $kernel;
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
protected function configure()
|
||||
{
|
||||
$this
|
||||
->setDescription('Cleanup runs')
|
||||
->setHelp('This command cleans the runs table')
|
||||
->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');
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output)
|
||||
{
|
||||
$maxage = $input->getOption('maxage');
|
||||
$jobs = $input->getOption('jobid');
|
||||
$runRepo = new Run($this->kernel->getDbCon());
|
||||
try {
|
||||
$deleted = $runRepo->cleanupRuns($jobs, $maxage);
|
||||
$output->writeln('Deleted ' . $deleted . ' runs');
|
||||
return Command::SUCCESS;
|
||||
} catch(Exception $exception) {
|
||||
$output->writeln($exception->getMessage());
|
||||
return Command::FAILURE;
|
||||
}
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
}
|
|
@ -4,6 +4,7 @@
|
|||
namespace JeroenED\Webcron\Repository;
|
||||
|
||||
|
||||
use Doctrine\DBAL\Exception;
|
||||
use JeroenED\Framework\Repository;
|
||||
|
||||
class Run extends Repository
|
||||
|
@ -47,4 +48,23 @@ class Run extends Repository
|
|||
$slowJob = $this->dbcon->prepare($slowJobSql)->executeQuery([':jobid' => $jobid])->fetchAssociative();
|
||||
return $slowJob['average'] > $timelimit;
|
||||
}
|
||||
|
||||
public function cleanupRuns(array $jobids, int $maxage): int
|
||||
{
|
||||
$sql = 'DELETE FROM run WHERE timestamp < :timestamp';
|
||||
$params[':timestamp'] = time() - ($maxage * 24 * 60 * 60);
|
||||
if(!empty($jobids)) {
|
||||
$jobidsql = [];
|
||||
foreach($jobids as $key=>$jobid){
|
||||
$jobidsql[] = ':job' . $key;
|
||||
$params[':job' . $key] = $jobid;
|
||||
}
|
||||
$sql .= ' AND job_id in (' . implode(',', $jobidsql) . ')';
|
||||
}
|
||||
try {
|
||||
return $this->dbcon->prepare($sql)->executeQuery($params)->rowCount();
|
||||
} catch(Exception $exception) {
|
||||
throw $exception;
|
||||
}
|
||||
}
|
||||
}
|
2
webcron
2
webcron
|
@ -3,6 +3,7 @@
|
|||
require_once 'bootstrap.php';
|
||||
|
||||
use JeroenED\Framework\Kernel;
|
||||
use JeroenED\Webcron\Command\CleanupCommand;
|
||||
use JeroenED\Webcron\Command\DaemonCommand;
|
||||
use JeroenED\Webcron\Command\RunCommand;
|
||||
use Symfony\Component\Console\Application;
|
||||
|
@ -18,6 +19,7 @@ $kernel->parseDotEnv($kernel->getProjectDir() . '/.env');
|
|||
|
||||
$application->add(new RunCommand($kernel));
|
||||
$application->add(new DaemonCommand($kernel));
|
||||
$application->add(new CleanupCommand($kernel));
|
||||
|
||||
$application->run();
|
||||
|
||||
|
|
Loading…
Reference in New Issue