mirror of
https://github.com/nextcloud/server.git
synced 2026-05-28 04:32:30 -04:00
Use a Generator for job list to fix background-job:list command
Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
This commit is contained in:
parent
4ecf4b4642
commit
679682c186
3 changed files with 22 additions and 19 deletions
|
|
@ -72,15 +72,16 @@ class ListCommand extends Base {
|
|||
return 0;
|
||||
}
|
||||
|
||||
protected function formatJobs(array $jobs): array {
|
||||
return array_map(
|
||||
fn ($job) => [
|
||||
protected function formatJobs(iterable $jobs): array {
|
||||
$jobsInfo = [];
|
||||
foreach ($jobs as $job) {
|
||||
$jobsInfo[] = [
|
||||
'id' => $job->getId(),
|
||||
'class' => get_class($job),
|
||||
'last_run' => date(DATE_ATOM, $job->getLastRun()),
|
||||
'argument' => json_encode($job->getArgument()),
|
||||
],
|
||||
$jobs
|
||||
);
|
||||
];
|
||||
}
|
||||
return $jobsInfo;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -160,19 +160,19 @@ class JobList implements IJobList {
|
|||
/**
|
||||
* get all jobs in the list
|
||||
*
|
||||
* @return IJob[]
|
||||
* @return iterable<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 {
|
||||
public function getAll(): iterable {
|
||||
return $this->getJobs(null, null, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* @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 getJobs($job, ?int $limit, int $offset): iterable {
|
||||
$query = $this->connection->getQueryBuilder();
|
||||
$query->select('*')
|
||||
->from('jobs')
|
||||
|
|
@ -190,20 +190,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 +259,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 +292,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 {
|
||||
|
|
|
|||
|
|
@ -78,21 +78,21 @@ interface IJobList {
|
|||
/**
|
||||
* get all jobs in the list
|
||||
*
|
||||
* @return IJob[]
|
||||
* @return iterable<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.
|
||||
*/
|
||||
public function getAll(): array;
|
||||
public function getAll(): iterable;
|
||||
|
||||
/**
|
||||
* Get jobs matching the search
|
||||
*
|
||||
* @param IJob|class-string<IJob>|null $job
|
||||
* @return IJob[]
|
||||
* @return iterable<IJob>
|
||||
* @since 25.0.0
|
||||
*/
|
||||
public function getJobs($job, ?int $limit, int $offset): array;
|
||||
public function getJobs($job, ?int $limit, int $offset): iterable;
|
||||
|
||||
/**
|
||||
* get the next job in the list
|
||||
|
|
|
|||
Loading…
Reference in a new issue