Skip to content

Commit b73269c

Browse files
juliusknorrtsdicloud
authored andcommitted
Allow calling cron.php with a background job class
Signed-off-by: Julius Härtl <jus@bitgrid.net>
1 parent df9a14d commit b73269c

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
@@ -205,7 +205,7 @@ public function getJobs($job, ?int $limit, int $offset): array {
205205
/**
206206
* get the next job in the list
207207
*/
208-
public function getNext(bool $onlyTimeSensitive = false): ?IJob {
208+
public function getNext(bool $onlyTimeSensitive = false, string $jobClass = null): ?IJob {
209209
$query = $this->connection->getQueryBuilder();
210210
$query->select('*')
211211
->from('jobs')
@@ -218,6 +218,10 @@ public function getNext(bool $onlyTimeSensitive = false): ?IJob {
218218
$query->andWhere($query->expr()->eq('time_sensitive', $query->createNamedParameter(IJob::TIME_SENSITIVE, IQueryBuilder::PARAM_INT)));
219219
}
220220

221+
if ($jobClass) {
222+
$query->andWhere($query->expr()->eq('class', $query->createNamedParameter($jobClass)));
223+
}
224+
221225
$update = $this->connection->getQueryBuilder();
222226
$update->update('jobs')
223227
->set('reserved_at', $update->createNamedParameter($this->timeFactory->getTime()))
@@ -238,7 +242,7 @@ public function getNext(bool $onlyTimeSensitive = false): ?IJob {
238242

239243
if ($count === 0) {
240244
// Background job already executed elsewhere, try again.
241-
return $this->getNext($onlyTimeSensitive);
245+
return $this->getNext($onlyTimeSensitive, $jobClass);
242246
}
243247
$job = $this->buildJob($row);
244248

@@ -252,7 +256,7 @@ public function getNext(bool $onlyTimeSensitive = false): ?IJob {
252256
$reset->executeStatement();
253257

254258
// Background job from disabled app, try again.
255-
return $this->getNext($onlyTimeSensitive);
259+
return $this->getNext($onlyTimeSensitive, $jobClass);
256260
}
257261

258262
return $job;

tests/lib/BackgroundJob/DummyJobList.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public function getJobs($job, ?int $limit, int $offset): array {
9191
/**
9292
* get the next job in the list
9393
*/
94-
public function getNext(bool $onlyTimeSensitive = false): ?IJob {
94+
public function getNext(bool $onlyTimeSensitive = false, string $jobClass = null): ?IJob {
9595
if (count($this->jobs) > 0) {
9696
if ($this->last < (count($this->jobs) - 1)) {
9797
$i = $this->last + 1;

0 commit comments

Comments
 (0)