You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Job.php 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 OCP\BackgroundJob\IJob;
  26. use OCP\BackgroundJob\IJobList;
  27. use OCP\ILogger;
  28. use Symfony\Component\Console\Command\Command;
  29. use Symfony\Component\Console\Input\InputArgument;
  30. use Symfony\Component\Console\Input\InputInterface;
  31. use Symfony\Component\Console\Input\InputOption;
  32. use Symfony\Component\Console\Output\OutputInterface;
  33. class Job extends Command {
  34. protected IJobList $jobList;
  35. protected ILogger $logger;
  36. public function __construct(IJobList $jobList,
  37. ILogger $logger) {
  38. parent::__construct();
  39. $this->jobList = $jobList;
  40. $this->logger = $logger;
  41. }
  42. protected function configure(): void {
  43. $this
  44. ->setName('background-job:execute')
  45. ->setDescription('Execute a single background job manually')
  46. ->addArgument(
  47. 'job-id',
  48. InputArgument::REQUIRED,
  49. 'The ID of the job in the database'
  50. )
  51. ->addOption(
  52. 'force-execute',
  53. null,
  54. InputOption::VALUE_NONE,
  55. 'Force execute the background job, independent from last run and being reserved'
  56. )
  57. ;
  58. }
  59. protected function execute(InputInterface $input, OutputInterface $output): int {
  60. $jobId = (int) $input->getArgument('job-id');
  61. $job = $this->jobList->getById($jobId);
  62. if ($job === null) {
  63. $output->writeln('<error>Job with ID ' . $jobId . ' could not be found in the database</error>');
  64. return 1;
  65. }
  66. $this->printJobInfo($jobId, $job, $output);
  67. $output->writeln('');
  68. $lastRun = $job->getLastRun();
  69. if ($input->getOption('force-execute')) {
  70. $lastRun = 0;
  71. $output->writeln('<comment>Forcing execution of the job</comment>');
  72. $output->writeln('');
  73. $this->jobList->resetBackgroundJob($job);
  74. }
  75. $job = $this->jobList->getById($jobId);
  76. $job->execute($this->jobList, $this->logger);
  77. $job = $this->jobList->getById($jobId);
  78. if ($lastRun !== $job->getLastRun()) {
  79. $output->writeln('<info>Job executed!</info>');
  80. $output->writeln('');
  81. if ($job instanceof \OC\BackgroundJob\TimedJob || $job instanceof \OCP\BackgroundJob\TimedJob) {
  82. $this->printJobInfo($jobId, $job, $output);
  83. }
  84. } else {
  85. $output->writeln('<comment>Job was not executed because it is not due</comment>');
  86. $output->writeln('Specify the <question>--force-execute</question> option to run it anyway');
  87. }
  88. return 0;
  89. }
  90. protected function printJobInfo(int $jobId, IJob $job, OutputInterface$output): void {
  91. $row = $this->jobList->getDetailsById($jobId);
  92. $lastRun = new \DateTime();
  93. $lastRun->setTimestamp((int) $row['last_run']);
  94. $lastChecked = new \DateTime();
  95. $lastChecked->setTimestamp((int) $row['last_checked']);
  96. $reservedAt = new \DateTime();
  97. $reservedAt->setTimestamp((int) $row['reserved_at']);
  98. $output->writeln('Job class: ' . get_class($job));
  99. $output->writeln('Arguments: ' . json_encode($job->getArgument()));
  100. $isTimedJob = $job instanceof \OC\BackgroundJob\TimedJob || $job instanceof \OCP\BackgroundJob\TimedJob;
  101. if ($isTimedJob) {
  102. $output->writeln('Type: timed');
  103. } elseif ($job instanceof \OC\BackgroundJob\QueuedJob || $job instanceof \OCP\BackgroundJob\QueuedJob) {
  104. $output->writeln('Type: queued');
  105. } else {
  106. $output->writeln('Type: job');
  107. }
  108. $output->writeln('');
  109. $output->writeln('Last checked: ' . $lastChecked->format(\DateTimeInterface::ATOM));
  110. if ((int) $row['reserved_at'] === 0) {
  111. $output->writeln('Reserved at: -');
  112. } else {
  113. $output->writeln('Reserved at: <comment>' . $reservedAt->format(\DateTimeInterface::ATOM) . '</comment>');
  114. }
  115. $output->writeln('Last executed: ' . $lastRun->format(\DateTimeInterface::ATOM));
  116. $output->writeln('Last duration: ' . $row['execution_duration']);
  117. if ($isTimedJob) {
  118. $reflection = new \ReflectionClass($job);
  119. $intervalProperty = $reflection->getProperty('interval');
  120. $intervalProperty->setAccessible(true);
  121. $interval = $intervalProperty->getValue($job);
  122. $nextRun = new \DateTime();
  123. $nextRun->setTimestamp($row['last_run'] + $interval);
  124. if ($nextRun > new \DateTime()) {
  125. $output->writeln('Next execution: <comment>' . $nextRun->format(\DateTimeInterface::ATOM) . '</comment>');
  126. } else {
  127. $output->writeln('Next execution: <info>' . $nextRun->format(\DateTimeInterface::ATOM) . '</info>');
  128. }
  129. }
  130. }
  131. }