From 6e4270afa92ab2642cac2be88a1fbbeb789f775b Mon Sep 17 00:00:00 2001 From: "szabolcs.gyuris" Date: Wed, 14 Feb 2024 13:25:48 +0000 Subject: [PATCH] =?UTF-8?q?Multiple=20cron=20background=20jobs=20based=20o?= =?UTF-8?q?n=20class=20-=20Patch=20-=20Julius=20H=C3=A4rtl=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cron.php | 3 ++- lib/private/BackgroundJob/JobList.php | 10 +++++++--- tests/lib/BackgroundJob/DummyJobList.php | 2 +- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/cron.php b/cron.php index 7d661621ed090..4268b2ef36657 100644 --- a/cron.php +++ b/cron.php @@ -142,7 +142,8 @@ $endTime = time() + 14 * 60; $executedJobs = []; - while ($job = $jobList->getNext($onlyTimeSensitive)) { + $jobClass = isset($argv[1]) ? $argv[1] : null; + while ($job = $jobList->getNext($onlyTimeSensitive, $jobClass)) { if (isset($executedJobs[$job->getId()])) { $jobList->unlockJob($job); break; diff --git a/lib/private/BackgroundJob/JobList.php b/lib/private/BackgroundJob/JobList.php index ae214f695095d..d5dc875156cbe 100644 --- a/lib/private/BackgroundJob/JobList.php +++ b/lib/private/BackgroundJob/JobList.php @@ -206,7 +206,7 @@ public function getJobsIterator($job, ?int $limit, int $offset): iterable { * Get the next job in the list * @return ?IJob the next job to run. Beware that this object may be a singleton and may be modified by the next call to buildJob. */ - public function getNext(bool $onlyTimeSensitive = false): ?IJob { + public function getNext(bool $onlyTimeSensitive = false, string $jobClass = null): ?IJob { $query = $this->connection->getQueryBuilder(); $query->select('*') ->from('jobs') @@ -219,6 +219,10 @@ public function getNext(bool $onlyTimeSensitive = false): ?IJob { $query->andWhere($query->expr()->eq('time_sensitive', $query->createNamedParameter(IJob::TIME_SENSITIVE, IQueryBuilder::PARAM_INT))); } + if ($jobClass) { + $query->andWhere($query->expr()->eq('class', $query->createNamedParameter($jobClass))); + } + $result = $query->executeQuery(); $row = $result->fetch(); $result->closeCursor(); @@ -253,7 +257,7 @@ public function getNext(bool $onlyTimeSensitive = false): ?IJob { if ($count === 0) { // Background job already executed elsewhere, try again. - return $this->getNext($onlyTimeSensitive); + return $this->getNext($onlyTimeSensitive, $jobClass); } if ($job === null) { @@ -266,7 +270,7 @@ public function getNext(bool $onlyTimeSensitive = false): ?IJob { $reset->executeStatement(); // Background job from disabled app, try again. - return $this->getNext($onlyTimeSensitive); + return $this->getNext($onlyTimeSensitive, $jobClass); } return $job; diff --git a/tests/lib/BackgroundJob/DummyJobList.php b/tests/lib/BackgroundJob/DummyJobList.php index 8574f462ca76e..6727e529ed93c 100644 --- a/tests/lib/BackgroundJob/DummyJobList.php +++ b/tests/lib/BackgroundJob/DummyJobList.php @@ -96,7 +96,7 @@ public function getJobsIterator($job, ?int $limit, int $offset): array { /** * get the next job in the list */ - public function getNext(bool $onlyTimeSensitive = false): ?IJob { + public function getNext(bool $onlyTimeSensitive = false, string $jobClass = null): ?IJob { if (count($this->jobs) > 0) { if ($this->last < (count($this->jobs) - 1)) { $i = $this->last + 1;