You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

146 lines
3.3 KiB

3 years ago
  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. /**
  29. * Base class for background jobs
  30. *
  31. * This is here if you want to do advanced stuff in your background jobs.
  32. * For the most common use cases have a look at QueuedJob and TimedJob
  33. *
  34. * @since 15.0.0
  35. */
  36. abstract class Job implements IJob {
  37. /** @var int $id */
  38. protected $id;
  39. /** @var int $lastRun */
  40. protected $lastRun;
  41. /** @var mixed $argument */
  42. protected $argument;
  43. /** @var ITimeFactory */
  44. protected $time;
  45. /**
  46. * @since 15.0.0
  47. */
  48. public function __construct(ITimeFactory $time) {
  49. $this->time = $time;
  50. }
  51. /**
  52. * The function to prepare the execution of the job.
  53. *
  54. *
  55. * @param IJobList $jobList
  56. * @param ILogger|null $logger
  57. *
  58. * @since 15.0.0
  59. */
  60. public function execute($jobList, ILogger $logger = null) {
  61. $jobList->setLastRun($this);
  62. if ($logger === null) {
  63. $logger = \OC::$server->getLogger();
  64. }
  65. try {
  66. $jobStartTime = $this->time->getTime();
  67. $logger->debug('Run ' . get_class($this) . ' job with ID ' . $this->getId(), ['app' => 'cron']);
  68. $this->run($this->argument);
  69. $timeTaken = $this->time->getTime() - $jobStartTime;
  70. $logger->debug('Finished ' . get_class($this) . ' job with ID ' . $this->getId() . ' in ' . $timeTaken . ' seconds', ['app' => 'cron']);
  71. $jobList->setExecutionTime($this, $timeTaken);
  72. } catch (\Exception $e) {
  73. if ($logger) {
  74. $logger->logException($e, [
  75. 'app' => 'core',
  76. 'message' => 'Error while running background job (class: ' . get_class($this) . ', arguments: ' . print_r($this->argument, true) . ')'
  77. ]);
  78. }
  79. }
  80. }
  81. /**
  82. * @since 15.0.0
  83. */
  84. final public function setId($id) {
  85. $this->id = $id;
  86. }
  87. /**
  88. * @since 15.0.0
  89. */
  90. final public function setLastRun($lastRun) {
  91. $this->lastRun = $lastRun;
  92. }
  93. /**
  94. * @since 15.0.0
  95. */
  96. public function setArgument($argument) {
  97. $this->argument = $argument;
  98. }
  99. /**
  100. * @since 15.0.0
  101. */
  102. final public function getId(): int {
  103. return $this->id;
  104. }
  105. /**
  106. * @since 15.0.0
  107. */
  108. final public function getLastRun(): int {
  109. return $this->lastRun;
  110. }
  111. /**
  112. * @since 15.0.0
  113. */
  114. public function getArgument() {
  115. return $this->argument;
  116. }
  117. /**
  118. * The actual function that is called to run the job
  119. *
  120. * @param $argument
  121. * @return mixed
  122. *
  123. * @since 15.0.0
  124. */
  125. abstract protected function run($argument);
  126. }