diff options
author | blizzz <blizzz@arthur-schiwon.de> | 2023-01-16 11:10:10 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-01-16 11:10:10 +0100 |
commit | dbfe7b1816ee4d7756478d3abbe336a2cf3dbe57 (patch) | |
tree | 0b3ed4442f2cad09c47ba73dd752ddf09cb443dd /lib | |
parent | 5e090d044d3d660316450c3d4d2d7bfc3bde20dd (diff) | |
parent | d74044f6341e93861d820f21b6d37a2f98e1ef60 (diff) | |
download | nextcloud-server-dbfe7b1816ee4d7756478d3abbe336a2cf3dbe57.tar.gz nextcloud-server-dbfe7b1816ee4d7756478d3abbe336a2cf3dbe57.zip |
Merge pull request #36073 from nextcloud/fix/fix-background-job-list
Use a Generator for job list to fix background-job:list command
Diffstat (limited to 'lib')
-rw-r--r-- | lib/private/BackgroundJob/JobList.php | 32 | ||||
-rw-r--r-- | lib/public/BackgroundJob/IJobList.php | 18 |
2 files changed, 25 insertions, 25 deletions
diff --git a/lib/private/BackgroundJob/JobList.php b/lib/private/BackgroundJob/JobList.php index 20176e45125..67b736b8dd9 100644 --- a/lib/private/BackgroundJob/JobList.php +++ b/lib/private/BackgroundJob/JobList.php @@ -157,22 +157,20 @@ class JobList implements IJobList { return (bool) $row; } - /** - * get all jobs in the list - * - * @return IJob[] - * @deprecated 9.0.0 - This method is dangerous since it can cause load and - * memory problems when creating too many instances. Use getJobs instead. - */ - public function getAll(): array { - return $this->getJobs(null, null, 0); + 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 IJob[] + * @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): array { + public function getJobsIterator($job, ?int $limit, int $offset): iterable { $query = $this->connection->getQueryBuilder(); $query->select('*') ->from('jobs') @@ -190,20 +188,18 @@ class JobList implements IJobList { $result = $query->executeQuery(); - $jobs = []; while ($row = $result->fetch()) { $job = $this->buildJob($row); if ($job) { - $jobs[] = $job; + yield $job; } } $result->closeCursor(); - - return $jobs; } /** - * get the next job in the list + * 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 { $query = $this->connection->getQueryBuilder(); @@ -261,6 +257,9 @@ class JobList implements IJobList { } } + /** + * @return ?IJob The job matching the id. Beware that this object may be a singleton and may be modified by the next call to buildJob. + */ public function getById(int $id): ?IJob { $row = $this->getDetailsById($id); @@ -291,6 +290,7 @@ class JobList implements IJobList { * get the job object from a row in the db * * @param array{class:class-string<IJob>, id:mixed, last_run:mixed, argument:string} $row + * @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. */ private function buildJob(array $row): ?IJob { try { diff --git a/lib/public/BackgroundJob/IJobList.php b/lib/public/BackgroundJob/IJobList.php index 8f6449b070b..e8d0380e604 100644 --- a/lib/public/BackgroundJob/IJobList.php +++ b/lib/public/BackgroundJob/IJobList.php @@ -76,23 +76,23 @@ interface IJobList { public function has($job, $argument): bool; /** - * get all jobs in the list + * Get jobs matching the search * - * @return IJob[] - * @since 7.0.0 - * @deprecated 9.0.0 - This method is dangerous since it can cause load and - * memory problems when creating too many instances. Use getJobs instead. + * @param IJob|class-string<IJob>|null $job + * @return array<IJob> + * @since 25.0.0 + * @deprecated 26.0.0 Use getJobsIterator instead to avoid duplicated job objects */ - public function getAll(): array; + public function getJobs($job, ?int $limit, int $offset): array; /** * Get jobs matching the search * * @param IJob|class-string<IJob>|null $job - * @return IJob[] - * @since 25.0.0 + * @return iterable<IJob> + * @since 26.0.0 */ - public function getJobs($job, ?int $limit, int $offset): array; + public function getJobsIterator($job, ?int $limit, int $offset): iterable; /** * get the next job in the list |