Job.php 5.2 KB

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