diff options
author | Joas Schilling <coding@schilljs.com> | 2022-01-31 17:56:43 +0100 |
---|---|---|
committer | Joas Schilling <coding@schilljs.com> | 2022-02-07 13:54:54 +0100 |
commit | b765f79368885ace29d8cd122144a1357435cfbb (patch) | |
tree | 80681213511834e39f5b34284fd1f7550933c144 /lib/public | |
parent | bb55b79837a677074473e980a6b88d358a118621 (diff) | |
download | nextcloud-server-b765f79368885ace29d8cd122144a1357435cfbb.tar.gz nextcloud-server-b765f79368885ace29d8cd122144a1357435cfbb.zip |
Allow apps to specify if their background job can be delayed
Signed-off-by: Joas Schilling <coding@schilljs.com>
Diffstat (limited to 'lib/public')
-rw-r--r-- | lib/public/BackgroundJob/IJob.php | 9 | ||||
-rw-r--r-- | lib/public/BackgroundJob/IJobList.php | 5 | ||||
-rw-r--r-- | lib/public/BackgroundJob/TimedJob.php | 32 |
3 files changed, 44 insertions, 2 deletions
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 |