Job.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Lukas Reschke <lukas@statuscode.ch>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Noveen Sachdeva <noveen.sachdeva@research.iiit.ac.in>
  8. * @author Robin Appelman <robin@icewind.nl>
  9. * @author Thomas Müller <thomas.mueller@tmit.eu>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OC\BackgroundJob;
  27. use OCP\BackgroundJob\IJob;
  28. use OCP\ILogger;
  29. abstract class Job implements IJob {
  30. /**
  31. * @var int $id
  32. */
  33. protected $id;
  34. /**
  35. * @var int $lastRun
  36. */
  37. protected $lastRun;
  38. /**
  39. * @var mixed $argument
  40. */
  41. protected $argument;
  42. /**
  43. * @param JobList $jobList
  44. * @param ILogger|null $logger
  45. */
  46. public function execute($jobList, ILogger $logger = null) {
  47. $jobList->setLastRun($this);
  48. if ($logger === null) {
  49. $logger = \OC::$server->getLogger();
  50. }
  51. try {
  52. $jobStartTime = time();
  53. $logger->debug('Run ' . get_class($this) . ' job with ID ' . $this->getId(), ['app' => 'cron']);
  54. $this->run($this->argument);
  55. $timeTaken = time() - $jobStartTime;
  56. $logger->debug('Finished ' . get_class($this) . ' job with ID ' . $this->getId() . ' in ' . $timeTaken . ' seconds', ['app' => 'cron']);
  57. $jobList->setExecutionTime($this, $timeTaken);
  58. } catch (\Exception $e) {
  59. if ($logger) {
  60. $logger->logException($e, [
  61. 'app' => 'core',
  62. 'message' => 'Error while running background job (class: ' . get_class($this) . ', arguments: ' . print_r($this->argument, true) . ')'
  63. ]);
  64. }
  65. }
  66. }
  67. abstract protected function run($argument);
  68. public function setId($id) {
  69. $this->id = $id;
  70. }
  71. public function setLastRun($lastRun) {
  72. $this->lastRun = $lastRun;
  73. }
  74. public function setArgument($argument) {
  75. $this->argument = $argument;
  76. }
  77. public function getId() {
  78. return $this->id;
  79. }
  80. public function getLastRun() {
  81. return $this->lastRun;
  82. }
  83. public function getArgument() {
  84. return $this->argument;
  85. }
  86. }