Skip to content

Commit ad32a2d

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

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
@@ -214,7 +214,7 @@ public function getJobsIterator($job, ?int $limit, int $offset): iterable {
214214
* Get the next job in the list
215215
* @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.
216216
*/
217-
public function getNext(bool $onlyTimeSensitive = false): ?IJob {
217+
public function getNext(bool $onlyTimeSensitive = false, string $jobClass = null): ?IJob {
218218
$query = $this->connection->getQueryBuilder();
219219
$query->select('*')
220220
->from('jobs')
@@ -227,6 +227,10 @@ public function getNext(bool $onlyTimeSensitive = false): ?IJob {
227227
$query->andWhere($query->expr()->eq('time_sensitive', $query->createNamedParameter(IJob::TIME_SENSITIVE, IQueryBuilder::PARAM_INT)));
228228
}
229229

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

262266
if ($count === 0) {
263267
// Background job already executed elsewhere, try again.
264-
return $this->getNext($onlyTimeSensitive);
268+
return $this->getNext($onlyTimeSensitive, $jobClass);
265269
}
266270

267271
if ($job === null) {
@@ -274,7 +278,7 @@ public function getNext(bool $onlyTimeSensitive = false): ?IJob {
274278
$reset->executeStatement();
275279

276280
// Background job from disabled app, try again.
277-
return $this->getNext($onlyTimeSensitive);
281+
return $this->getNext($onlyTimeSensitive, $jobClass);
278282
}
279283

280284
return $job;

tests/lib/BackgroundJob/DummyJobList.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public function getJobsIterator($job, ?int $limit, int $offset): array {
100100
/**
101101
* get the next job in the list
102102
*/
103-
public function getNext(bool $onlyTimeSensitive = false): ?IJob {
103+
public function getNext(bool $onlyTimeSensitive = false, string $jobClass = null): ?IJob {
104104
if (count($this->jobs) > 0) {
105105
if ($this->last < (count($this->jobs) - 1)) {
106106
$i = $this->last + 1;

0 commit comments

Comments
 (0)