diff options
author | Marcel Klehr <mklehr@gmx.net> | 2023-04-20 12:55:06 +0200 |
---|---|---|
committer | Marcel Klehr <mklehr@gmx.net> | 2023-04-23 12:36:12 +0200 |
commit | c01c516323d91bb23f16186ffe0be09b3c412624 (patch) | |
tree | fb243da31b49e4eb9b43d322f7fb8eaa688e315e /lib/public/BackgroundJob | |
parent | f4f6431d4d645931bdbac01c86250cae9b27b0e6 (diff) | |
download | nextcloud-server-c01c516323d91bb23f16186ffe0be09b3c412624.tar.gz nextcloud-server-c01c516323d91bb23f16186ffe0be09b3c412624.zip |
feat(BackgroundJobs): Allow preventing parallel runs for a job class
Signed-off-by: Marcel Klehr <mklehr@gmx.net>
Diffstat (limited to 'lib/public/BackgroundJob')
-rw-r--r-- | lib/public/BackgroundJob/IJobList.php | 9 | ||||
-rw-r--r-- | lib/public/BackgroundJob/Job.php | 25 |
2 files changed, 34 insertions, 0 deletions
diff --git a/lib/public/BackgroundJob/IJobList.php b/lib/public/BackgroundJob/IJobList.php index e8d0380e604..71faefb8825 100644 --- a/lib/public/BackgroundJob/IJobList.php +++ b/lib/public/BackgroundJob/IJobList.php @@ -145,4 +145,13 @@ interface IJobList { * @since 23.0.0 */ public function resetBackgroundJob(IJob $job): void; + + /** + * Checks whether a job of the passed class is reserved to run + * + * @param string|null $className + * @return bool + * @since 27.0.0 + */ + public function hasReservedJob(?string $className): bool; } diff --git a/lib/public/BackgroundJob/Job.php b/lib/public/BackgroundJob/Job.php index d60fb5905c9..89ea41381c9 100644 --- a/lib/public/BackgroundJob/Job.php +++ b/lib/public/BackgroundJob/Job.php @@ -43,6 +43,7 @@ abstract class Job implements IJob { protected int $lastRun = 0; protected $argument; protected ITimeFactory $time; + protected bool $allowParallelRuns = true; /** * @since 15.0.0 @@ -72,6 +73,11 @@ abstract class Job implements IJob { $jobList->setLastRun($this); $logger = \OCP\Server::get(LoggerInterface::class); + if (!$this->getAllowParallelRuns() && $jobList->hasReservedJob(get_class($this))) { + $logger->debug('Skipping ' . get_class($this) . ' job with ID ' . $this->getId() . ' because another job with the same class is already running', ['app' => 'cron']); + return; + } + try { $jobStartTime = $this->time->getTime(); $logger->debug('Run ' . get_class($this) . ' job with ID ' . $this->getId(), ['app' => 'cron']); @@ -133,6 +139,25 @@ abstract class Job implements IJob { } /** + * Set this to false to prevent two Jobs from this class from running in parallel + * + * @param bool $allow + * @return void + * @since 27.0.0 + */ + public function setAllowParallelRuns(bool $allow) { + $this->allowParallelRuns = $allow; + } + + /** + * @return bool + * @since 27.0.0 + */ + public function getAllowParallelRuns(): bool { + return $this->allowParallelRuns; + } + + /** * The actual function that is called to run the job * * @param $argument |