JobWorker.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 | InputArgument::IS_ARRAY,
  51. 'The classes of the jobs to look for in the database'
  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. $jobClasses = $input->getArgument('job-classes');
  70. $jobClasses = empty($jobClasses) ? null : $jobClasses;
  71. if ($jobClasses !== null) {
  72. // at least one class is invalid
  73. foreach ($jobClasses as $jobClass) {
  74. if (!class_exists($jobClass)) {
  75. $output->writeln('<error>Invalid job class: ' . $jobClass . '</error>');
  76. return 1;
  77. }
  78. }
  79. }
  80. while (true) {
  81. // Handle canceling of the process
  82. try {
  83. $this->abortIfInterrupted();
  84. } catch (InterruptedException $e) {
  85. $output->writeln('<info>Background job worker stopped</info>');
  86. break;
  87. }
  88. $this->printSummary($input, $output);
  89. usleep(50000);
  90. $job = $this->jobList->getNext(false, $jobClasses);
  91. if (!$job) {
  92. if ($input->getOption('once') === true) {
  93. if ($jobClasses === null) {
  94. $output->writeln('No job is currently queued', OutputInterface::VERBOSITY_VERBOSE);
  95. } else {
  96. $output->writeln('No job of classes [' . implode(', ', $jobClasses) . '] is currently queued', OutputInterface::VERBOSITY_VERBOSE);
  97. }
  98. $output->writeln('Exiting...', OutputInterface::VERBOSITY_VERBOSE);
  99. break;
  100. }
  101. $output->writeln('Waiting for new jobs to be queued', OutputInterface::VERBOSITY_VERBOSE);
  102. // Re-check interval for new jobs
  103. sleep(1);
  104. continue;
  105. }
  106. $output->writeln('Running job ' . get_class($job) . ' with ID ' . $job->getId());
  107. if ($output->isVerbose()) {
  108. $this->printJobInfo($job->getId(), $job, $output);
  109. }
  110. /** @psalm-suppress DeprecatedMethod Calling execute until it is removed, then will switch to start */
  111. $job->execute($this->jobList);
  112. $output->writeln('Job ' . $job->getId() . ' has finished', OutputInterface::VERBOSITY_VERBOSE);
  113. // clean up after unclean jobs
  114. $this->setupManager->tearDown();
  115. $this->tempManager->clean();
  116. $this->jobList->setLastJob($job);
  117. $this->jobList->unlockJob($job);
  118. if ($input->getOption('once') === true) {
  119. break;
  120. }
  121. }
  122. return 0;
  123. }
  124. private function printSummary(InputInterface $input, OutputInterface $output): void {
  125. if (!$output->isVeryVerbose()) {
  126. return;
  127. }
  128. $output->writeln('<comment>Summary</comment>');
  129. $counts = [];
  130. foreach ($this->jobList->countByClass() as $row) {
  131. $counts[] = $row;
  132. }
  133. $this->writeTableInOutputFormat($input, $output, $counts);
  134. }
  135. }