aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorChristoph Wurst <ChristophWurst@users.noreply.github.com>2025-03-31 17:31:44 +0200
committerGitHub <noreply@github.com>2025-03-31 17:31:44 +0200
commite655ffeb435ad801ccceba361cf77d7893b9ee30 (patch)
tree1f1ec9a5fdc5d03f572fcfbccfc9771d5e76fd78 /lib
parent12e66badc44367bd7078faedf61f2b54be7c46b6 (diff)
parent2395526e6c25f1e8aafa1abf322a0d38c0ab5d54 (diff)
downloadnextcloud-server-e655ffeb435ad801ccceba361cf77d7893b9ee30.tar.gz
nextcloud-server-e655ffeb435ad801ccceba361cf77d7893b9ee30.zip
Merge pull request #50768 from nextcloud/perf/cron/delay-timedjob-checking
perf(cron): Delay (re)checking timed jobs
Diffstat (limited to 'lib')
-rw-r--r--lib/private/BackgroundJob/JobList.php21
-rw-r--r--lib/public/BackgroundJob/TimedJob.php9
2 files changed, 30 insertions, 0 deletions
diff --git a/lib/private/BackgroundJob/JobList.php b/lib/private/BackgroundJob/JobList.php
index 98d037e4dec..0d88200cff7 100644
--- a/lib/private/BackgroundJob/JobList.php
+++ b/lib/private/BackgroundJob/JobList.php
@@ -20,6 +20,7 @@ use OCP\IDBConnection;
use Psr\Log\LoggerInterface;
use function get_class;
use function json_encode;
+use function min;
use function strlen;
class JobList implements IJobList {
@@ -209,6 +210,26 @@ class JobList implements IJobList {
return $this->getNext($onlyTimeSensitive, $jobClasses);
}
+ if ($job instanceof \OCP\BackgroundJob\TimedJob) {
+ $now = $this->timeFactory->getTime();
+ $nextPossibleRun = $job->getLastRun() + $job->getInterval();
+ if ($now < $nextPossibleRun) {
+ // This job is not ready for execution yet. Set timestamps to the future to avoid
+ // re-checking with every cron run.
+ // To avoid bugs that lead to jobs never executing again, the future timestamp is
+ // capped at two days.
+ $nextCheck = min($nextPossibleRun, $now + 48 * 3600);
+ $updateTimedJob = $this->connection->getQueryBuilder();
+ $updateTimedJob->update('jobs')
+ ->set('last_checked', $updateTimedJob->createNamedParameter($nextCheck, IQueryBuilder::PARAM_INT))
+ ->where($updateTimedJob->expr()->eq('id', $updateTimedJob->createParameter('jobid')));
+ $updateTimedJob->setParameter('jobid', $row['id']);
+ $updateTimedJob->executeStatement();
+
+ return $this->getNext($onlyTimeSensitive, $jobClasses);
+ }
+ }
+
$update = $this->connection->getQueryBuilder();
$update->update('jobs')
->set('reserved_at', $update->createNamedParameter($this->timeFactory->getTime()))
diff --git a/lib/public/BackgroundJob/TimedJob.php b/lib/public/BackgroundJob/TimedJob.php
index 85ca34665b4..296bd9e4e32 100644
--- a/lib/public/BackgroundJob/TimedJob.php
+++ b/lib/public/BackgroundJob/TimedJob.php
@@ -33,6 +33,15 @@ abstract class TimedJob extends Job {
}
/**
+ * Get the interval [seconds] for the job
+ *
+ * @since 32.0.0
+ */
+ public function getInterval(): int {
+ return $this->interval;
+ }
+
+ /**
* Whether the background job is time sensitive and needs to run soon after
* the scheduled interval, of if it is okay to be delayed until a later time.
*