1
0

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. /**
  44. * @since 15.0.0
  45. */
  46. public function __construct(ITimeFactory $time) {
  47. $this->time = $time;
  48. }
  49. /**
  50. * The function to prepare the execution of the job.
  51. *
  52. * @return void
  53. *
  54. * @since 15.0.0
  55. * @deprecated since 25.0.0 Use start() instead. This method will be removed
  56. * with the ILogger interface
  57. */
  58. public function execute(IJobList $jobList, ?ILogger $logger = null) {
  59. $this->start($jobList);
  60. }
  61. /**
  62. * @inheritdoc
  63. * @since 25.0.0
  64. */
  65. public function start(IJobList $jobList): void {
  66. $jobList->setLastRun($this);
  67. $logger = \OCP\Server::get(LoggerInterface::class);
  68. try {
  69. $jobDetails = get_class($this) . ' (id: ' . $this->getId() . ', arguments: ' . json_encode($this->getArgument()) . ')';
  70. $jobStartTime = $this->time->getTime();
  71. $logger->debug('Starting job ' . $jobDetails, ['app' => 'cron']);
  72. $this->run($this->argument);
  73. $timeTaken = $this->time->getTime() - $jobStartTime;
  74. $logger->debug('Finished job ' . $jobDetails . ' in ' . $timeTaken . ' seconds', ['app' => 'cron']);
  75. $jobList->setExecutionTime($this, $timeTaken);
  76. } catch (\Throwable $e) {
  77. if ($logger) {
  78. $logger->error('Error while running background job ' . $jobDetails, [
  79. 'app' => 'core',
  80. 'exception' => $e,
  81. ]);
  82. }
  83. }
  84. }
  85. /**
  86. * @since 15.0.0
  87. */
  88. final public function setId(int $id) {
  89. $this->id = $id;
  90. }
  91. /**
  92. * @since 15.0.0
  93. */
  94. final public function setLastRun(int $lastRun) {
  95. $this->lastRun = $lastRun;
  96. }
  97. /**
  98. * @since 15.0.0
  99. */
  100. public function setArgument($argument) {
  101. $this->argument = $argument;
  102. }
  103. /**
  104. * @since 15.0.0
  105. */
  106. final public function getId(): int {
  107. return $this->id;
  108. }
  109. /**
  110. * @since 15.0.0
  111. */
  112. final public function getLastRun(): int {
  113. return $this->lastRun;
  114. }
  115. /**
  116. * @since 15.0.0
  117. */
  118. public function getArgument() {
  119. return $this->argument;
  120. }
  121. /**
  122. * Set this to false to prevent two Jobs from this class from running in parallel
  123. *
  124. * @param bool $allow
  125. * @return void
  126. * @since 27.0.0
  127. */
  128. public function setAllowParallelRuns(bool $allow): void {
  129. $this->allowParallelRuns = $allow;
  130. }
  131. /**
  132. * @return bool
  133. * @since 27.0.0
  134. */
  135. public function getAllowParallelRuns(): bool {
  136. return $this->allowParallelRuns;
  137. }
  138. /**
  139. * The actual function that is called to run the job
  140. *
  141. * @param $argument
  142. * @return void
  143. *
  144. * @since 15.0.0
  145. */
  146. abstract protected function run($argument);
  147. }