Преглед на файлове

feat(bg-jobs): Allow calling cron.php with a background job class

Signed-off-by: Julius Härtl <jus@bitgrid.net>
Julius Härtl преди 2 години
родител
ревизия
52eb6d8726
променени са 3 файла, в които са добавени 10 реда и са изтрити 5 реда
  1. 2 1
      cron.php
  2. 7 3
      lib/private/BackgroundJob/JobList.php
  3. 1 1
      tests/lib/BackgroundJob/DummyJobList.php

+ 2 - 1
cron.php

@@ -160,7 +160,8 @@ try {
 		$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;

+ 7 - 3
lib/private/BackgroundJob/JobList.php

@@ -214,7 +214,7 @@ class JobList implements IJobList {
 	 * 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')
@@ -227,6 +227,10 @@ class JobList implements IJobList {
 			$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();
@@ -261,7 +265,7 @@ class JobList implements IJobList {
 
 			if ($count === 0) {
 				// Background job already executed elsewhere, try again.
-				return $this->getNext($onlyTimeSensitive);
+				return $this->getNext($onlyTimeSensitive, $jobClass);
 			}
 
 			if ($job === null) {
@@ -274,7 +278,7 @@ class JobList implements IJobList {
 				$reset->executeStatement();
 
 				// Background job from disabled app, try again.
-				return $this->getNext($onlyTimeSensitive);
+				return $this->getNext($onlyTimeSensitive, $jobClass);
 			}
 
 			return $job;

+ 1 - 1
tests/lib/BackgroundJob/DummyJobList.php

@@ -100,7 +100,7 @@ class DummyJobList extends \OC\BackgroundJob\JobList {
 	/**
 	 * 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;