Job.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. try {
  71. $jobStartTime = $this->time->getTime();
  72. $logger->debug('Run ' . get_class($this) . ' job with ID ' . $this->getId(), ['app' => 'cron']);
  73. $this->run($this->argument);
  74. $timeTaken = $this->time->getTime() - $jobStartTime;
  75. $logger->debug('Finished ' . get_class($this) . ' job with ID ' . $this->getId() . ' in ' . $timeTaken . ' seconds', ['app' => 'cron']);
  76. $jobList->setExecutionTime($this, $timeTaken);
  77. } catch (\Throwable $e) {
  78. if ($logger) {
  79. $logger->error('Error while running background job (class: ' . get_class($this) . ', arguments: ' . print_r($this->argument, true) . ')', [
  80. 'app' => 'core',
  81. 'exception' => $e,
  82. ]);
  83. }
  84. }
  85. }
  86. /**
  87. * @since 15.0.0
  88. */
  89. final public function setId(int $id) {
  90. $this->id = $id;
  91. }
  92. /**
  93. * @since 15.0.0
  94. */
  95. final public function setLastRun(int $lastRun) {
  96. $this->lastRun = $lastRun;
  97. }
  98. /**
  99. * @since 15.0.0
  100. */
  101. public function setArgument($argument) {
  102. $this->argument = $argument;
  103. }
  104. /**
  105. * @since 15.0.0
  106. */
  107. final public function getId(): int {
  108. return $this->id;
  109. }
  110. /**
  111. * @since 15.0.0
  112. */
  113. final public function getLastRun(): int {
  114. return $this->lastRun;
  115. }
  116. /**
  117. * @since 15.0.0
  118. */
  119. public function getArgument() {
  120. return $this->argument;
  121. }
  122. /**
  123. * Set this to false to prevent two Jobs from this class from running in parallel
  124. *
  125. * @param bool $allow
  126. * @return void
  127. * @since 27.0.0
  128. */
  129. public function setAllowParallelRuns(bool $allow): void {
  130. $this->allowParallelRuns = $allow;
  131. }
  132. /**
  133. * @return bool
  134. * @since 27.0.0
  135. */
  136. public function getAllowParallelRuns(): bool {
  137. return $this->allowParallelRuns;
  138. }
  139. /**
  140. * The actual function that is called to run the job
  141. *
  142. * @param $argument
  143. *
  144. * @since 15.0.0
  145. */
  146. abstract protected function run($argument);
  147. }