Skip to content

Commit 8306493

Browse files
committed
Merge #260 [backport 28]/nmc/allow calling cron background jobs by class
2 parents 66c43a6 + b1ad129 commit 8306493

File tree

3 files changed

+10
-5
lines changed

3 files changed

+10
-5
lines changed

cron.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@
142142
$endTime = time() + 14 * 60;
143143

144144
$executedJobs = [];
145-
while ($job = $jobList->getNext($onlyTimeSensitive)) {
145+
$jobClass = isset($argv[1]) ? $argv[1] : null;
146+
while ($job = $jobList->getNext($onlyTimeSensitive, $jobClass)) {
146147
if (isset($executedJobs[$job->getId()])) {
147148
$jobList->unlockJob($job);
148149
break;

lib/private/BackgroundJob/JobList.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ public function getJobsIterator($job, ?int $limit, int $offset): iterable {
213213
* Get the next job in the list
214214
* @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.
215215
*/
216-
public function getNext(bool $onlyTimeSensitive = false): ?IJob {
216+
public function getNext(bool $onlyTimeSensitive = false, string $jobClass = null): ?IJob {
217217
$query = $this->connection->getQueryBuilder();
218218
$query->select('*')
219219
->from('jobs')
@@ -226,6 +226,10 @@ public function getNext(bool $onlyTimeSensitive = false): ?IJob {
226226
$query->andWhere($query->expr()->eq('time_sensitive', $query->createNamedParameter(IJob::TIME_SENSITIVE, IQueryBuilder::PARAM_INT)));
227227
}
228228

229+
if ($jobClass) {
230+
$query->andWhere($query->expr()->eq('class', $query->createNamedParameter($jobClass)));
231+
}
232+
229233
$result = $query->executeQuery();
230234
$row = $result->fetch();
231235
$result->closeCursor();
@@ -260,7 +264,7 @@ public function getNext(bool $onlyTimeSensitive = false): ?IJob {
260264

261265
if ($count === 0) {
262266
// Background job already executed elsewhere, try again.
263-
return $this->getNext($onlyTimeSensitive);
267+
return $this->getNext($onlyTimeSensitive, $jobClass);
264268
}
265269

266270
if ($job === null) {
@@ -273,7 +277,7 @@ public function getNext(bool $onlyTimeSensitive = false): ?IJob {
273277
$reset->executeStatement();
274278

275279
// Background job from disabled app, try again.
276-
return $this->getNext($onlyTimeSensitive);
280+
return $this->getNext($onlyTimeSensitive, $jobClass);
277281
}
278282

279283
return $job;

tests/lib/BackgroundJob/DummyJobList.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public function getJobsIterator($job, ?int $limit, int $offset): array {
114114
/**
115115
* get the next job in the list
116116
*/
117-
public function getNext(bool $onlyTimeSensitive = false): ?IJob {
117+
public function getNext(bool $onlyTimeSensitive = false, string $jobClass = null): ?IJob {
118118
if (count($this->jobs) > 0) {
119119
if ($this->last < (count($this->jobs) - 1)) {
120120
$i = $this->last + 1;

0 commit comments

Comments
 (0)