JobBase.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Core\Command\Background;
  8. use OCP\BackgroundJob\IJob;
  9. use OCP\BackgroundJob\IJobList;
  10. use Psr\Log\LoggerInterface;
  11. use Symfony\Component\Console\Output\OutputInterface;
  12. abstract class JobBase extends \OC\Core\Command\Base {
  13. public function __construct(
  14. protected IJobList $jobList,
  15. protected LoggerInterface $logger,
  16. ) {
  17. parent::__construct();
  18. }
  19. protected function printJobInfo(int $jobId, IJob $job, OutputInterface $output): void {
  20. $row = $this->jobList->getDetailsById($jobId);
  21. if ($row === null) {
  22. return;
  23. }
  24. $lastRun = new \DateTime();
  25. $lastRun->setTimestamp((int)$row['last_run']);
  26. $lastChecked = new \DateTime();
  27. $lastChecked->setTimestamp((int)$row['last_checked']);
  28. $reservedAt = new \DateTime();
  29. $reservedAt->setTimestamp((int)$row['reserved_at']);
  30. $output->writeln('Job class: ' . get_class($job));
  31. $output->writeln('Arguments: ' . json_encode($job->getArgument()));
  32. $isTimedJob = $job instanceof \OCP\BackgroundJob\TimedJob;
  33. if ($isTimedJob) {
  34. $output->writeln('Type: timed');
  35. } elseif ($job instanceof \OCP\BackgroundJob\QueuedJob) {
  36. $output->writeln('Type: queued');
  37. } else {
  38. $output->writeln('Type: job');
  39. }
  40. $output->writeln('');
  41. $output->writeln('Last checked: ' . $lastChecked->format(\DateTimeInterface::ATOM));
  42. if ((int)$row['reserved_at'] === 0) {
  43. $output->writeln('Reserved at: -');
  44. } else {
  45. $output->writeln('Reserved at: <comment>' . $reservedAt->format(\DateTimeInterface::ATOM) . '</comment>');
  46. }
  47. $output->writeln('Last executed: ' . $lastRun->format(\DateTimeInterface::ATOM));
  48. $output->writeln('Last duration: ' . $row['execution_duration']);
  49. if ($isTimedJob) {
  50. $reflection = new \ReflectionClass($job);
  51. $intervalProperty = $reflection->getProperty('interval');
  52. $intervalProperty->setAccessible(true);
  53. $interval = $intervalProperty->getValue($job);
  54. $nextRun = new \DateTime();
  55. $nextRun->setTimestamp((int)$row['last_run'] + $interval);
  56. if ($nextRun > new \DateTime()) {
  57. $output->writeln('Next execution: <comment>' . $nextRun->format(\DateTimeInterface::ATOM) . '</comment>');
  58. } else {
  59. $output->writeln('Next execution: <info>' . $nextRun->format(\DateTimeInterface::ATOM) . '</info>');
  60. }
  61. }
  62. }
  63. }