mirror of
https://github.com/getgrav/grav.git
synced 2026-05-06 11:37:12 +02:00
Added 'outdated' option to scheduler command (#3771)
This commit is contained in:
@@ -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.
|
||||
*
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user