Job.php 2.6 KB

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