Job.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. try {
  47. $this->run($this->argument);
  48. } catch (\Exception $e) {
  49. if ($logger) {
  50. $logger->logException($e, [
  51. 'app' => 'core',
  52. 'message' => 'Error while running background job (class: ' . get_class($this) . ', arguments: ' . print_r($this->argument, true) . ')'
  53. ]);
  54. }
  55. }
  56. }
  57. abstract protected function run($argument);
  58. public function setId($id) {
  59. $this->id = $id;
  60. }
  61. public function setLastRun($lastRun) {
  62. $this->lastRun = $lastRun;
  63. }
  64. public function setArgument($argument) {
  65. $this->argument = $argument;
  66. }
  67. public function getId() {
  68. return $this->id;
  69. }
  70. public function getLastRun() {
  71. return $this->lastRun;
  72. }
  73. public function getArgument() {
  74. return $this->argument;
  75. }
  76. }