|
| 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 OC\Core\Command\InterruptedException; |
| 29 | +use OCP\BackgroundJob\IJobList; |
| 30 | +use Psr\Log\LoggerInterface; |
| 31 | +use Symfony\Component\Console\Input\InputArgument; |
| 32 | +use Symfony\Component\Console\Input\InputInterface; |
| 33 | +use Symfony\Component\Console\Input\InputOption; |
| 34 | +use Symfony\Component\Console\Output\OutputInterface; |
| 35 | + |
| 36 | +class JobWorker extends JobBase { |
| 37 | + private array $executedJobs = []; |
| 38 | + |
| 39 | + public function __construct(IJobList $jobList, |
| 40 | + LoggerInterface $logger) { |
| 41 | + parent::__construct($jobList, $logger); |
| 42 | + } |
| 43 | + |
| 44 | + protected function configure(): void { |
| 45 | + parent::configure(); |
| 46 | + |
| 47 | + $this |
| 48 | + ->setName('background-job:worker') |
| 49 | + ->setDescription('Run a background job worker') |
| 50 | + ->addArgument( |
| 51 | + 'job-class', |
| 52 | + InputArgument::OPTIONAL, |
| 53 | + 'The class of the job in the database' |
| 54 | + ) |
| 55 | + ->addOption( |
| 56 | + 'once', |
| 57 | + null, |
| 58 | + InputOption::VALUE_NONE, |
| 59 | + 'Only execute the worker once (as a regular cron execution would do it)' |
| 60 | + ) |
| 61 | + ->addOption( |
| 62 | + 'interval', |
| 63 | + 'i', |
| 64 | + InputOption::VALUE_OPTIONAL, |
| 65 | + 'Interval in seconds in which the worker should repeat already processed jobs (set to 0 for no repeat)', |
| 66 | + 5 |
| 67 | + ) |
| 68 | + ; |
| 69 | + } |
| 70 | + |
| 71 | + protected function execute(InputInterface $input, OutputInterface $output): int { |
| 72 | + $jobClass = $input->getArgument('job-class'); |
| 73 | + |
| 74 | + if ($jobClass && !class_exists($jobClass)) { |
| 75 | + $output->writeln('<error>Invalid job class</error>'); |
| 76 | + return 1; |
| 77 | + } |
| 78 | + |
| 79 | + while (true) { |
| 80 | + // Handle canceling of the process |
| 81 | + try { |
| 82 | + $this->abortIfInterrupted(); |
| 83 | + } catch (InterruptedException $e) { |
| 84 | + $output->writeln('<comment>Cleaning up before quitting. Press Ctrl-C again to kill, but this may have unexpected side effects.</comment>'); |
| 85 | + $this->unlockExecuted(); |
| 86 | + $output->writeln('<info>Background job worker stopped</info>'); |
| 87 | + break; |
| 88 | + } |
| 89 | + |
| 90 | + $this->printSummary($input, $output); |
| 91 | + |
| 92 | + $interval = (int)($input->getOption('interval') ?? 5); |
| 93 | + |
| 94 | + // Unlock jobs that should be executed again after the interval |
| 95 | + // Alternative could be to set last_checked to interval in the future to avoid the extra locks |
| 96 | + foreach ($this->executedJobs as $id => $time) { |
| 97 | + if ($time <= time() - $interval) { |
| 98 | + unset($this->executedJobs[$id]); |
| 99 | + $job = $this->jobList->getById($id); |
| 100 | + if ($job !== null) { |
| 101 | + $this->jobList->unlockJob($job); |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + usleep(50000); |
| 107 | + $job = $this->jobList->getNext(false, $jobClass); |
| 108 | + if (!$job) { |
| 109 | + if ($input->getOption('once') === true || $interval === 0) { |
| 110 | + break; |
| 111 | + } |
| 112 | + |
| 113 | + $output->writeln("Waiting for new jobs to be queued", OutputInterface::VERBOSITY_VERBOSE); |
| 114 | + // Re-check interval for new jobs |
| 115 | + sleep(1); |
| 116 | + continue; |
| 117 | + } |
| 118 | + |
| 119 | + |
| 120 | + if (isset($this->executedJobs[$job->getId()]) && ($this->executedJobs[$job->getId()] + $interval > time())) { |
| 121 | + $output->writeln("<comment>Job already executed within timeframe " . get_class($job) . " " . $job->getId() . '</comment>', OutputInterface::VERBOSITY_VERBOSE); |
| 122 | + continue; |
| 123 | + } |
| 124 | + |
| 125 | + $output->writeln("Running job " . get_class($job) . " with ID " . $job->getId()); |
| 126 | + |
| 127 | + if ($output->isVerbose()) { |
| 128 | + $this->printJobInfo($job->getId(), $job, $output); |
| 129 | + } |
| 130 | + |
| 131 | + $job->execute($this->jobList, \OC::$server->getLogger()); |
| 132 | + |
| 133 | + // clean up after unclean jobs |
| 134 | + \OC_Util::tearDownFS(); |
| 135 | + \OC::$server->getTempManager()->clean(); |
| 136 | + |
| 137 | + $this->jobList->setLastJob($job); |
| 138 | + $this->jobList->unlockJob($job); |
| 139 | + $this->executedJobs[$job->getId()] = time(); |
| 140 | + |
| 141 | + if ($input->getOption('once') === true) { |
| 142 | + break; |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + $this->unlockExecuted(); |
| 147 | + |
| 148 | + return 0; |
| 149 | + } |
| 150 | + |
| 151 | + private function printSummary(InputInterface $input, OutputInterface $output): void { |
| 152 | + if (!$output->isVeryVerbose()) { |
| 153 | + return; |
| 154 | + } |
| 155 | + $output->writeln("<comment>Summary</comment>"); |
| 156 | + |
| 157 | + $counts = []; |
| 158 | + foreach ($this->jobList->countByClass() as $row) { |
| 159 | + $counts[] = $row; |
| 160 | + } |
| 161 | + $this->writeTableInOutputFormat($input, $output, $counts); |
| 162 | + } |
| 163 | + |
| 164 | + private function unlockExecuted() { |
| 165 | + foreach ($this->executedJobs as $id => $time) { |
| 166 | + unset($this->executedJobs[$id]); |
| 167 | + $job = $this->jobList->getById($id); |
| 168 | + if ($job !== null) { |
| 169 | + $this->jobList->unlockJob($job); |
| 170 | + } |
| 171 | + } |
| 172 | + } |
| 173 | +} |
0 commit comments