Skip to content

Commit 5be76d1

Browse files
committed
Merge #270 [fix 29]allow calling cron background jobs by class
2 parents 089b707 + adbc6a1 commit 5be76d1

File tree

3 files changed

+10
-5
lines changed

3 files changed

+10
-5
lines changed

cron.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,8 @@
160160
$endTime = time() + 14 * 60;
161161

162162
$executedJobs = [];
163-
while ($job = $jobList->getNext($onlyTimeSensitive)) {
163+
$jobClass = isset($argv[1]) ? $argv[1] : null;
164+
while ($job = $jobList->getNext($onlyTimeSensitive, $jobClass)) {
164165
if (isset($executedJobs[$job->getId()])) {
165166
$jobList->unlockJob($job);
166167
break;

lib/private/BackgroundJob/JobList.php

+7-3
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ public function getJobsIterator($job, ?int $limit, int $offset): iterable {
192192
* Get the next job in the list
193193
* @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.
194194
*/
195-
public function getNext(bool $onlyTimeSensitive = false): ?IJob {
195+
public function getNext(bool $onlyTimeSensitive = false, string $jobClass = null): ?IJob {
196196
$query = $this->connection->getQueryBuilder();
197197
$query->select('*')
198198
->from('jobs')
@@ -205,6 +205,10 @@ public function getNext(bool $onlyTimeSensitive = false): ?IJob {
205205
$query->andWhere($query->expr()->eq('time_sensitive', $query->createNamedParameter(IJob::TIME_SENSITIVE, IQueryBuilder::PARAM_INT)));
206206
}
207207

208+
if ($jobClass) {
209+
$query->andWhere($query->expr()->eq('class', $query->createNamedParameter($jobClass)));
210+
}
211+
208212
$result = $query->executeQuery();
209213
$row = $result->fetch();
210214
$result->closeCursor();
@@ -239,7 +243,7 @@ public function getNext(bool $onlyTimeSensitive = false): ?IJob {
239243

240244
if ($count === 0) {
241245
// Background job already executed elsewhere, try again.
242-
return $this->getNext($onlyTimeSensitive);
246+
return $this->getNext($onlyTimeSensitive, $jobClass);
243247
}
244248

245249
if ($job === null) {
@@ -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

+1-1
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)