Job.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. $job->start($this->jobList);
  77. $job = $this->jobList->getById($jobId);
  78. if (($job === null) || ($lastRun !== $job->getLastRun())) {
  79. $output->writeln('<info>Job executed!</info>');
  80. $output->writeln('');
  81. if ($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 \OCP\BackgroundJob\TimedJob;
  101. if ($isTimedJob) {
  102. $output->writeln('Type: timed');
  103. } elseif ($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. }