JobWorker.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2021, Joas Schilling <coding@schilljs.com>
  5. *
  6. * @author Joas Schilling <coding@schilljs.com>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OC\Core\Command\Background;
  25. use OC\Core\Command\InterruptedException;
  26. use OC\Files\SetupManager;
  27. use OCP\BackgroundJob\IJobList;
  28. use OCP\ITempManager;
  29. use Psr\Log\LoggerInterface;
  30. use Symfony\Component\Console\Input\InputArgument;
  31. use Symfony\Component\Console\Input\InputInterface;
  32. use Symfony\Component\Console\Input\InputOption;
  33. use Symfony\Component\Console\Output\OutputInterface;
  34. class JobWorker extends JobBase {
  35. public function __construct(
  36. protected IJobList $jobList,
  37. protected LoggerInterface $logger,
  38. private ITempManager $tempManager,
  39. private SetupManager $setupManager,
  40. ) {
  41. parent::__construct($jobList, $logger);
  42. }
  43. protected function configure(): void {
  44. parent::configure();
  45. $this
  46. ->setName('background-job:worker')
  47. ->setDescription('Run a background job worker')
  48. ->addArgument(
  49. 'job-classes',
  50. InputArgument::OPTIONAL,
  51. 'The classes of the jobs to look for in the database, comma-separated'
  52. )
  53. ->addOption(
  54. 'once',
  55. null,
  56. InputOption::VALUE_NONE,
  57. 'Only execute the worker once (as a regular cron execution would do it)'
  58. )
  59. ->addOption(
  60. 'interval',
  61. 'i',
  62. InputOption::VALUE_OPTIONAL,
  63. 'Interval in seconds in which the worker should repeat already processed jobs (set to 0 for no repeat)',
  64. 5
  65. )
  66. ;
  67. }
  68. protected function execute(InputInterface $input, OutputInterface $output): int {
  69. $jobClassesString = $input->getArgument('job-classes');
  70. // only keep non-empty strings
  71. $jobClasses = $jobClassesString === null
  72. ? null
  73. : array_filter(
  74. explode(',', $jobClassesString),
  75. static function (string $jobClass) {
  76. return strlen($jobClass) > 0;
  77. }
  78. );
  79. if ($jobClasses !== null) {
  80. // no class
  81. if (count($jobClasses) === 0) {
  82. $output->writeln('<error>Invalid job class list supplied</error>');
  83. return 1;
  84. }
  85. // at least one invalid class
  86. foreach ($jobClasses as $jobClass) {
  87. if (!class_exists($jobClass)) {
  88. $output->writeln('<error>Invalid job class: ' . $jobClass . '</error>');
  89. return 1;
  90. }
  91. }
  92. }
  93. while (true) {
  94. // Handle canceling of the process
  95. try {
  96. $this->abortIfInterrupted();
  97. } catch (InterruptedException $e) {
  98. $output->writeln('<info>Background job worker stopped</info>');
  99. break;
  100. }
  101. $this->printSummary($input, $output);
  102. usleep(50000);
  103. $job = $this->jobList->getNext(false, $jobClasses);
  104. if (!$job) {
  105. if ($input->getOption('once') === true) {
  106. if ($jobClassesString === null) {
  107. $output->writeln('No job is currently queued', OutputInterface::VERBOSITY_VERBOSE);
  108. } else {
  109. $output->writeln('No job of classes ' . $jobClassesString . ' is currently queued', OutputInterface::VERBOSITY_VERBOSE);
  110. }
  111. $output->writeln('Exiting...', OutputInterface::VERBOSITY_VERBOSE);
  112. break;
  113. }
  114. $output->writeln('Waiting for new jobs to be queued', OutputInterface::VERBOSITY_VERBOSE);
  115. // Re-check interval for new jobs
  116. sleep(1);
  117. continue;
  118. }
  119. $output->writeln('Running job ' . get_class($job) . ' with ID ' . $job->getId());
  120. if ($output->isVerbose()) {
  121. $this->printJobInfo($job->getId(), $job, $output);
  122. }
  123. /** @psalm-suppress DeprecatedMethod Calling execute until it is removed, then will switch to start */
  124. $job->execute($this->jobList);
  125. $output->writeln('Job ' . $job->getId() . ' has finished', OutputInterface::VERBOSITY_VERBOSE);
  126. // clean up after unclean jobs
  127. $this->setupManager->tearDown();
  128. $this->tempManager->clean();
  129. $this->jobList->setLastJob($job);
  130. $this->jobList->unlockJob($job);
  131. if ($input->getOption('once') === true) {
  132. break;
  133. }
  134. }
  135. return 0;
  136. }
  137. private function printSummary(InputInterface $input, OutputInterface $output): void {
  138. if (!$output->isVeryVerbose()) {
  139. return;
  140. }
  141. $output->writeln('<comment>Summary</comment>');
  142. $counts = [];
  143. foreach ($this->jobList->countByClass() as $row) {
  144. $counts[] = $row;
  145. }
  146. $this->writeTableInOutputFormat($input, $output, $counts);
  147. }
  148. }