Job.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2018, Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCP\BackgroundJob;
  25. use OCP\AppFramework\Utility\ITimeFactory;
  26. use OCP\ILogger;
  27. /**
  28. * Base class for background jobs
  29. *
  30. * This is here if you want to do advanced stuff in your background jobs.
  31. * For the most common use cases have a look at QueuedJob and TimedJob
  32. *
  33. * @since 15.0.0
  34. */
  35. abstract class Job implements IJob {
  36. /** @var int $id */
  37. protected $id;
  38. /** @var int $lastRun */
  39. protected $lastRun;
  40. /** @var mixed $argument */
  41. protected $argument;
  42. /** @var ITimeFactory */
  43. protected $time;
  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. * @param IJobList $jobList
  54. * @param ILogger|null $logger
  55. *
  56. * @since 15.0.0
  57. */
  58. public function execute($jobList, ILogger $logger = null) {
  59. $jobList->setLastRun($this);
  60. if ($logger === null) {
  61. $logger = \OC::$server->getLogger();
  62. }
  63. try {
  64. $jobStartTime = $this->time->getTime();
  65. $logger->debug('Run ' . get_class($this) . ' job with ID ' . $this->getId(), ['app' => 'cron']);
  66. $this->run($this->argument);
  67. $timeTaken = $this->time->getTime() - $jobStartTime;
  68. $logger->debug('Finished ' . get_class($this) . ' job with ID ' . $this->getId() . ' in ' . $timeTaken . ' seconds', ['app' => 'cron']);
  69. $jobList->setExecutionTime($this, $timeTaken);
  70. } catch (\Exception $e) {
  71. if ($logger) {
  72. $logger->logException($e, [
  73. 'app' => 'core',
  74. 'message' => 'Error while running background job (class: ' . get_class($this) . ', arguments: ' . print_r($this->argument, true) . ')'
  75. ]);
  76. }
  77. }
  78. }
  79. /**
  80. * @since 15.0.0
  81. */
  82. final public function setId($id) {
  83. $this->id = $id;
  84. }
  85. /**
  86. * @since 15.0.0
  87. */
  88. final public function setLastRun($lastRun) {
  89. $this->lastRun = $lastRun;
  90. }
  91. /**
  92. * @since 15.0.0
  93. */
  94. public function setArgument($argument) {
  95. $this->argument = $argument;
  96. }
  97. /**
  98. * @since 15.0.0
  99. */
  100. final public function getId(): int {
  101. return $this->id;
  102. }
  103. /**
  104. * @since 15.0.0
  105. */
  106. final public function getLastRun(): int {
  107. return $this->lastRun;
  108. }
  109. /**
  110. * @since 15.0.0
  111. */
  112. public function getArgument() {
  113. return $this->argument;
  114. }
  115. /**
  116. * The actual function that is called to run the job
  117. *
  118. * @param $argument
  119. * @return mixed
  120. *
  121. * @since 15.0.0
  122. */
  123. abstract protected function run($argument);
  124. }