Commit 00e4be15 authored by Timo Bühlmann's avatar Timo Bühlmann
Browse files

feat: automatically add timestamp to last-run argument

parent 14824410
......@@ -129,7 +129,8 @@ class ExecuteCommand extends Command
$cronJob = $this->cronJobManager->getCronJob($input->getArgument('cron_job'));
// Build command to execute
$command = array_merge(['bin/console', $cronJob->getCommand(), '--env='.$this->environment], $cronJob->getArguments());
$arguments = $this->prepareArguments($cronJob);
$command = array_merge(['bin/console', $cronJob->getCommand(), '--env='.$this->environment], $arguments);
// Create execution
$execution = new Execution();
......@@ -165,4 +166,32 @@ class ExecuteCommand extends Command
->setExitCode($process->getExitCode());
$this->em->flush($execution);
}
/**
* @param string[] $arguments
*/
protected function prepareArguments(CronJobInterface $cronJob) {
$arguments = $cronJob->getArguments();
$lastExecution = $this->getLastExecution($cronJob);
if ($lastExecution && $arguments && in_array('--last-run', $arguments)) {
// add last-run timestamp
$index = array_search('--last-run', $arguments);
array_splice($arguments, $index + 1, 0, $lastExecution->getStartedAt()->getTimestamp());
} else if (!$lastExecution && $arguments && in_array('--last-run', $arguments)) {
// remove --last-run argument if timestamp not available
$index = array_search('--last-run', $arguments);
unset($arguments[$index]);
}
return $arguments;
}
/**
* @param CronJobInterface $cronJob
*
* @return Execution|null
*/
public function getLastExecution(CronJobInterface $cronJob): ?Execution
{
return $this->em->getRepository(Execution::class)->findLastExecution($cronJob);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment