Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nextcloud/server.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorJoas Schilling <coding@schilljs.com>2022-01-31 19:56:43 +0300
committerbackportbot[bot] <backportbot[bot]@users.noreply.github.com>2022-02-22 13:59:19 +0300
commitc8db7d35bfaeced6536b2794829f3cabd085ff31 (patch)
treedca551b60f8e38ca239efe4bfe5da48b7d2e7999 /lib
parent69e73cbdab6ba1c3ad8aab61e801c54accd0cfb9 (diff)
Allow apps to specify if their background job can be delayed
Signed-off-by: Joas Schilling <coding@schilljs.com>
Diffstat (limited to 'lib')
-rw-r--r--lib/composer/composer/autoload_classmap.php1
-rw-r--r--lib/composer/composer/autoload_static.php1
-rw-r--r--lib/private/BackgroundJob/JobList.php17
-rw-r--r--lib/public/BackgroundJob/IJob.php9
-rw-r--r--lib/public/BackgroundJob/IJobList.php5
-rw-r--r--lib/public/BackgroundJob/TimedJob.php32
6 files changed, 60 insertions, 5 deletions
diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php
index ab55e9e8a1e..2992bdfe638 100644
--- a/lib/composer/composer/autoload_classmap.php
+++ b/lib/composer/composer/autoload_classmap.php
@@ -994,6 +994,7 @@ return array(
'OC\\Core\\Migrations\\Version23000Date20211203110726' => $baseDir . '/core/Migrations/Version23000Date20211203110726.php',
'OC\\Core\\Migrations\\Version23000Date20211213203940' => $baseDir . '/core/Migrations/Version23000Date20211213203940.php',
'OC\\Core\\Migrations\\Version24000Date20211230140012' => $baseDir . '/core/Migrations/Version24000Date20211230140012.php',
+ 'OC\\Core\\Migrations\\Version24000Date20220131153041' => $baseDir . '/core/Migrations/Version24000Date20220131153041.php',
'OC\\Core\\Notification\\CoreNotifier' => $baseDir . '/core/Notification/CoreNotifier.php',
'OC\\Core\\Service\\LoginFlowV2Service' => $baseDir . '/core/Service/LoginFlowV2Service.php',
'OC\\DB\\Adapter' => $baseDir . '/lib/private/DB/Adapter.php',
diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php
index 6f48286f56b..a5079f6c214 100644
--- a/lib/composer/composer/autoload_static.php
+++ b/lib/composer/composer/autoload_static.php
@@ -1023,6 +1023,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
'OC\\Core\\Migrations\\Version23000Date20211203110726' => __DIR__ . '/../../..' . '/core/Migrations/Version23000Date20211203110726.php',
'OC\\Core\\Migrations\\Version23000Date20211213203940' => __DIR__ . '/../../..' . '/core/Migrations/Version23000Date20211213203940.php',
'OC\\Core\\Migrations\\Version24000Date20211230140012' => __DIR__ . '/../../..' . '/core/Migrations/Version24000Date20211230140012.php',
+ 'OC\\Core\\Migrations\\Version24000Date20220131153041' => __DIR__ . '/../../..' . '/core/Migrations/Version24000Date20220131153041.php',
'OC\\Core\\Notification\\CoreNotifier' => __DIR__ . '/../../..' . '/core/Notification/CoreNotifier.php',
'OC\\Core\\Service\\LoginFlowV2Service' => __DIR__ . '/../../..' . '/core/Service/LoginFlowV2Service.php',
'OC\\DB\\Adapter' => __DIR__ . '/../../..' . '/lib/private/DB/Adapter.php',
diff --git a/lib/private/BackgroundJob/JobList.php b/lib/private/BackgroundJob/JobList.php
index cece8bdf900..16dfa7b58a6 100644
--- a/lib/private/BackgroundJob/JobList.php
+++ b/lib/private/BackgroundJob/JobList.php
@@ -184,9 +184,10 @@ class JobList implements IJobList {
/**
* get the next job in the list
*
+ * @param bool $onlyTimeSensitive
* @return IJob|null
*/
- public function getNext() {
+ public function getNext(bool $onlyTimeSensitive = true): ?IJob {
$query = $this->connection->getQueryBuilder();
$query->select('*')
->from('jobs')
@@ -195,6 +196,10 @@ class JobList implements IJobList {
->orderBy('last_checked', 'ASC')
->setMaxResults(1);
+ if ($onlyTimeSensitive) {
+ $query->andWhere($query->expr()->eq('time_sensitive', $query->createNamedParameter(IJob::TIME_SENSITIVE, IQueryBuilder::PARAM_INT)));
+ }
+
$update = $this->connection->getQueryBuilder();
$update->update('jobs')
->set('reserved_at', $update->createNamedParameter($this->timeFactory->getTime()))
@@ -215,7 +220,7 @@ class JobList implements IJobList {
if ($count === 0) {
// Background job already executed elsewhere, try again.
- return $this->getNext();
+ return $this->getNext($onlyTimeSensitive);
}
$job = $this->buildJob($row);
@@ -229,7 +234,7 @@ class JobList implements IJobList {
$reset->execute();
// Background job from disabled app, try again.
- return $this->getNext();
+ return $this->getNext($onlyTimeSensitive);
}
return $job;
@@ -333,6 +338,12 @@ class JobList implements IJobList {
$query->update('jobs')
->set('last_run', $query->createNamedParameter(time(), IQueryBuilder::PARAM_INT))
->where($query->expr()->eq('id', $query->createNamedParameter($job->getId(), IQueryBuilder::PARAM_INT)));
+
+ if ($job instanceof \OCP\BackgroundJob\TimedJob
+ && !$job->isTimeSensitive()) {
+ $query->set('time_sensitive', $query->createNamedParameter(IJob::TIME_INSENSITIVE));
+ }
+
$query->execute();
}
diff --git a/lib/public/BackgroundJob/IJob.php b/lib/public/BackgroundJob/IJob.php
index 341ae2ac545..3c2da42bf88 100644
--- a/lib/public/BackgroundJob/IJob.php
+++ b/lib/public/BackgroundJob/IJob.php
@@ -35,6 +35,15 @@ use OCP\ILogger;
*/
interface IJob {
/**
+ * @since 24.0.0
+ */
+ public const TIME_INSENSITIVE = 0;
+ /**
+ * @since 24.0.0
+ */
+ public const TIME_SENSITIVE = 1;
+
+ /**
* Run the background job with the registered argument
*
* @param IJobList $jobList The job list that manages the state of this job
diff --git a/lib/public/BackgroundJob/IJobList.php b/lib/public/BackgroundJob/IJobList.php
index 9f3b485c280..eab37a03f36 100644
--- a/lib/public/BackgroundJob/IJobList.php
+++ b/lib/public/BackgroundJob/IJobList.php
@@ -85,10 +85,11 @@ interface IJobList {
/**
* get the next job in the list
*
+ * @param bool $onlyTimeSensitive
* @return \OCP\BackgroundJob\IJob|null
- * @since 7.0.0
+ * @since 7.0.0 - In 24.0.0 parameter $onlyTimeSensitive got added
*/
- public function getNext();
+ public function getNext(bool $onlyTimeSensitive = false): ?IJob;
/**
* @param int $id
diff --git a/lib/public/BackgroundJob/TimedJob.php b/lib/public/BackgroundJob/TimedJob.php
index 2cc9ea8e293..579486f6fbf 100644
--- a/lib/public/BackgroundJob/TimedJob.php
+++ b/lib/public/BackgroundJob/TimedJob.php
@@ -38,6 +38,8 @@ use OCP\ILogger;
abstract class TimedJob extends Job {
/** @var int */
protected $interval = 0;
+ /** @var int */
+ protected $timeSensitivity = IJob::TIME_SENSITIVE;
/**
* set the interval for the job
@@ -51,6 +53,36 @@ abstract class TimedJob extends Job {
}
/**
+ * 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.
+ *
+ * @return bool
+ * @since 24.0.0
+ */
+ public function isTimeSensitive(): bool {
+ return $this->timeSensitivity === IJob::TIME_SENSITIVE;
+ }
+
+ /**
+ * If your background job is not time sensitive (sending instant email
+ * notifications, etc.) it would be nice to set it to IJob::TIME_INSENSITIVE
+ * This way the execution can be delayed during high usage times.
+ *
+ * @param int $sensitivity
+ * @psalm-param IJob::TIME_* $sensitivity
+ * @return void
+ * @since 24.0.0
+ */
+ public function setTimeSensitivity(int $sensitivity): void {
+ if ($sensitivity !== IJob::TIME_SENSITIVE &&
+ $sensitivity !== IJob::TIME_INSENSITIVE) {
+ throw new \InvalidArgumentException('Invalid sensitivity');
+ }
+
+ $this->timeSensitivity = $sensitivity;
+ }
+
+ /**
* run the job if the last run is is more than the interval ago
*
* @param JobList $jobList