Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 25 additions & 23 deletions cron.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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();

Expand All @@ -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();
Expand All @@ -167,8 +168,9 @@
} else {
// Work and success :-)
$jobList = \OC::$server->getJobList();
$job = $jobList->getNext();
$job = $jobList->getNext($onlyTimeSensitive);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The actual fix would be to just inject false here or change the default of the method

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please comment why?
I'm running NextCloud in Kubernetes and thus I can't use system cron daemon. Having Kubernetes' CronJob is a solution - it runs periodically and "curl's" cron.php. In this setup I still have short background jobs and time-sensitive ones which I want to run during maintenance hours.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Explanation in #30899 (comment)

if ($job != null) {
$logger->debug('WebCron call has selected job with ID ' . strval($job->getId()), ['app' => 'cron']);
$job->execute($jobList, $logger);
$jobList->setLastJob($job);
}
Expand Down