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.

92 lines
2.4 KiB

3 years ago
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Daniel Kesselberg <mail@danielkesselberg.de>
  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\BackgroundJob\IJobList;
  29. use OCP\ILogger;
  30. abstract class Job implements IJob {
  31. /** @var int */
  32. protected $id;
  33. /** @var int */
  34. protected $lastRun;
  35. /** @var mixed */
  36. protected $argument;
  37. public function execute(IJobList $jobList, ILogger $logger = null) {
  38. $jobList->setLastRun($this);
  39. if ($logger === null) {
  40. $logger = \OC::$server->getLogger();
  41. }
  42. try {
  43. $jobStartTime = time();
  44. $logger->debug('Run ' . get_class($this) . ' job with ID ' . $this->getId(), ['app' => 'cron']);
  45. $this->run($this->argument);
  46. $timeTaken = time() - $jobStartTime;
  47. $logger->debug('Finished ' . get_class($this) . ' job with ID ' . $this->getId() . ' in ' . $timeTaken . ' seconds', ['app' => 'cron']);
  48. $jobList->setExecutionTime($this, $timeTaken);
  49. } catch (\Throwable $e) {
  50. if ($logger) {
  51. $logger->logException($e, [
  52. 'app' => 'core',
  53. 'message' => 'Error while running background job (class: ' . get_class($this) . ', arguments: ' . print_r($this->argument, true) . ')'
  54. ]);
  55. }
  56. }
  57. }
  58. abstract protected function run($argument);
  59. public function setId(int $id) {
  60. $this->id = $id;
  61. }
  62. public function setLastRun(int $lastRun) {
  63. $this->lastRun = $lastRun;
  64. }
  65. public function setArgument($argument) {
  66. $this->argument = $argument;
  67. }
  68. public function getId() {
  69. return $this->id;
  70. }
  71. public function getLastRun() {
  72. return $this->lastRun;
  73. }
  74. public function getArgument() {
  75. return $this->argument;
  76. }
  77. }