From f075e8db4375883bd287a7f7dd1ade010bd99e44 Mon Sep 17 00:00:00 2001 From: Jeroen De Meerleer Date: Fri, 16 Jul 2021 14:31:18 +0200 Subject: [PATCH] NEW FEATURE: added cleanup command --- src/Command/CleanupCommand.php | 50 ++++++++++++++++++++++++++++++++++ src/Repository/Run.php | 20 ++++++++++++++ webcron | 2 ++ 3 files changed, 72 insertions(+) create mode 100644 src/Command/CleanupCommand.php diff --git a/src/Command/CleanupCommand.php b/src/Command/CleanupCommand.php new file mode 100644 index 0000000..e448408 --- /dev/null +++ b/src/Command/CleanupCommand.php @@ -0,0 +1,50 @@ +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; + } +} \ No newline at end of file diff --git a/src/Repository/Run.php b/src/Repository/Run.php index f3ebae1..42af71d 100644 --- a/src/Repository/Run.php +++ b/src/Repository/Run.php @@ -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; + } + } } \ No newline at end of file diff --git a/webcron b/webcron index 17e6620..6ee7047 100644 --- a/webcron +++ b/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();