@@ -37,107 +37,106 @@ |
||
| 37 | 37 | */ |
| 38 | 38 | abstract class Job implements IJob { |
| 39 | 39 | |
| 40 | - /** @var int $id */ |
|
| 41 | - protected $id; |
|
| 42 | - |
|
| 43 | - /** @var int $lastRun */ |
|
| 44 | - protected $lastRun; |
|
| 45 | - |
|
| 46 | - /** @var mixed $argument */ |
|
| 47 | - protected $argument; |
|
| 48 | - |
|
| 49 | - /** @var ITimeFactory */ |
|
| 50 | - protected $time; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * @since 15.0.0 |
|
| 54 | - */ |
|
| 55 | - public function __construct(ITimeFactory $time) { |
|
| 56 | - $this->time = $time; |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * The function to prepare the execution of the job. |
|
| 61 | - |
|
| 62 | - * |
|
| 63 | - * @param IJobList $jobList |
|
| 64 | - * @param ILogger|null $logger |
|
| 65 | - * |
|
| 66 | - * @since 15.0.0 |
|
| 67 | - */ |
|
| 68 | - public function execute($jobList, ILogger $logger = null) { |
|
| 69 | - $jobList->setLastRun($this); |
|
| 70 | - if ($logger === null) { |
|
| 71 | - $logger = \OC::$server->getLogger(); |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - try { |
|
| 75 | - $jobStartTime = $this->time->getTime(); |
|
| 76 | - $logger->debug('Run ' . get_class($this) . ' job with ID ' . $this->getId(), ['app' => 'cron']); |
|
| 77 | - $this->run($this->argument); |
|
| 78 | - $timeTaken = $this->time->getTime() - $jobStartTime; |
|
| 79 | - |
|
| 80 | - $logger->debug('Finished ' . get_class($this) . ' job with ID ' . $this->getId() . ' in ' . $timeTaken . ' seconds', ['app' => 'cron']); |
|
| 81 | - $jobList->setExecutionTime($this, $timeTaken); |
|
| 82 | - } catch (\Exception $e) { |
|
| 83 | - if ($logger) { |
|
| 84 | - $logger->logException($e, [ |
|
| 85 | - 'app' => 'core', |
|
| 86 | - 'message' => 'Error while running background job (class: ' . get_class($this) . ', arguments: ' . print_r($this->argument, true) . ')' |
|
| 87 | - ]); |
|
| 88 | - } |
|
| 89 | - } |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * @since 15.0.0 |
|
| 94 | - */ |
|
| 95 | - final public function setId($id) { |
|
| 96 | - $this->id = $id; |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - /** |
|
| 100 | - * @since 15.0.0 |
|
| 101 | - */ |
|
| 102 | - final public function setLastRun($lastRun) { |
|
| 103 | - $this->lastRun = $lastRun; |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - /** |
|
| 107 | - * @since 15.0.0 |
|
| 108 | - */ |
|
| 109 | - public function setArgument($argument) { |
|
| 110 | - $this->argument = $argument; |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - /** |
|
| 114 | - * @since 15.0.0 |
|
| 115 | - */ |
|
| 116 | - final public function getId(): int { |
|
| 117 | - return $this->id; |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - /** |
|
| 121 | - * @since 15.0.0 |
|
| 122 | - */ |
|
| 123 | - final public function getLastRun(): int { |
|
| 124 | - return $this->lastRun; |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - /** |
|
| 128 | - * @since 15.0.0 |
|
| 129 | - */ |
|
| 130 | - public function getArgument() { |
|
| 131 | - return $this->argument; |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - /** |
|
| 135 | - * The actual function that is called to run the job |
|
| 136 | - * |
|
| 137 | - * @param $argument |
|
| 138 | - * @return mixed |
|
| 139 | - * |
|
| 140 | - * @since 15.0.0 |
|
| 141 | - */ |
|
| 142 | - abstract protected function run($argument); |
|
| 40 | + /** @var int $id */ |
|
| 41 | + protected $id; |
|
| 42 | + |
|
| 43 | + /** @var int $lastRun */ |
|
| 44 | + protected $lastRun; |
|
| 45 | + |
|
| 46 | + /** @var mixed $argument */ |
|
| 47 | + protected $argument; |
|
| 48 | + |
|
| 49 | + /** @var ITimeFactory */ |
|
| 50 | + protected $time; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * @since 15.0.0 |
|
| 54 | + */ |
|
| 55 | + public function __construct(ITimeFactory $time) { |
|
| 56 | + $this->time = $time; |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * The function to prepare the execution of the job. |
|
| 61 | + * |
|
| 62 | + * @param IJobList $jobList |
|
| 63 | + * @param ILogger|null $logger |
|
| 64 | + * |
|
| 65 | + * @since 15.0.0 |
|
| 66 | + */ |
|
| 67 | + public function execute($jobList, ILogger $logger = null) { |
|
| 68 | + $jobList->setLastRun($this); |
|
| 69 | + if ($logger === null) { |
|
| 70 | + $logger = \OC::$server->getLogger(); |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + try { |
|
| 74 | + $jobStartTime = $this->time->getTime(); |
|
| 75 | + $logger->debug('Run ' . get_class($this) . ' job with ID ' . $this->getId(), ['app' => 'cron']); |
|
| 76 | + $this->run($this->argument); |
|
| 77 | + $timeTaken = $this->time->getTime() - $jobStartTime; |
|
| 78 | + |
|
| 79 | + $logger->debug('Finished ' . get_class($this) . ' job with ID ' . $this->getId() . ' in ' . $timeTaken . ' seconds', ['app' => 'cron']); |
|
| 80 | + $jobList->setExecutionTime($this, $timeTaken); |
|
| 81 | + } catch (\Exception $e) { |
|
| 82 | + if ($logger) { |
|
| 83 | + $logger->logException($e, [ |
|
| 84 | + 'app' => 'core', |
|
| 85 | + 'message' => 'Error while running background job (class: ' . get_class($this) . ', arguments: ' . print_r($this->argument, true) . ')' |
|
| 86 | + ]); |
|
| 87 | + } |
|
| 88 | + } |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * @since 15.0.0 |
|
| 93 | + */ |
|
| 94 | + final public function setId($id) { |
|
| 95 | + $this->id = $id; |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * @since 15.0.0 |
|
| 100 | + */ |
|
| 101 | + final public function setLastRun($lastRun) { |
|
| 102 | + $this->lastRun = $lastRun; |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + /** |
|
| 106 | + * @since 15.0.0 |
|
| 107 | + */ |
|
| 108 | + public function setArgument($argument) { |
|
| 109 | + $this->argument = $argument; |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * @since 15.0.0 |
|
| 114 | + */ |
|
| 115 | + final public function getId(): int { |
|
| 116 | + return $this->id; |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * @since 15.0.0 |
|
| 121 | + */ |
|
| 122 | + final public function getLastRun(): int { |
|
| 123 | + return $this->lastRun; |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * @since 15.0.0 |
|
| 128 | + */ |
|
| 129 | + public function getArgument() { |
|
| 130 | + return $this->argument; |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + /** |
|
| 134 | + * The actual function that is called to run the job |
|
| 135 | + * |
|
| 136 | + * @param $argument |
|
| 137 | + * @return mixed |
|
| 138 | + * |
|
| 139 | + * @since 15.0.0 |
|
| 140 | + */ |
|
| 141 | + abstract protected function run($argument); |
|
| 143 | 142 | } |
@@ -33,16 +33,16 @@ |
||
| 33 | 33 | */ |
| 34 | 34 | abstract class QueuedJob extends Job { |
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * run the job, then remove it from the joblist |
|
| 38 | - * |
|
| 39 | - * @param IJobList $jobList |
|
| 40 | - * @param ILogger|null $logger |
|
| 41 | - * |
|
| 42 | - * @since 15.0.0 |
|
| 43 | - */ |
|
| 44 | - final public function execute($jobList, ILogger $logger = null) { |
|
| 45 | - $jobList->remove($this, $this->argument); |
|
| 46 | - parent::execute($jobList, $logger); |
|
| 47 | - } |
|
| 36 | + /** |
|
| 37 | + * run the job, then remove it from the joblist |
|
| 38 | + * |
|
| 39 | + * @param IJobList $jobList |
|
| 40 | + * @param ILogger|null $logger |
|
| 41 | + * |
|
| 42 | + * @since 15.0.0 |
|
| 43 | + */ |
|
| 44 | + final public function execute($jobList, ILogger $logger = null) { |
|
| 45 | + $jobList->remove($this, $this->argument); |
|
| 46 | + parent::execute($jobList, $logger); |
|
| 47 | + } |
|
| 48 | 48 | } |
@@ -34,29 +34,29 @@ |
||
| 34 | 34 | * @since 15.0.0 |
| 35 | 35 | */ |
| 36 | 36 | abstract class TimedJob extends Job { |
| 37 | - /** @var int */ |
|
| 38 | - protected $interval = 0; |
|
| 37 | + /** @var int */ |
|
| 38 | + protected $interval = 0; |
|
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * set the interval for the job |
|
| 42 | - * |
|
| 43 | - * @since 15.0.0 |
|
| 44 | - */ |
|
| 45 | - public function setInterval(int $interval) { |
|
| 46 | - $this->interval = $interval; |
|
| 47 | - } |
|
| 40 | + /** |
|
| 41 | + * set the interval for the job |
|
| 42 | + * |
|
| 43 | + * @since 15.0.0 |
|
| 44 | + */ |
|
| 45 | + public function setInterval(int $interval) { |
|
| 46 | + $this->interval = $interval; |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | - /** |
|
| 50 | - * run the job if the last run is is more than the interval ago |
|
| 51 | - * |
|
| 52 | - * @param JobList $jobList |
|
| 53 | - * @param ILogger|null $logger |
|
| 54 | - * |
|
| 55 | - * @since 15.0.0 |
|
| 56 | - */ |
|
| 57 | - final public function execute($jobList, ILogger $logger = null) { |
|
| 58 | - if (($this->time->getTime() - $this->lastRun) > $this->interval) { |
|
| 59 | - parent::execute($jobList, $logger); |
|
| 60 | - } |
|
| 61 | - } |
|
| 49 | + /** |
|
| 50 | + * run the job if the last run is is more than the interval ago |
|
| 51 | + * |
|
| 52 | + * @param JobList $jobList |
|
| 53 | + * @param ILogger|null $logger |
|
| 54 | + * |
|
| 55 | + * @since 15.0.0 |
|
| 56 | + */ |
|
| 57 | + final public function execute($jobList, ILogger $logger = null) { |
|
| 58 | + if (($this->time->getTime() - $this->lastRun) > $this->interval) { |
|
| 59 | + parent::execute($jobList, $logger); |
|
| 60 | + } |
|
| 61 | + } |
|
| 62 | 62 | } |