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.

168 lines
5.6 KiB

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