Job.php 5.1 KB

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