Browse Source

Fix API breakage by using a new method instead

Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
Côme Chilliet 1 year ago
parent
commit
d74044f634

+ 1 - 1
core/Command/Background/ListCommand.php

@@ -67,7 +67,7 @@ class ListCommand extends Base {
 	}
 
 	protected function execute(InputInterface $input, OutputInterface $output): int {
-		$jobs = $this->jobList->getJobs($input->getOption('class'), (int)$input->getOption('limit'), (int)$input->getOption('offset'));
+		$jobs = $this->jobList->getJobsIterator($input->getOption('class'), (int)$input->getOption('limit'), (int)$input->getOption('offset'));
 		$this->writeTableInOutputFormat($input, $output, $this->formatJobs($jobs));
 		return 0;
 	}

+ 10 - 1
lib/private/BackgroundJob/JobList.php

@@ -157,11 +157,20 @@ class JobList implements IJobList {
 		return (bool) $row;
 	}
 
+	public function getJobs($job, ?int $limit, int $offset): array {
+		$iterable = $this->getJobsIterator($job, $limit, $offset);
+		if (is_array($iterable)) {
+			return $iterable;
+		} else {
+			return iterator_to_array($iterable);
+		}
+	}
+
 	/**
 	 * @param IJob|class-string<IJob>|null $job
 	 * @return iterable<IJob> Avoid to store these objects as they may share a Singleton instance. You should instead use these IJobs instances while looping on the iterable.
 	 */
-	public function getJobs($job, ?int $limit, int $offset): iterable {
+	public function getJobsIterator($job, ?int $limit, int $offset): iterable {
 		$query = $this->connection->getQueryBuilder();
 		$query->select('*')
 			->from('jobs')

+ 12 - 2
lib/public/BackgroundJob/IJobList.php

@@ -79,10 +79,20 @@ interface IJobList {
 	 * Get jobs matching the search
 	 *
 	 * @param IJob|class-string<IJob>|null $job
-	 * @return iterable<IJob>
+	 * @return array<IJob>
 	 * @since 25.0.0
+	 * @deprecated 26.0.0 Use getJobsIterator instead to avoid duplicated job objects
+	 */
+	public function getJobs($job, ?int $limit, int $offset): array;
+
+	/**
+	 * Get jobs matching the search
+	 *
+	 * @param IJob|class-string<IJob>|null $job
+	 * @return iterable<IJob>
+	 * @since 26.0.0
 	 */
-	public function getJobs($job, ?int $limit, int $offset): iterable;
+	public function getJobsIterator($job, ?int $limit, int $offset): iterable;
 
 	/**
 	 * get the next job in the list

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

@@ -72,7 +72,7 @@ class DummyJobList extends \OC\BackgroundJob\JobList {
 		return $this->jobs;
 	}
 
-	public function getJobs($job, ?int $limit, int $offset): array {
+	public function getJobsIterator($job, ?int $limit, int $offset): array {
 		if ($job instanceof IJob) {
 			$jobClass = get_class($job);
 		} else {

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

@@ -54,7 +54,7 @@ class JobListTest extends TestCase {
 	}
 
 	protected function getAllSorted() {
-		$iterator = $this->instance->getJobs(null, null, 0);
+		$iterator = $this->instance->getJobsIterator(null, null, 0);
 		$jobs = [];
 
 		foreach ($iterator as $job) {