JobBase.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2022 Julius Härtl <jus@bitgrid.net>
  5. *
  6. * @author Julius Härtl <jus@bitgrid.net>
  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. namespace OC\Core\Command\Background;
  24. use OCP\BackgroundJob\IJob;
  25. use OCP\BackgroundJob\IJobList;
  26. use Psr\Log\LoggerInterface;
  27. use Symfony\Component\Console\Output\OutputInterface;
  28. abstract class JobBase extends \OC\Core\Command\Base {
  29. public function __construct(
  30. protected IJobList $jobList,
  31. protected LoggerInterface $logger
  32. ) {
  33. parent::__construct();
  34. }
  35. protected function printJobInfo(int $jobId, IJob $job, OutputInterface $output): void {
  36. $row = $this->jobList->getDetailsById($jobId);
  37. if ($row === null) {
  38. return;
  39. }
  40. $lastRun = new \DateTime();
  41. $lastRun->setTimestamp((int) $row['last_run']);
  42. $lastChecked = new \DateTime();
  43. $lastChecked->setTimestamp((int) $row['last_checked']);
  44. $reservedAt = new \DateTime();
  45. $reservedAt->setTimestamp((int) $row['reserved_at']);
  46. $output->writeln('Job class: ' . get_class($job));
  47. $output->writeln('Arguments: ' . json_encode($job->getArgument()));
  48. $isTimedJob = $job instanceof \OCP\BackgroundJob\TimedJob;
  49. if ($isTimedJob) {
  50. $output->writeln('Type: timed');
  51. } elseif ($job instanceof \OCP\BackgroundJob\QueuedJob) {
  52. $output->writeln('Type: queued');
  53. } else {
  54. $output->writeln('Type: job');
  55. }
  56. $output->writeln('');
  57. $output->writeln('Last checked: ' . $lastChecked->format(\DateTimeInterface::ATOM));
  58. if ((int) $row['reserved_at'] === 0) {
  59. $output->writeln('Reserved at: -');
  60. } else {
  61. $output->writeln('Reserved at: <comment>' . $reservedAt->format(\DateTimeInterface::ATOM) . '</comment>');
  62. }
  63. $output->writeln('Last executed: ' . $lastRun->format(\DateTimeInterface::ATOM));
  64. $output->writeln('Last duration: ' . $row['execution_duration']);
  65. if ($isTimedJob) {
  66. $reflection = new \ReflectionClass($job);
  67. $intervalProperty = $reflection->getProperty('interval');
  68. $intervalProperty->setAccessible(true);
  69. $interval = $intervalProperty->getValue($job);
  70. $nextRun = new \DateTime();
  71. $nextRun->setTimestamp((int)$row['last_run'] + $interval);
  72. if ($nextRun > new \DateTime()) {
  73. $output->writeln('Next execution: <comment>' . $nextRun->format(\DateTimeInterface::ATOM) . '</comment>');
  74. } else {
  75. $output->writeln('Next execution: <info>' . $nextRun->format(\DateTimeInterface::ATOM) . '</info>');
  76. }
  77. }
  78. }
  79. }