Added 'outdated' option to scheduler command (#3771)

This commit is contained in:
maelanleborgne
2024-01-05 12:46:14 +01:00
committed by GitHub
parent 88eb9f915a
commit a71403f158
4 changed files with 194 additions and 2 deletions

View File

@@ -200,6 +200,28 @@ class Job
return $this->executionTime->isDue($date);
}
/**
* Check if the Job should have run previously.
*
* @param DateTime|null $date
* @return bool
*/
public function isOverdue(?DateTime $date = null, ?DateTime $lastRun = null)
{
// If the time elapsed since the creation is inferior to the interval, it's not overdue
if ($this->creationTime > $this->executionTime->getPreviousRunDate($date)) {
return false;
}
// Else, if the job has never run, it's overdue
if (null === $lastRun) {
return true;
}
$date = $date ?? new DateTime('now');
// Else if the last run time is inferior to the previous scheduled time, it's overdue
return $lastRun < $this->executionTime->getPreviousRunDate($date);
}
/**
* Check if the Job is overlapping.
*

View File

@@ -188,7 +188,7 @@ class Scheduler
* @param DateTime|null $runTime Optional, run at specific moment
* @param bool $force force run even if not due
*/
public function run(DateTime $runTime = null, $force = false)
public function run(DateTime $runTime = null, $force = false, $overdue = false)
{
$this->loadSavedJobs();
@@ -199,9 +199,17 @@ class Scheduler
$runTime = new DateTime('now');
}
if ($overdue) {
$lastRuns = [];
foreach ($this->getJobStates()->content() as $id => $state) {
$timestamp = $state['last-run'] ?? time();
$lastRuns[$id] = DateTime::createFromFormat('U',$timestamp);
}
}
// Star processing jobs
foreach ($alljobs as $job) {
if ($job->isDue($runTime) || $force) {
if ($job->isDue($runTime) || $force || ($overdue && $job->isOverdue($runTime, $lastRuns[$job->getId()] ?? null))) {
$job->run();
$this->jobs_run[] = $job;
}