From 02f018e94caefec0fe4366bb88111f258607b8e4 Mon Sep 17 00:00:00 2001 From: Jeroen De Meerleer Date: Wed, 22 May 2024 17:51:26 +0200 Subject: [PATCH] Add TimefixCommand for adjusting job run times - Added a new command to adjust next run and last run times of jobs based on input options. The command parses the adjustment time provided and updates the job entities accordingly. --- src/Command/TimefixCommand.php | 78 ++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 src/Command/TimefixCommand.php diff --git a/src/Command/TimefixCommand.php b/src/Command/TimefixCommand.php new file mode 100644 index 0000000..bff8f0c --- /dev/null +++ b/src/Command/TimefixCommand.php @@ -0,0 +1,78 @@ +kernel = $kernel; + $this->doctrine = $doctrine; + parent::__construct(); + } + + protected function configure() + { + $this + ->addOption('exclude', 'x', InputOption::VALUE_IS_ARRAY + InputOption::VALUE_OPTIONAL, 'The ids of the jobs exclude') + ->addOption('adjustment', 'a', InputOption::VALUE_REQUIRED, 'The amount of time to add or subtract from nextrun and last run times. Time can be a number of seconds or hh:mm:ss preceded with + or - (if not preceded + is taken)'); + } + protected function execute(InputInterface $input, OutputInterface $output): int + { + + $exclude = $input->getOption('exclude'); + $adjustment = $this->parseAdjustment($input->getOption('adjustment')); + if(!empty($exclude)) { + $qb = $this->doctrine->getRepository(Job::class)->createQueryBuilder('job'); + $qb + ->where($qb->expr()->notIn('job.id', ':ids')) + ->setParameter('ids', $exclude); + $results = $qb->getQuery()->getResult(); + } else { + $results = $this->doctrine->getRepository(Job::class)->findAll(); + } + foreach ($results as $job) { + $job->setNextRun($job->getNextrun() + $adjustment); + if($job->getLastrun() !== NULL) $job->setLastrun($job->getLastrun() + $adjustment); + } + $this->doctrine->getManager()->flush(); + return Command::SUCCESS; + } + + private function parseAdjustment(string $adjustment): int + { + $time = $adjustment; + if(in_array(substr($adjustment, 0, 1), ['+','-']) !== false) { + $time = substr($adjustment, 1); + } + + $time = explode(':', $time); + if(count($time) === 1) { + $seconds = $time[0]; + } elseif(count($time) === 2) { + throw new \InvalidArgumentException('Ambigious time format'); + } elseif(count($time) === 3) { + $seconds = $time[0] * 3600 + $time[1] * 60 + $time[2]; + } + + if(substr($adjustment, 0, 1) === '-') { + $seconds = 0 - $seconds; + } + return $seconds; + } +}