Job.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCP\BackgroundJob;
  8. use OCP\AppFramework\Utility\ITimeFactory;
  9. use OCP\ILogger;
  10. use Psr\Log\LoggerInterface;
  11. /**
  12. * Base class for background jobs
  13. *
  14. * This is here if you want to do advanced stuff in your background jobs.
  15. * For the most common use cases have a look at QueuedJob and TimedJob
  16. *
  17. * @since 15.0.0
  18. */
  19. abstract class Job implements IJob, IParallelAwareJob {
  20. protected int $id = 0;
  21. protected int $lastRun = 0;
  22. protected $argument;
  23. protected ITimeFactory $time;
  24. protected bool $allowParallelRuns = true;
  25. /**
  26. * @since 15.0.0
  27. */
  28. public function __construct(ITimeFactory $time) {
  29. $this->time = $time;
  30. }
  31. /**
  32. * The function to prepare the execution of the job.
  33. *
  34. * @return void
  35. *
  36. * @since 15.0.0
  37. * @deprecated since 25.0.0 Use start() instead. This method will be removed
  38. * with the ILogger interface
  39. */
  40. public function execute(IJobList $jobList, ?ILogger $logger = null) {
  41. $this->start($jobList);
  42. }
  43. /**
  44. * @inheritdoc
  45. * @since 25.0.0
  46. */
  47. public function start(IJobList $jobList): void {
  48. $jobList->setLastRun($this);
  49. $logger = \OCP\Server::get(LoggerInterface::class);
  50. try {
  51. $jobDetails = get_class($this) . ' (id: ' . $this->getId() . ', arguments: ' . json_encode($this->getArgument()) . ')';
  52. $jobStartTime = $this->time->getTime();
  53. $logger->debug('Starting job ' . $jobDetails, ['app' => 'cron']);
  54. $this->run($this->argument);
  55. $timeTaken = $this->time->getTime() - $jobStartTime;
  56. $logger->debug('Finished job ' . $jobDetails . ' in ' . $timeTaken . ' seconds', ['app' => 'cron']);
  57. $jobList->setExecutionTime($this, $timeTaken);
  58. } catch (\Throwable $e) {
  59. if ($logger) {
  60. $logger->error('Error while running background job ' . $jobDetails, [
  61. 'app' => 'core',
  62. 'exception' => $e,
  63. ]);
  64. }
  65. }
  66. }
  67. /**
  68. * @since 15.0.0
  69. */
  70. final public function setId(int $id) {
  71. $this->id = $id;
  72. }
  73. /**
  74. * @since 15.0.0
  75. */
  76. final public function setLastRun(int $lastRun) {
  77. $this->lastRun = $lastRun;
  78. }
  79. /**
  80. * @since 15.0.0
  81. */
  82. public function setArgument($argument) {
  83. $this->argument = $argument;
  84. }
  85. /**
  86. * @since 15.0.0
  87. */
  88. final public function getId(): int {
  89. return $this->id;
  90. }
  91. /**
  92. * @since 15.0.0
  93. */
  94. final public function getLastRun(): int {
  95. return $this->lastRun;
  96. }
  97. /**
  98. * @since 15.0.0
  99. */
  100. public function getArgument() {
  101. return $this->argument;
  102. }
  103. /**
  104. * Set this to false to prevent two Jobs from this class from running in parallel
  105. *
  106. * @param bool $allow
  107. * @return void
  108. * @since 27.0.0
  109. */
  110. public function setAllowParallelRuns(bool $allow): void {
  111. $this->allowParallelRuns = $allow;
  112. }
  113. /**
  114. * @return bool
  115. * @since 27.0.0
  116. */
  117. public function getAllowParallelRuns(): bool {
  118. return $this->allowParallelRuns;
  119. }
  120. /**
  121. * The actual function that is called to run the job
  122. *
  123. * @param $argument
  124. * @return void
  125. *
  126. * @since 15.0.0
  127. */
  128. abstract protected function run($argument);
  129. }