aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
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/private/DB/ConnectionFactory.php13
-rw-r--r--lib/private/Security/Bruteforce/CleanupJob.php2
-rw-r--r--lib/public/BackgroundJob/IJob.php9
-rw-r--r--lib/public/BackgroundJob/IJobList.php5
-rw-r--r--lib/public/BackgroundJob/TimedJob.php32
8 files changed, 69 insertions, 11 deletions
diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php
index 58c6fe9665b..6b0ca70a796 100644
--- a/lib/composer/composer/autoload_classmap.php
+++ b/lib/composer/composer/autoload_classmap.php
@@ -1001,6 +1001,7 @@ return array(
'OC\\Core\\Migrations\\Version24000Date20211213081604' => $baseDir . '/core/Migrations/Version24000Date20211213081604.php',
'OC\\Core\\Migrations\\Version24000Date20211222112246' => $baseDir . '/core/Migrations/Version24000Date20211222112246.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 d80bead5592..3678f371f2e 100644
--- a/lib/composer/composer/autoload_static.php
+++ b/lib/composer/composer/autoload_static.php
@@ -1030,6 +1030,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
'OC\\Core\\Migrations\\Version24000Date20211213081604' => __DIR__ . '/../../..' . '/core/Migrations/Version24000Date20211213081604.php',
'OC\\Core\\Migrations\\Version24000Date20211222112246' => __DIR__ . '/../../..' . '/core/Migrations/Version24000Date20211222112246.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/private/DB/ConnectionFactory.php b/lib/private/DB/ConnectionFactory.php
index e9ed5979894..95f3185bcdb 100644
--- a/lib/private/DB/ConnectionFactory.php
+++ b/lib/private/DB/ConnectionFactory.php
@@ -189,22 +189,23 @@ class ConnectionFactory {
/**
* Create the connection parameters for the config
*
+ * @param string $configPrefix
* @return array
*/
- public function createConnectionParams() {
+ public function createConnectionParams(string $configPrefix = '') {
$type = $this->config->getValue('dbtype', 'sqlite');
$connectionParams = [
- 'user' => $this->config->getValue('dbuser', ''),
- 'password' => $this->config->getValue('dbpassword', ''),
+ 'user' => $this->config->getValue($configPrefix . 'dbuser', $this->config->getValue('dbuser', '')),
+ 'password' => $this->config->getValue($configPrefix . 'dbpassword', $this->config->getValue('dbpassword', '')),
];
- $name = $this->config->getValue('dbname', self::DEFAULT_DBNAME);
+ $name = $this->config->getValue($configPrefix . 'dbname', $this->config->getValue('dbname', self::DEFAULT_DBNAME));
if ($this->normalizeType($type) === 'sqlite3') {
$dataDir = $this->config->getValue("datadirectory", \OC::$SERVERROOT . '/data');
$connectionParams['path'] = $dataDir . '/' . $name . '.db';
} else {
- $host = $this->config->getValue('dbhost', '');
+ $host = $this->config->getValue($configPrefix . 'dbhost', $this->config->getValue('dbhost', ''));
$connectionParams = array_merge($connectionParams, $this->splitHostFromPortAndSocket($host));
$connectionParams['dbname'] = $name;
}
@@ -213,7 +214,7 @@ class ConnectionFactory {
$connectionParams['sqlite.journal_mode'] = $this->config->getValue('sqlite.journal_mode', 'WAL');
//additional driver options, eg. for mysql ssl
- $driverOptions = $this->config->getValue('dbdriveroptions', null);
+ $driverOptions = $this->config->getValue($configPrefix . 'dbdriveroptions', $this->config->getValue('dbdriveroptions', null));
if ($driverOptions) {
$connectionParams['driverOptions'] = $driverOptions;
}
diff --git a/lib/private/Security/Bruteforce/CleanupJob.php b/lib/private/Security/Bruteforce/CleanupJob.php
index 1e5f83360d5..6faf853760a 100644
--- a/lib/private/Security/Bruteforce/CleanupJob.php
+++ b/lib/private/Security/Bruteforce/CleanupJob.php
@@ -26,6 +26,7 @@ declare(strict_types=1);
namespace OC\Security\Bruteforce;
use OCP\AppFramework\Utility\ITimeFactory;
+use OCP\BackgroundJob\IJob;
use OCP\BackgroundJob\TimedJob;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
@@ -41,6 +42,7 @@ class CleanupJob extends TimedJob {
// Run once a day
$this->setInterval(3600 * 24);
+ $this->setTimeSensitivity(IJob::TIME_INSENSITIVE);
}
protected function run($argument) {
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