Job.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 {
  38. protected int $id = 0;
  39. protected int $lastRun = 0;
  40. protected $argument;
  41. protected ITimeFactory $time;
  42. /**
  43. * @since 15.0.0
  44. */
  45. public function __construct(ITimeFactory $time) {
  46. $this->time = $time;
  47. }
  48. /**
  49. * The function to prepare the execution of the job.
  50. *
  51. *
  52. * @param IJobList $jobList
  53. * @param ILogger|null $logger
  54. *
  55. * @since 15.0.0
  56. */
  57. public function execute(IJobList $jobList, ILogger $logger = null) {
  58. $this->start($jobList);
  59. }
  60. /**
  61. * @inheritdoc
  62. * @since 25.0.0
  63. */
  64. public function start(IJobList $jobList): void {
  65. $jobList->setLastRun($this);
  66. $logger = \OCP\Server::get(LoggerInterface::class);
  67. try {
  68. $jobStartTime = $this->time->getTime();
  69. $logger->debug('Run ' . get_class($this) . ' job with ID ' . $this->getId(), ['app' => 'cron']);
  70. $this->run($this->argument);
  71. $timeTaken = $this->time->getTime() - $jobStartTime;
  72. $logger->debug('Finished ' . get_class($this) . ' job with ID ' . $this->getId() . ' in ' . $timeTaken . ' seconds', ['app' => 'cron']);
  73. $jobList->setExecutionTime($this, $timeTaken);
  74. } catch (\Exception $e) {
  75. if ($logger) {
  76. $logger->error('Error while running background job (class: ' . get_class($this) . ', arguments: ' . print_r($this->argument, true) . ')', [
  77. 'app' => 'core',
  78. 'exception' => $e,
  79. ]);
  80. }
  81. }
  82. }
  83. /**
  84. * @since 15.0.0
  85. */
  86. final public function setId(int $id) {
  87. $this->id = $id;
  88. }
  89. /**
  90. * @since 15.0.0
  91. */
  92. final public function setLastRun(int $lastRun) {
  93. $this->lastRun = $lastRun;
  94. }
  95. /**
  96. * @since 15.0.0
  97. */
  98. public function setArgument($argument) {
  99. $this->argument = $argument;
  100. }
  101. /**
  102. * @since 15.0.0
  103. */
  104. final public function getId(): int {
  105. return $this->id;
  106. }
  107. /**
  108. * @since 15.0.0
  109. */
  110. final public function getLastRun(): int {
  111. return $this->lastRun;
  112. }
  113. /**
  114. * @since 15.0.0
  115. */
  116. public function getArgument() {
  117. return $this->argument;
  118. }
  119. /**
  120. * The actual function that is called to run the job
  121. *
  122. * @param $argument
  123. *
  124. * @since 15.0.0
  125. */
  126. abstract protected function run($argument);
  127. }