X Tutup
Skip to content

Commit 2b4ad01

Browse files
authored
Merge pull request #40113 from owncloud/e5205-feature-background-status
add last_checked, reserved_at and execution_duration to cc background:queue:status
2 parents 08110c7 + 32895eb commit 2b4ad01

File tree

8 files changed

+161
-18
lines changed

8 files changed

+161
-18
lines changed

changelog/unreleased/40113

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Enhancement: Add additional columns to background job queue status
2+
3+
Command `occ background:queue:status` now shows additional columns for:
4+
- Last Checked
5+
- Reserved At
6+
- Execution Duration
7+
8+
https://github.com/owncloud/core/pull/40113

core/Command/Background/Queue/Status.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/**
33
* @author Thomas Müller <thomas.mueller@tmit.eu>
44
*
5-
* @copyright Copyright (c) 2018, ownCloud GmbH
5+
* @copyright Copyright (c) 2022, ownCloud GmbH
66
* @license AGPL-3.0
77
*
88
* This code is free software: you can redistribute it and/or modify
@@ -58,9 +58,17 @@ private function getJobArgumentAsString($argument) {
5858
*/
5959
protected function execute(InputInterface $input, OutputInterface $output) {
6060
$t = new Table($output);
61-
$t->setHeaders(['Job ID', 'Job', 'Last Run', 'Job Arguments']);
61+
$t->setHeaders(['Job ID', 'Job', 'Job Arguments', 'Last Run', 'Last Checked', 'Reserved At', 'Execution Duration (s)']);
6262
$this->jobList->listJobs(function (IJob $job) use ($t) {
63-
$t->addRow([$job->getId(), \get_class($job), \date('c', $job->getLastRun()), $this->getJobArgumentAsString($job->getArgument())]);
63+
$t->addRow([
64+
$job->getId(),
65+
\get_class($job),
66+
$this->getJobArgumentAsString($job->getArgument()),
67+
$job->getLastRun() == 0 ? 'N/A' : \date('c', $job->getLastRun()),
68+
\date('c', $job->getLastChecked()),
69+
$job->getReservedAt() == 0 ? 'N/A' : \date('c', $job->getReservedAt()),
70+
$job->getExecutionDuration() == -1 ? 'N/A' : $job->getExecutionDuration(),
71+
]);
6472
});
6573
$t->render();
6674
}

lib/private/BackgroundJob/Job.php

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* @author Robin Appelman <icewind@owncloud.com>
55
* @author Thomas Müller <thomas.mueller@tmit.eu>
66
*
7-
* @copyright Copyright (c) 2018, ownCloud GmbH
7+
* @copyright Copyright (c) 2022, ownCloud GmbH
88
* @license AGPL-3.0
99
*
1010
* This code is free software: you can redistribute it and/or modify
@@ -42,6 +42,21 @@ abstract class Job implements IJob {
4242
*/
4343
protected $argument;
4444

45+
/**
46+
* @var int $lastChecked
47+
*/
48+
protected $lastChecked;
49+
50+
/**
51+
* @var int $reservedAt
52+
*/
53+
protected $reservedAt;
54+
55+
/**
56+
* @var int $executionDuration
57+
*/
58+
protected $executionDuration;
59+
4560
/**
4661
* @param JobList $jobList
4762
* @param ILogger $logger
@@ -105,6 +120,18 @@ public function setLastRun($lastRun) {
105120
public function setArgument($argument) {
106121
$this->argument = $argument;
107122
}
123+
124+
public function setLastChecked(int $lastChecked): void {
125+
$this->lastChecked = $lastChecked;
126+
}
127+
128+
public function setReservedAt(int $reservedAt): void {
129+
$this->reservedAt = $reservedAt;
130+
}
131+
132+
public function setExecutionDuration(int $executionDuration): void {
133+
$this->executionDuration = $executionDuration;
134+
}
108135

109136
public function getId() {
110137
return $this->id;
@@ -117,4 +144,16 @@ public function getLastRun() {
117144
public function getArgument() {
118145
return $this->argument;
119146
}
147+
148+
public function getLastChecked(): int {
149+
return $this->lastChecked;
150+
}
151+
152+
public function getReservedAt(): int {
153+
return $this->reservedAt;
154+
}
155+
156+
public function getExecutionDuration(): int {
157+
return $this->executionDuration;
158+
}
120159
}

lib/private/BackgroundJob/JobList.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ public function add($job, $argument = null) {
8181
'argument' => $query->createNamedParameter($argument),
8282
'last_run' => $query->createNamedParameter(0, IQueryBuilder::PARAM_INT),
8383
'last_checked' => $query->createNamedParameter($this->timeFactory->getTime(), IQueryBuilder::PARAM_INT),
84+
'reserved_at' => $query->createNamedParameter(0, IQueryBuilder::PARAM_INT),
85+
'execution_duration' => $query->createNamedParameter(-1, IQueryBuilder::PARAM_INT),
8486
]);
8587
$query->execute();
8688
}
@@ -265,6 +267,9 @@ private function buildJob($row) {
265267
$job->setId($row['id']);
266268
$job->setLastRun($row['last_run']);
267269
$job->setArgument(\json_decode($row['argument'], true));
270+
$job->setLastChecked($row['last_checked']);
271+
$job->setReservedAt($row['reserved_at']);
272+
$job->setExecutionDuration($row['execution_duration']);
268273

269274
return $job;
270275
} catch (AutoloadNotAllowedException $e) {

lib/public/BackgroundJob/IJob.php

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* @author Morris Jobke <hey@morrisjobke.de>
55
* @author Robin Appelman <icewind@owncloud.com>
66
*
7-
* @copyright Copyright (c) 2018, ownCloud GmbH
7+
* @copyright Copyright (c) 2022, ownCloud GmbH
88
* @license AGPL-3.0
99
*
1010
* This code is free software: you can redistribute it and/or modify
@@ -58,6 +58,24 @@ public function setLastRun($lastRun);
5858
*/
5959
public function setArgument($argument);
6060

61+
/**
62+
* @param int $lastChecked
63+
* @since 10.11.0
64+
*/
65+
public function setLastChecked(int $lastChecked): void;
66+
67+
/**
68+
* @param int $reservedAt
69+
* @since 10.11.0
70+
*/
71+
public function setReservedAt(int $reservedAt): void;
72+
73+
/**
74+
* @param int $executionDuration
75+
* @since 10.11.0
76+
*/
77+
public function setExecutionDuration(int $executionDuration): void;
78+
6179
/**
6280
* Get the id of the background job
6381
* This id is determined by the job list when a job is added to the list
@@ -68,7 +86,8 @@ public function setArgument($argument);
6886
public function getId();
6987

7088
/**
71-
* Get the last time this job was run as unix timestamp
89+
* Get the last time this job was run as unix timestamp.
90+
* Returns 0 if job never run.
7291
*
7392
* @return int
7493
* @since 7.0.0
@@ -83,4 +102,30 @@ public function getLastRun();
83102
* @since 7.0.0
84103
*/
85104
public function getArgument();
105+
106+
/**
107+
* Get the last time this job was added or checked for scheduling as unix timestamp.
108+
*
109+
* @return int time as unix timestamp
110+
* @since 10.11.0
111+
*/
112+
public function getLastChecked(): int;
113+
114+
/**
115+
* Get the reservation time of this job as unix timestamp.
116+
* Returns 0 if job is not reserved for scheduling, otherwise unix timestamp.
117+
*
118+
* @return int time as 0 if job is not reserved for scheduling, otherwise unix timestamp
119+
* @since 10.11.0
120+
*/
121+
public function getReservedAt(): int;
122+
123+
/**
124+
* Get the last execution duration of this job in seconds.
125+
* Returns -1 for never scheduled, 0 below a second duration, otherwise duration in seconds.
126+
*
127+
* @return int duration as -1 for never scheduled, 0 for below a second duration, otherwise in seconds
128+
* @since 10.11.0
129+
*/
130+
public function getExecutionDuration(): int;
86131
}

lib/public/BackgroundJob/IJobList.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* @author Robin Appelman <icewind@owncloud.com>
66
* @author Robin McCorkell <robin@mccorkell.me.uk>
77
*
8-
* @copyright Copyright (c) 2018, ownCloud GmbH
8+
* @copyright Copyright (c) 2022, ownCloud GmbH
99
* @license AGPL-3.0
1010
*
1111
* This code is free software: you can redistribute it and/or modify
@@ -70,7 +70,7 @@ public function has($job, $argument);
7070
public function getAll();
7171

7272
/**
73-
* get the next job in the list
73+
* get the next job in the list, allocating reservation to the job
7474
*
7575
* @return \OCP\BackgroundJob\IJob|null
7676
* @since 7.0.0

tests/Core/Command/Background/Queue/StatusTest.php

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,20 @@ public function testCommandInput() {
5454
->willReturnCallback(function (\Closure $callBack) {
5555
$job = new RegularJob();
5656
$job->setId(666);
57+
$job->setLastChecked(10);
58+
$job->setReservedAt(0);
59+
$job->setExecutionDuration(-1);
5760
$callBack($job);
5861
});
5962

6063
$this->commandTester->execute([]);
6164
$output = $this->commandTester->getDisplay();
6265
$expected = <<<EOS
63-
+--------+------------------------------------+---------------------------+---------------+
64-
| Job ID | Job | Last Run | Job Arguments |
65-
+--------+------------------------------------+---------------------------+---------------+
66-
| 666 | OC\BackgroundJob\Legacy\RegularJob | 1970-01-01T00:00:00+00:00 | |
67-
+--------+------------------------------------+---------------------------+---------------+
66+
+--------+------------------------------------+---------------+----------+---------------------------+-------------+------------------------+
67+
| Job ID | Job | Job Arguments | Last Run | Last Checked | Reserved At | Execution Duration (s) |
68+
+--------+------------------------------------+---------------+----------+---------------------------+-------------+------------------------+
69+
| 666 | OC\BackgroundJob\Legacy\RegularJob | | N/A | 1970-01-01T00:00:10+00:00 | N/A | N/A |
70+
+--------+------------------------------------+---------------+----------+---------------------------+-------------+------------------------+
6871
EOS;
6972

7073
$this->assertStringContainsString($expected, $output);
@@ -76,16 +79,44 @@ public function testJobWithArray() {
7679
$job = new RegularJob();
7780
$job->setId(666);
7881
$job->setArgument(['k'=> 'v','test2']);
82+
$job->setLastChecked(10);
83+
$job->setReservedAt(0);
84+
$job->setExecutionDuration(-1);
7985
$callBack($job);
8086
});
8187
$this->commandTester->execute([]);
8288
$output = $this->commandTester->getDisplay();
8389
$expected = <<<EOS
84-
+--------+------------------------------------+---------------------------+-----------------------+
85-
| Job ID | Job | Last Run | Job Arguments |
86-
+--------+------------------------------------+---------------------------+-----------------------+
87-
| 666 | OC\BackgroundJob\Legacy\RegularJob | 1970-01-01T00:00:00+00:00 | {"k":"v","0":"test2"} |
88-
+--------+------------------------------------+---------------------------+-----------------------+
90+
+--------+------------------------------------+-----------------------+----------+---------------------------+-------------+------------------------+
91+
| Job ID | Job | Job Arguments | Last Run | Last Checked | Reserved At | Execution Duration (s) |
92+
+--------+------------------------------------+-----------------------+----------+---------------------------+-------------+------------------------+
93+
| 666 | OC\BackgroundJob\Legacy\RegularJob | {"k":"v","0":"test2"} | N/A | 1970-01-01T00:00:10+00:00 | N/A | N/A |
94+
+--------+------------------------------------+-----------------------+----------+---------------------------+-------------+------------------------+
95+
EOS;
96+
97+
$this->assertStringContainsString($expected, $output);
98+
}
99+
100+
public function testJobWithSchedulingInfo() {
101+
$this->jobList->expects($this->any())->method('listJobs')
102+
->willReturnCallback(function (\Closure $callBack) {
103+
$job = new RegularJob();
104+
$job->setId(666);
105+
$job->setArgument(['k'=> 'v','test2']);
106+
$job->setLastRun(10);
107+
$job->setLastChecked(10);
108+
$job->setReservedAt(10);
109+
$job->setExecutionDuration(1);
110+
$callBack($job);
111+
});
112+
$this->commandTester->execute([]);
113+
$output = $this->commandTester->getDisplay();
114+
$expected = <<<EOS
115+
+--------+------------------------------------+-----------------------+---------------------------+---------------------------+---------------------------+------------------------+
116+
| Job ID | Job | Job Arguments | Last Run | Last Checked | Reserved At | Execution Duration (s) |
117+
+--------+------------------------------------+-----------------------+---------------------------+---------------------------+---------------------------+------------------------+
118+
| 666 | OC\BackgroundJob\Legacy\RegularJob | {"k":"v","0":"test2"} | 1970-01-01T00:00:10+00:00 | 1970-01-01T00:00:10+00:00 | 1970-01-01T00:00:10+00:00 | 1 |
119+
+--------+------------------------------------+-----------------------+---------------------------+---------------------------+---------------------------+------------------------+
89120
EOS;
90121

91122
$this->assertStringContainsString($expected, $output);

tests/lib/BackgroundJob/JobListTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ public function argumentProvider() {
8585
* @param $argument
8686
*/
8787
public function testAddRemove($argument) {
88+
$this->timeFactory->method('getTime')->willReturn(164419800);
8889
$existingJobs = $this->getAllSorted();
8990
$job = new TestJob();
9091
$this->instance->add($job, $argument);
@@ -107,6 +108,7 @@ public function testAddRemove($argument) {
107108
* @param $argument
108109
*/
109110
public function testRemoveDifferentArgument($argument) {
111+
$this->timeFactory->method('getTime')->willReturn(164419800);
110112
$existingJobs = $this->getAllSorted();
111113
$job = new TestJob();
112114
$this->instance->add($job, $argument);
@@ -128,6 +130,7 @@ public function testRemoveDifferentArgument($argument) {
128130
* @param $argument
129131
*/
130132
public function testHas($argument) {
133+
$this->timeFactory->method('getTime')->willReturn(164419800);
131134
$job = new TestJob();
132135
$this->assertFalse($this->instance->has($job, $argument));
133136
$this->instance->add($job, $argument);
@@ -144,6 +147,7 @@ public function testHas($argument) {
144147
* @param $argument
145148
*/
146149
public function testHasDifferentArgument($argument) {
150+
$this->timeFactory->method('getTime')->willReturn(164419800);
147151
$job = new TestJob();
148152
$this->instance->add($job, $argument);
149153

@@ -172,6 +176,7 @@ protected function createTempJob($class, $argument, $reservedTime = 0, $lastChec
172176
'last_run' => $query->createNamedParameter(0, IQueryBuilder::PARAM_INT),
173177
'last_checked' => $query->createNamedParameter($lastChecked, IQueryBuilder::PARAM_INT),
174178
'reserved_at' => $query->createNamedParameter($reservedTime, IQueryBuilder::PARAM_INT),
179+
'execution_duration' => $query->createNamedParameter(-1, IQueryBuilder::PARAM_INT),
175180
]);
176181
$query->execute();
177182
}
@@ -225,6 +230,7 @@ public function testGetNextSkipNonExisting() {
225230
* @param $argument
226231
*/
227232
public function testGetById($argument) {
233+
$this->timeFactory->method('getTime')->willReturn(164419800);
228234
$job = new TestJob();
229235
$this->instance->add($job, $argument);
230236

@@ -236,6 +242,7 @@ public function testGetById($argument) {
236242
}
237243

238244
public function testSetLastRun() {
245+
$this->timeFactory->method('getTime')->willReturn(164419800);
239246
$job = new TestJob();
240247
$this->instance->add($job);
241248

0 commit comments

Comments
 (0)
X Tutup