1
0

Job.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2018, Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OCP\BackgroundJob;
  26. use OCP\AppFramework\Utility\ITimeFactory;
  27. use OCP\ILogger;
  28. use Psr\Log\LoggerInterface;
  29. /**
  30. * Base class for background jobs
  31. *
  32. * This is here if you want to do advanced stuff in your background jobs.
  33. * For the most common use cases have a look at QueuedJob and TimedJob
  34. *
  35. * @since 15.0.0
  36. */
  37. abstract class Job implements IJob, IParallelAwareJob {
  38. protected int $id = 0;
  39. protected int $lastRun = 0;
  40. protected $argument;
  41. protected ITimeFactory $time;
  42. protected bool $allowParallelRuns = true;
  43. private ?ILogger $logger = null;
  44. /**
  45. * @since 15.0.0
  46. */
  47. public function __construct(ITimeFactory $time) {
  48. $this->time = $time;
  49. }
  50. /**
  51. * The function to prepare the execution of the job.
  52. *
  53. *
  54. * @param IJobList $jobList
  55. * @param ILogger|null $logger
  56. *
  57. * @since 15.0.0
  58. */
  59. public function execute(IJobList $jobList, ILogger $logger = null) {
  60. $this->logger = $logger;
  61. $this->start($jobList);
  62. }
  63. /**
  64. * @inheritdoc
  65. * @since 25.0.0
  66. */
  67. public function start(IJobList $jobList): void {
  68. $jobList->setLastRun($this);
  69. $logger = $this->logger ?? \OCP\Server::get(LoggerInterface::class);
  70. if (!$this->getAllowParallelRuns() && $jobList->hasReservedJob(get_class($this))) {
  71. $logger->debug('Skipping ' . get_class($this) . ' job with ID ' . $this->getId() . ' because another job with the same class is already running', ['app' => 'cron']);
  72. return;
  73. }
  74. try {
  75. $jobStartTime = $this->time->getTime();
  76. $logger->debug('Run ' . get_class($this) . ' job with ID ' . $this->getId(), ['app' => 'cron']);
  77. $this->run($this->argument);
  78. $timeTaken = $this->time->getTime() - $jobStartTime;
  79. $logger->debug('Finished ' . get_class($this) . ' job with ID ' . $this->getId() . ' in ' . $timeTaken . ' seconds', ['app' => 'cron']);
  80. $jobList->setExecutionTime($this, $timeTaken);
  81. } catch (\Throwable $e) {
  82. if ($logger) {
  83. $logger->error('Error while running background job (class: ' . get_class($this) . ', arguments: ' . print_r($this->argument, true) . ')', [
  84. 'app' => 'core',
  85. 'exception' => $e,
  86. ]);
  87. }
  88. }
  89. }
  90. /**
  91. * @since 15.0.0
  92. */
  93. final public function setId(int $id) {
  94. $this->id = $id;
  95. }
  96. /**
  97. * @since 15.0.0
  98. */
  99. final public function setLastRun(int $lastRun) {
  100. $this->lastRun = $lastRun;
  101. }
  102. /**
  103. * @since 15.0.0
  104. */
  105. public function setArgument($argument) {
  106. $this->argument = $argument;
  107. }
  108. /**
  109. * @since 15.0.0
  110. */
  111. final public function getId(): int {
  112. return $this->id;
  113. }
  114. /**
  115. * @since 15.0.0
  116. */
  117. final public function getLastRun(): int {
  118. return $this->lastRun;
  119. }
  120. /**
  121. * @since 15.0.0
  122. */
  123. public function getArgument() {
  124. return $this->argument;
  125. }
  126. /**
  127. * Set this to false to prevent two Jobs from this class from running in parallel
  128. *
  129. * @param bool $allow
  130. * @return void
  131. * @since 27.0.0
  132. */
  133. public function setAllowParallelRuns(bool $allow): void {
  134. $this->allowParallelRuns = $allow;
  135. }
  136. /**
  137. * @return bool
  138. * @since 27.0.0
  139. */
  140. public function getAllowParallelRuns(): bool {
  141. return $this->allowParallelRuns;
  142. }
  143. /**
  144. * The actual function that is called to run the job
  145. *
  146. * @param $argument
  147. *
  148. * @since 15.0.0
  149. */
  150. abstract protected function run($argument);
  151. }