Job.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. if ($job === null) {
  77. $output->writeln('<error>Something went wrong when trying to retrieve Job with ID ' . $jobId . ' from database</error>');
  78. return 1;
  79. }
  80. $job->execute($this->jobList, $this->logger);
  81. $job = $this->jobList->getById($jobId);
  82. if (($job === null) || ($lastRun !== $job->getLastRun())) {
  83. $output->writeln('<info>Job executed!</info>');
  84. $output->writeln('');
  85. if ($job instanceof \OC\BackgroundJob\TimedJob || $job instanceof \OCP\BackgroundJob\TimedJob) {
  86. $this->printJobInfo($jobId, $job, $output);
  87. }
  88. } else {
  89. $output->writeln('<comment>Job was not executed because it is not due</comment>');
  90. $output->writeln('Specify the <question>--force-execute</question> option to run it anyway');
  91. }
  92. return 0;
  93. }
  94. protected function printJobInfo(int $jobId, IJob $job, OutputInterface$output): void {
  95. $row = $this->jobList->getDetailsById($jobId);
  96. $lastRun = new \DateTime();
  97. $lastRun->setTimestamp((int) $row['last_run']);
  98. $lastChecked = new \DateTime();
  99. $lastChecked->setTimestamp((int) $row['last_checked']);
  100. $reservedAt = new \DateTime();
  101. $reservedAt->setTimestamp((int) $row['reserved_at']);
  102. $output->writeln('Job class: ' . get_class($job));
  103. $output->writeln('Arguments: ' . json_encode($job->getArgument()));
  104. $isTimedJob = $job instanceof \OC\BackgroundJob\TimedJob || $job instanceof \OCP\BackgroundJob\TimedJob;
  105. if ($isTimedJob) {
  106. $output->writeln('Type: timed');
  107. } elseif ($job instanceof \OC\BackgroundJob\QueuedJob || $job instanceof \OCP\BackgroundJob\QueuedJob) {
  108. $output->writeln('Type: queued');
  109. } else {
  110. $output->writeln('Type: job');
  111. }
  112. $output->writeln('');
  113. $output->writeln('Last checked: ' . $lastChecked->format(\DateTimeInterface::ATOM));
  114. if ((int) $row['reserved_at'] === 0) {
  115. $output->writeln('Reserved at: -');
  116. } else {
  117. $output->writeln('Reserved at: <comment>' . $reservedAt->format(\DateTimeInterface::ATOM) . '</comment>');
  118. }
  119. $output->writeln('Last executed: ' . $lastRun->format(\DateTimeInterface::ATOM));
  120. $output->writeln('Last duration: ' . $row['execution_duration']);
  121. if ($isTimedJob) {
  122. $reflection = new \ReflectionClass($job);
  123. $intervalProperty = $reflection->getProperty('interval');
  124. $intervalProperty->setAccessible(true);
  125. $interval = $intervalProperty->getValue($job);
  126. $nextRun = new \DateTime();
  127. $nextRun->setTimestamp($row['last_run'] + $interval);
  128. if ($nextRun > new \DateTime()) {
  129. $output->writeln('Next execution: <comment>' . $nextRun->format(\DateTimeInterface::ATOM) . '</comment>');
  130. } else {
  131. $output->writeln('Next execution: <info>' . $nextRun->format(\DateTimeInterface::ATOM) . '</info>');
  132. }
  133. }
  134. }
  135. }