From 05c35bef4d9a9abe7534e27e0f81ee2a21cf2193 Mon Sep 17 00:00:00 2001 From: Kirill Popov Date: Sat, 23 Apr 2022 16:42:37 +0300 Subject: [PATCH] Check maintenance window time when using WebCron The patch moves maintenance window check earlier so that maintenance window was respected when using both system cron and WebCron. Before the change webcron used to select **only** time-sensitive tasks. Signed-off-by: Kirill Popov --- cron.php | 48 +++++++++++++++++++++++++----------------------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/cron.php b/cron.php index 5095a2c757488..be2b8a99101ab 100644 --- a/cron.php +++ b/cron.php @@ -83,6 +83,28 @@ exit(1); } + // Low-load hours + $onlyTimeSensitive = false; + $startHour = $config->getSystemValueInt('maintenance_window_start', 100); + if ($startHour <= 23) { + $date = new \DateTime('now', new \DateTimeZone('UTC')); + $currentHour = (int) $date->format('G'); + $endHour = $startHour + 4; + + if ($startHour <= 20) { + // Start time: 01:00 + // End time: 05:00 + // Only run sensitive tasks when it's before the start or after the end + $onlyTimeSensitive = $currentHour < $startHour || $currentHour > $endHour; + } else { + // Start time: 23:00 + // End time: 03:00 + $endHour -= 24; // Correct the end time from 27:00 to 03:00 + // Only run sensitive tasks when it's after the end and before the start + $onlyTimeSensitive = $currentHour > $endHour && $currentHour < $startHour; + } + } + if (OC::$CLI) { // set to run indefinitely if needed if (strpos(@ini_get('disable_functions'), 'set_time_limit') === false) { @@ -110,28 +132,6 @@ $config->setAppValue('core', 'backgroundjobs_mode', 'cron'); } - // Low-load hours - $onlyTimeSensitive = false; - $startHour = $config->getSystemValueInt('maintenance_window_start', 100); - if ($startHour <= 23) { - $date = new \DateTime('now', new \DateTimeZone('UTC')); - $currentHour = (int) $date->format('G'); - $endHour = $startHour + 4; - - if ($startHour <= 20) { - // Start time: 01:00 - // End time: 05:00 - // Only run sensitive tasks when it's before the start or after the end - $onlyTimeSensitive = $currentHour < $startHour || $currentHour > $endHour; - } else { - // Start time: 23:00 - // End time: 03:00 - $endHour -= 24; // Correct the end time from 27:00 to 03:00 - // Only run sensitive tasks when it's after the end and before the start - $onlyTimeSensitive = $currentHour > $endHour && $currentHour < $startHour; - } - } - // Work $jobList = \OC::$server->getJobList(); @@ -147,6 +147,7 @@ break; } + $logger->debug('CLI cron call has selected job with ID ' . strval($job->getId()), ['app' => 'cron']); $job->execute($jobList, $logger); // clean up after unclean jobs \OC_Util::tearDownFS(); @@ -167,8 +168,9 @@ } else { // Work and success :-) $jobList = \OC::$server->getJobList(); - $job = $jobList->getNext(); + $job = $jobList->getNext($onlyTimeSensitive); if ($job != null) { + $logger->debug('WebCron call has selected job with ID ' . strval($job->getId()), ['app' => 'cron']); $job->execute($jobList, $logger); $jobList->setLastJob($job); }