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.

Job.php 2.4KB

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