Skip to content

Commit 1e39448

Browse files
juliusknorrtsdicloud
authored andcommitted
Add background worker occ command
Signed-off-by: Julius Härtl <jus@bitgrid.net>
1 parent b73269c commit 1e39448

File tree

3 files changed

+226
-0
lines changed

3 files changed

+226
-0
lines changed

core/Command/Background/Worker.php

Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* @copyright Copyright (c) 2021, Joas Schilling <coding@schilljs.com>
6+
*
7+
* @author Joas Schilling <coding@schilljs.com>
8+
*
9+
* @license GNU AGPL version 3 or any later version
10+
*
11+
* This program is free software: you can redistribute it and/or modify
12+
* it under the terms of the GNU Affero General Public License as
13+
* published by the Free Software Foundation, either version 3 of the
14+
* License, or (at your option) any later version.
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU Affero General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU Affero General Public License
22+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
23+
*
24+
*/
25+
26+
namespace OC\Core\Command\Background;
27+
28+
use OCP\BackgroundJob\IJob;
29+
use OCP\BackgroundJob\IJobList;
30+
use Psr\Log\LoggerInterface;
31+
use Symfony\Component\Console\Command\Command;
32+
use Symfony\Component\Console\Input\InputArgument;
33+
use Symfony\Component\Console\Input\InputInterface;
34+
use Symfony\Component\Console\Input\InputOption;
35+
use Symfony\Component\Console\Output\OutputInterface;
36+
37+
class Worker extends Command {
38+
protected IJobList $jobList;
39+
protected LoggerInterface $logger;
40+
41+
const DEFAULT_INTERVAL = 5;
42+
43+
public function __construct(IJobList $jobList,
44+
LoggerInterface $logger) {
45+
parent::__construct();
46+
$this->jobList = $jobList;
47+
$this->logger = $logger;
48+
}
49+
50+
protected function configure(): void {
51+
$this
52+
->setName('background-job:worker')
53+
->setDescription('Run a background job worker')
54+
->addArgument(
55+
'job-class',
56+
InputArgument::OPTIONAL,
57+
'The class of the job in the database'
58+
)
59+
->addOption(
60+
'once',
61+
null,
62+
InputOption::VALUE_NONE,
63+
'Only execute the worker once (as a regular cron execution would do it)'
64+
)
65+
;
66+
}
67+
68+
protected function execute(InputInterface $input, OutputInterface $output): int {
69+
$jobClass = $input->getArgument('job-class');
70+
71+
$executedJobs = [];
72+
73+
$ended = false;
74+
pcntl_signal(SIGINT, function () use (&$ended, $output, $executedJobs) {
75+
$output->writeln('SIGINT');
76+
if ($ended) {
77+
foreach ($executedJobs as $id => $time) {
78+
unset($executedJobs[$id]);
79+
$job = $this->jobList->getById($id);
80+
$this->jobList->unlockJob($job);
81+
}
82+
$output->writeln('<error>Killed');
83+
exit(1);
84+
}
85+
$ended = true;
86+
$output->writeln('<comment>Waiting for job to finish. Press Ctrl-C again to kill, but this may have unexpected side effects.</comment>');
87+
});
88+
89+
while (true) {
90+
if ($ended) {
91+
break;
92+
}
93+
$count = 0;
94+
$total = 0;
95+
foreach($this->jobList->countByClass() as $row) {
96+
if ((int)$row['count'] === 1) {
97+
$count++;
98+
} else {
99+
$output->writeln($row['class'] . " " . $row['count']);
100+
}
101+
$total += $row['count'];
102+
}
103+
$output->writeln("Other jobs " . $count);
104+
$output->writeln("Total jobs " . $count);
105+
106+
107+
108+
foreach ($executedJobs as $id => $time) {
109+
if ($time < time() - self::DEFAULT_INTERVAL) {
110+
unset($executedJobs[$id]);
111+
$job = $this->jobList->getById($id);
112+
$this->jobList->unlockJob($job);
113+
}
114+
}
115+
116+
$job = $this->jobList->getNext(false, $jobClass);
117+
if (!$job) {
118+
$output->writeln("Waiting for new jobs to be queued");
119+
sleep(1);
120+
continue;
121+
}
122+
123+
124+
if (isset($executedJobs[$job->getId()])) {
125+
continue;
126+
}
127+
128+
$output->writeln("- Running job " . get_class($job) . " " . $job->getId());
129+
130+
if ($output->isVerbose()) {
131+
$this->printJobInfo($job->getId(), $job, $output);
132+
}
133+
134+
$job->execute($this->jobList, \OC::$server->getLogger());
135+
136+
// clean up after unclean jobs
137+
\OC_Util::tearDownFS();
138+
\OC::$server->getTempManager()->clean();
139+
140+
$this->jobList->setLastJob($job);
141+
$executedJobs[$job->getId()] = time();
142+
unset($job);
143+
144+
if ($input->getOption('once')) {
145+
break;
146+
}
147+
}
148+
149+
foreach ($executedJobs as $id => $time) {
150+
unset($executedJobs[$id]);
151+
$job = $this->jobList->getById($id);
152+
$this->jobList->unlockJob($job);
153+
}
154+
155+
return 0;
156+
}
157+
158+
protected function printJobInfo(int $jobId, IJob $job, OutputInterface$output): void {
159+
$row = $this->jobList->getDetailsById($jobId);
160+
161+
$lastRun = new \DateTime();
162+
$lastRun->setTimestamp((int) $row['last_run']);
163+
$lastChecked = new \DateTime();
164+
$lastChecked->setTimestamp((int) $row['last_checked']);
165+
$reservedAt = new \DateTime();
166+
$reservedAt->setTimestamp((int) $row['reserved_at']);
167+
168+
$output->writeln('Job class: ' . get_class($job));
169+
$output->writeln('Arguments: ' . json_encode($job->getArgument()));
170+
171+
$isTimedJob = $job instanceof \OC\BackgroundJob\TimedJob || $job instanceof \OCP\BackgroundJob\TimedJob;
172+
if ($isTimedJob) {
173+
$output->writeln('Type: timed');
174+
} elseif ($job instanceof \OC\BackgroundJob\QueuedJob || $job instanceof \OCP\BackgroundJob\QueuedJob) {
175+
$output->writeln('Type: queued');
176+
} else {
177+
$output->writeln('Type: job');
178+
}
179+
180+
$output->writeln('');
181+
$output->writeln('Last checked: ' . $lastChecked->format(\DateTimeInterface::ATOM));
182+
if ((int) $row['reserved_at'] === 0) {
183+
$output->writeln('Reserved at: -');
184+
} else {
185+
$output->writeln('Reserved at: <comment>' . $reservedAt->format(\DateTimeInterface::ATOM) . '</comment>');
186+
}
187+
$output->writeln('Last executed: ' . $lastRun->format(\DateTimeInterface::ATOM));
188+
$output->writeln('Last duration: ' . $row['execution_duration']);
189+
190+
if ($isTimedJob) {
191+
$reflection = new \ReflectionClass($job);
192+
$intervalProperty = $reflection->getProperty('interval');
193+
$intervalProperty->setAccessible(true);
194+
$interval = $intervalProperty->getValue($job);
195+
196+
$nextRun = new \DateTime();
197+
$nextRun->setTimestamp($row['last_run'] + $interval);
198+
199+
if ($nextRun > new \DateTime()) {
200+
$output->writeln('Next execution: <comment>' . $nextRun->format(\DateTimeInterface::ATOM) . '</comment>');
201+
} else {
202+
$output->writeln('Next execution: <info>' . $nextRun->format(\DateTimeInterface::ATOM) . '</info>');
203+
}
204+
}
205+
}
206+
}

core/register_command.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
$application->add(new OC\Core\Command\Background\WebCron(\OC::$server->getConfig()));
9191
$application->add(new OC\Core\Command\Background\Ajax(\OC::$server->getConfig()));
9292
$application->add(new OC\Core\Command\Background\Job(\OC::$server->getJobList(), \OC::$server->getLogger()));
93+
$application->add(new OC\Core\Command\Background\Worker(\OC::$server->getJobList(), \OC::$server->get(LoggerInterface::class)));
9394
$application->add(new OC\Core\Command\Background\ListCommand(\OC::$server->getJobList()));
9495

9596
$application->add(\OC::$server->query(\OC\Core\Command\Broadcast\Test::class));

lib/private/BackgroundJob/JobList.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,4 +386,23 @@ public function resetBackgroundJob(IJob $job): void {
386386
->where($query->expr()->eq('id', $query->createNamedParameter($job->getId()), IQueryBuilder::PARAM_INT));
387387
$query->executeStatement();
388388
}
389+
390+
public function countByClass(): array {
391+
$query = $this->connection->getQueryBuilder();
392+
$query->select('class')
393+
->selectAlias($query->func()->count('id'), 'count')
394+
->from('jobs')
395+
->orderBy('count')
396+
->groupBy('class');
397+
398+
$result = $query->executeQuery();
399+
400+
$jobs = [];
401+
while ($row = $result->fetch()) {
402+
$jobs[] = $row;
403+
}
404+
405+
return $jobs;
406+
407+
}
389408
}

0 commit comments

Comments
 (0)