Skip to content

Commit bd5905a

Browse files
committed
list reserved data and allow filtering by reservered for background job list command
Signed-off-by: Robin Appelman <robin@icewind.nl>
1 parent 76d4487 commit bd5905a

7 files changed

Lines changed: 54 additions & 10 deletions

File tree

core/Command/Background/ListCommand.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
namespace OC\Core\Command\Background;
2727

2828
use OC\Core\Command\Base;
29+
use OCP\BackgroundJob\IJob;
2930
use OCP\BackgroundJob\IJobList;
3031
use Symfony\Component\Console\Input\InputInterface;
3132
use Symfony\Component\Console\Input\InputOption;
@@ -61,24 +62,34 @@ protected function configure(): void {
6162
InputOption::VALUE_OPTIONAL,
6263
'Offset for retrieving jobs',
6364
'0'
65+
)->addOption(
66+
'reserved',
67+
null,
68+
InputOption::VALUE_NONE,
69+
'Only show reserved jobs'
6470
)
6571
;
6672
parent::configure();
6773
}
6874

6975
protected function execute(InputInterface $input, OutputInterface $output): int {
70-
$jobs = $this->jobList->getJobsIterator($input->getOption('class'), (int)$input->getOption('limit'), (int)$input->getOption('offset'));
76+
$jobs = $this->jobList->getJobsIterator($input->getOption('class'), (int)$input->getOption('limit'), (int)$input->getOption('offset'), (bool)$input->getOption('reserved'));
7177
$this->writeTableInOutputFormat($input, $output, $this->formatJobs($jobs));
7278
return 0;
7379
}
7480

81+
/**
82+
* @param iterable<IJob> $jobs
83+
* @return array
84+
*/
7585
protected function formatJobs(iterable $jobs): array {
7686
$jobsInfo = [];
7787
foreach ($jobs as $job) {
7888
$jobsInfo[] = [
7989
'id' => $job->getId(),
8090
'class' => get_class($job),
8191
'last_run' => date(DATE_ATOM, $job->getLastRun()),
92+
'reserved_at' => $job->getReservedAt() > 0 ? date(DATE_ATOM, $job->getReservedAt()) : 'not reserved',
8293
'argument' => json_encode($job->getArgument()),
8394
];
8495
}

lib/private/BackgroundJob/Job.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,9 @@
3333
* @deprecated internal class, use \OCP\BackgroundJob\Job
3434
*/
3535
abstract class Job implements IJob {
36-
/** @var int */
37-
protected $id;
38-
39-
/** @var int */
40-
protected $lastRun;
36+
protected int $id;
37+
protected int $lastRun;
38+
protected int $reservedAt;
4139

4240
/** @var mixed */
4341
protected $argument;
@@ -80,6 +78,10 @@ public function setLastRun(int $lastRun) {
8078
$this->lastRun = $lastRun;
8179
}
8280

81+
public function setReservedAt(int $reservedAt): void {
82+
$this->reservedAt = $reservedAt;
83+
}
84+
8385
public function setArgument($argument) {
8486
$this->argument = $argument;
8587
}
@@ -95,4 +97,8 @@ public function getLastRun() {
9597
public function getArgument() {
9698
return $this->argument;
9799
}
100+
101+
public function getReservedAt(): int {
102+
return $this->reservedAt;
103+
}
98104
}

lib/private/BackgroundJob/JobList.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ public function getJobs($job, ?int $limit, int $offset): array {
174174
* @param IJob|class-string<IJob>|null $job
175175
* @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.
176176
*/
177-
public function getJobsIterator($job, ?int $limit, int $offset): iterable {
177+
public function getJobsIterator($job, ?int $limit, int $offset, bool $reservedOnly = false): iterable {
178178
$query = $this->connection->getQueryBuilder();
179179
$query->select('*')
180180
->from('jobs')
@@ -190,6 +190,10 @@ public function getJobsIterator($job, ?int $limit, int $offset): iterable {
190190
$query->where($query->expr()->eq('class', $query->createNamedParameter($class)));
191191
}
192192

193+
if ($reservedOnly) {
194+
$query->where($query->expr()->gt('reserved_at', $query->createNamedParameter($this->timeFactory->getTime() - 12 * 3600, IQueryBuilder::PARAM_INT)));
195+
}
196+
193197
$result = $query->executeQuery();
194198

195199
while ($row = $result->fetch()) {
@@ -293,7 +297,7 @@ public function getDetailsById(int $id): ?array {
293297
/**
294298
* get the job object from a row in the db
295299
*
296-
* @param array{class:class-string<IJob>, id:mixed, last_run:mixed, argument:string} $row
300+
* @param array{class:class-string<IJob>, id:mixed, last_run:mixed, argument:string, reserved_at:int} $row
297301
* @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.
298302
*/
299303
private function buildJob(array $row): ?IJob {
@@ -320,6 +324,7 @@ private function buildJob(array $row): ?IJob {
320324
$job->setId((int) $row['id']);
321325
$job->setLastRun((int) $row['last_run']);
322326
$job->setArgument(json_decode($row['argument'], true));
327+
$job->setReservedAt((int) $row['reserved_at']);
323328
return $job;
324329
} catch (AutoloadNotAllowedException $e) {
325330
// job is from a disabled app, ignore

lib/public/BackgroundJob/IJob.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ public function setLastRun(int $lastRun);
8787
*/
8888
public function setArgument($argument);
8989

90+
/**
91+
* @param int $reservedAt
92+
* @since 28.0.0
93+
*/
94+
public function setReservedAt(int $reservedAt): void;
95+
9096
/**
9197
* Get the id of the background job
9298
* This id is determined by the job list when a job is added to the list
@@ -112,4 +118,11 @@ public function getLastRun();
112118
* @since 7.0.0
113119
*/
114120
public function getArgument();
121+
122+
/**
123+
* Get the timestamp when the job was reserved, or 0 if the job is not currently reserved
124+
*
125+
* @return int
126+
*/
127+
public function getReservedAt(): int;
115128
}

lib/public/BackgroundJob/IJobList.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public function getJobs($job, ?int $limit, int $offset): array;
9292
* @return iterable<IJob>
9393
* @since 26.0.0
9494
*/
95-
public function getJobsIterator($job, ?int $limit, int $offset): iterable;
95+
public function getJobsIterator($job, ?int $limit, int $offset, bool $reservedOnly = false): iterable;
9696

9797
/**
9898
* get the next job in the list

lib/public/BackgroundJob/Job.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
abstract class Job implements IJob, IParallelAwareJob {
4242
protected int $id = 0;
4343
protected int $lastRun = 0;
44+
protected int $reservedAt;
4445
protected $argument;
4546
protected ITimeFactory $time;
4647
protected bool $allowParallelRuns = true;
@@ -119,6 +120,10 @@ public function setArgument($argument) {
119120
$this->argument = $argument;
120121
}
121122

123+
public function setReservedAt(int $reservedAt): void {
124+
$this->reservedAt = $reservedAt;
125+
}
126+
122127
/**
123128
* @since 15.0.0
124129
*/
@@ -140,6 +145,10 @@ public function getArgument() {
140145
return $this->argument;
141146
}
142147

148+
public function getReservedAt(): int {
149+
return $this->reservedAt;
150+
}
151+
143152
/**
144153
* Set this to false to prevent two Jobs from this class from running in parallel
145154
*

tests/lib/BackgroundJob/DummyJobList.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public function getAll(): array {
7777
return $this->jobs;
7878
}
7979

80-
public function getJobsIterator($job, ?int $limit, int $offset): array {
80+
public function getJobsIterator($job, ?int $limit, int $offset, bool $reservedOnly = false): array {
8181
if ($job instanceof IJob) {
8282
$jobClass = get_class($job);
8383
} else {

0 commit comments

Comments
 (0)