@@ -320,7 +320,7 @@ |
||
| 320 | 320 | |
| 321 | 321 | /** |
| 322 | 322 | * @param IJob $job |
| 323 | - * @param $timeTaken |
|
| 323 | + * @param integer $timeTaken |
|
| 324 | 324 | */ |
| 325 | 325 | public function setExecutionTime(IJob $job, $timeTaken) { |
| 326 | 326 | $query = $this->connection->getQueryBuilder(); |
@@ -36,297 +36,297 @@ |
||
| 36 | 36 | |
| 37 | 37 | class JobList implements IJobList { |
| 38 | 38 | |
| 39 | - /** @var IDBConnection */ |
|
| 40 | - protected $connection; |
|
| 41 | - |
|
| 42 | - /**@var IConfig */ |
|
| 43 | - protected $config; |
|
| 44 | - |
|
| 45 | - /**@var ITimeFactory */ |
|
| 46 | - protected $timeFactory; |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * @param IDBConnection $connection |
|
| 50 | - * @param IConfig $config |
|
| 51 | - * @param ITimeFactory $timeFactory |
|
| 52 | - */ |
|
| 53 | - public function __construct(IDBConnection $connection, IConfig $config, ITimeFactory $timeFactory) { |
|
| 54 | - $this->connection = $connection; |
|
| 55 | - $this->config = $config; |
|
| 56 | - $this->timeFactory = $timeFactory; |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * @param IJob|string $job |
|
| 61 | - * @param mixed $argument |
|
| 62 | - */ |
|
| 63 | - public function add($job, $argument = null) { |
|
| 64 | - if (!$this->has($job, $argument)) { |
|
| 65 | - if ($job instanceof IJob) { |
|
| 66 | - $class = get_class($job); |
|
| 67 | - } else { |
|
| 68 | - $class = $job; |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - $argument = json_encode($argument); |
|
| 72 | - if (strlen($argument) > 4000) { |
|
| 73 | - throw new \InvalidArgumentException('Background job arguments can\'t exceed 4000 characters (json encoded)'); |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - $query = $this->connection->getQueryBuilder(); |
|
| 77 | - $query->insert('jobs') |
|
| 78 | - ->values([ |
|
| 79 | - 'class' => $query->createNamedParameter($class), |
|
| 80 | - 'argument' => $query->createNamedParameter($argument), |
|
| 81 | - 'last_run' => $query->createNamedParameter(0, IQueryBuilder::PARAM_INT), |
|
| 82 | - 'last_checked' => $query->createNamedParameter($this->timeFactory->getTime(), IQueryBuilder::PARAM_INT), |
|
| 83 | - ]); |
|
| 84 | - $query->execute(); |
|
| 85 | - } |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * @param IJob|string $job |
|
| 90 | - * @param mixed $argument |
|
| 91 | - */ |
|
| 92 | - public function remove($job, $argument = null) { |
|
| 93 | - if ($job instanceof IJob) { |
|
| 94 | - $class = get_class($job); |
|
| 95 | - } else { |
|
| 96 | - $class = $job; |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - $query = $this->connection->getQueryBuilder(); |
|
| 100 | - $query->delete('jobs') |
|
| 101 | - ->where($query->expr()->eq('class', $query->createNamedParameter($class))); |
|
| 102 | - if (!is_null($argument)) { |
|
| 103 | - $argument = json_encode($argument); |
|
| 104 | - $query->andWhere($query->expr()->eq('argument', $query->createNamedParameter($argument))); |
|
| 105 | - } |
|
| 106 | - $query->execute(); |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * @param int $id |
|
| 111 | - */ |
|
| 112 | - protected function removeById($id) { |
|
| 113 | - $query = $this->connection->getQueryBuilder(); |
|
| 114 | - $query->delete('jobs') |
|
| 115 | - ->where($query->expr()->eq('id', $query->createNamedParameter($id, IQueryBuilder::PARAM_INT))); |
|
| 116 | - $query->execute(); |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - /** |
|
| 120 | - * check if a job is in the list |
|
| 121 | - * |
|
| 122 | - * @param IJob|string $job |
|
| 123 | - * @param mixed $argument |
|
| 124 | - * @return bool |
|
| 125 | - */ |
|
| 126 | - public function has($job, $argument) { |
|
| 127 | - if ($job instanceof IJob) { |
|
| 128 | - $class = get_class($job); |
|
| 129 | - } else { |
|
| 130 | - $class = $job; |
|
| 131 | - } |
|
| 132 | - $argument = json_encode($argument); |
|
| 133 | - |
|
| 134 | - $query = $this->connection->getQueryBuilder(); |
|
| 135 | - $query->select('id') |
|
| 136 | - ->from('jobs') |
|
| 137 | - ->where($query->expr()->eq('class', $query->createNamedParameter($class))) |
|
| 138 | - ->andWhere($query->expr()->eq('argument', $query->createNamedParameter($argument))) |
|
| 139 | - ->setMaxResults(1); |
|
| 140 | - |
|
| 141 | - $result = $query->execute(); |
|
| 142 | - $row = $result->fetch(); |
|
| 143 | - $result->closeCursor(); |
|
| 144 | - |
|
| 145 | - return (bool) $row; |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - /** |
|
| 149 | - * get all jobs in the list |
|
| 150 | - * |
|
| 151 | - * @return IJob[] |
|
| 152 | - * @deprecated 9.0.0 - This method is dangerous since it can cause load and |
|
| 153 | - * memory problems when creating too many instances. |
|
| 154 | - */ |
|
| 155 | - public function getAll() { |
|
| 156 | - $query = $this->connection->getQueryBuilder(); |
|
| 157 | - $query->select('*') |
|
| 158 | - ->from('jobs'); |
|
| 159 | - $result = $query->execute(); |
|
| 160 | - |
|
| 161 | - $jobs = []; |
|
| 162 | - while ($row = $result->fetch()) { |
|
| 163 | - $job = $this->buildJob($row); |
|
| 164 | - if ($job) { |
|
| 165 | - $jobs[] = $job; |
|
| 166 | - } |
|
| 167 | - } |
|
| 168 | - $result->closeCursor(); |
|
| 169 | - |
|
| 170 | - return $jobs; |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - /** |
|
| 174 | - * get the next job in the list |
|
| 175 | - * |
|
| 176 | - * @return IJob|null |
|
| 177 | - */ |
|
| 178 | - public function getNext() { |
|
| 179 | - $query = $this->connection->getQueryBuilder(); |
|
| 180 | - $query->select('*') |
|
| 181 | - ->from('jobs') |
|
| 182 | - ->where($query->expr()->lte('reserved_at', $query->createNamedParameter($this->timeFactory->getTime() - 12 * 3600, IQueryBuilder::PARAM_INT))) |
|
| 183 | - ->orderBy('last_checked', 'ASC') |
|
| 184 | - ->setMaxResults(1); |
|
| 185 | - |
|
| 186 | - $update = $this->connection->getQueryBuilder(); |
|
| 187 | - $update->update('jobs') |
|
| 188 | - ->set('reserved_at', $update->createNamedParameter($this->timeFactory->getTime())) |
|
| 189 | - ->set('last_checked', $update->createNamedParameter($this->timeFactory->getTime())) |
|
| 190 | - ->where($update->expr()->eq('id', $update->createParameter('jobid'))) |
|
| 191 | - ->andWhere($update->expr()->eq('reserved_at', $update->createParameter('reserved_at'))) |
|
| 192 | - ->andWhere($update->expr()->eq('last_checked', $update->createParameter('last_checked'))); |
|
| 193 | - |
|
| 194 | - $result = $query->execute(); |
|
| 195 | - $row = $result->fetch(); |
|
| 196 | - $result->closeCursor(); |
|
| 197 | - |
|
| 198 | - if ($row) { |
|
| 199 | - $update->setParameter('jobid', $row['id']); |
|
| 200 | - $update->setParameter('reserved_at', $row['reserved_at']); |
|
| 201 | - $update->setParameter('last_checked', $row['last_checked']); |
|
| 202 | - $count = $update->execute(); |
|
| 203 | - |
|
| 204 | - if ($count === 0) { |
|
| 205 | - // Background job already executed elsewhere, try again. |
|
| 206 | - return $this->getNext(); |
|
| 207 | - } |
|
| 208 | - $job = $this->buildJob($row); |
|
| 209 | - |
|
| 210 | - if ($job === null) { |
|
| 211 | - // Background job from disabled app, try again. |
|
| 212 | - return $this->getNext(); |
|
| 213 | - } |
|
| 214 | - |
|
| 215 | - return $job; |
|
| 216 | - } else { |
|
| 217 | - return null; |
|
| 218 | - } |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - /** |
|
| 222 | - * @param int $id |
|
| 223 | - * @return IJob|null |
|
| 224 | - */ |
|
| 225 | - public function getById($id) { |
|
| 226 | - $query = $this->connection->getQueryBuilder(); |
|
| 227 | - $query->select('*') |
|
| 228 | - ->from('jobs') |
|
| 229 | - ->where($query->expr()->eq('id', $query->createNamedParameter($id, IQueryBuilder::PARAM_INT))); |
|
| 230 | - $result = $query->execute(); |
|
| 231 | - $row = $result->fetch(); |
|
| 232 | - $result->closeCursor(); |
|
| 233 | - |
|
| 234 | - if ($row) { |
|
| 235 | - return $this->buildJob($row); |
|
| 236 | - } else { |
|
| 237 | - return null; |
|
| 238 | - } |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - /** |
|
| 242 | - * get the job object from a row in the db |
|
| 243 | - * |
|
| 244 | - * @param array $row |
|
| 245 | - * @return IJob|null |
|
| 246 | - */ |
|
| 247 | - private function buildJob($row) { |
|
| 248 | - try { |
|
| 249 | - try { |
|
| 250 | - // Try to load the job as a service |
|
| 251 | - /** @var IJob $job */ |
|
| 252 | - $job = \OC::$server->query($row['class']); |
|
| 253 | - } catch (QueryException $e) { |
|
| 254 | - if (class_exists($row['class'])) { |
|
| 255 | - $class = $row['class']; |
|
| 256 | - $job = new $class(); |
|
| 257 | - } else { |
|
| 258 | - // job from disabled app or old version of an app, no need to do anything |
|
| 259 | - return null; |
|
| 260 | - } |
|
| 261 | - } |
|
| 262 | - |
|
| 263 | - $job->setId($row['id']); |
|
| 264 | - $job->setLastRun($row['last_run']); |
|
| 265 | - $job->setArgument(json_decode($row['argument'], true)); |
|
| 266 | - return $job; |
|
| 267 | - } catch (AutoloadNotAllowedException $e) { |
|
| 268 | - // job is from a disabled app, ignore |
|
| 269 | - return null; |
|
| 270 | - } |
|
| 271 | - } |
|
| 272 | - |
|
| 273 | - /** |
|
| 274 | - * set the job that was last ran |
|
| 275 | - * |
|
| 276 | - * @param IJob $job |
|
| 277 | - */ |
|
| 278 | - public function setLastJob(IJob $job) { |
|
| 279 | - $this->unlockJob($job); |
|
| 280 | - $this->config->setAppValue('backgroundjob', 'lastjob', $job->getId()); |
|
| 281 | - } |
|
| 282 | - |
|
| 283 | - /** |
|
| 284 | - * Remove the reservation for a job |
|
| 285 | - * |
|
| 286 | - * @param IJob $job |
|
| 287 | - */ |
|
| 288 | - public function unlockJob(IJob $job) { |
|
| 289 | - $query = $this->connection->getQueryBuilder(); |
|
| 290 | - $query->update('jobs') |
|
| 291 | - ->set('reserved_at', $query->expr()->literal(0, IQueryBuilder::PARAM_INT)) |
|
| 292 | - ->where($query->expr()->eq('id', $query->createNamedParameter($job->getId(), IQueryBuilder::PARAM_INT))); |
|
| 293 | - $query->execute(); |
|
| 294 | - } |
|
| 295 | - |
|
| 296 | - /** |
|
| 297 | - * get the id of the last ran job |
|
| 298 | - * |
|
| 299 | - * @return int |
|
| 300 | - * @deprecated 9.1.0 - The functionality behind the value is deprecated, it |
|
| 301 | - * only tells you which job finished last, but since we now allow multiple |
|
| 302 | - * executors to run in parallel, it's not used to calculate the next job. |
|
| 303 | - */ |
|
| 304 | - public function getLastJob() { |
|
| 305 | - return (int) $this->config->getAppValue('backgroundjob', 'lastjob', 0); |
|
| 306 | - } |
|
| 307 | - |
|
| 308 | - /** |
|
| 309 | - * set the lastRun of $job to now |
|
| 310 | - * |
|
| 311 | - * @param IJob $job |
|
| 312 | - */ |
|
| 313 | - public function setLastRun(IJob $job) { |
|
| 314 | - $query = $this->connection->getQueryBuilder(); |
|
| 315 | - $query->update('jobs') |
|
| 316 | - ->set('last_run', $query->createNamedParameter(time(), IQueryBuilder::PARAM_INT)) |
|
| 317 | - ->where($query->expr()->eq('id', $query->createNamedParameter($job->getId(), IQueryBuilder::PARAM_INT))); |
|
| 318 | - $query->execute(); |
|
| 319 | - } |
|
| 320 | - |
|
| 321 | - /** |
|
| 322 | - * @param IJob $job |
|
| 323 | - * @param $timeTaken |
|
| 324 | - */ |
|
| 325 | - public function setExecutionTime(IJob $job, $timeTaken) { |
|
| 326 | - $query = $this->connection->getQueryBuilder(); |
|
| 327 | - $query->update('jobs') |
|
| 328 | - ->set('execution_duration', $query->createNamedParameter($timeTaken, IQueryBuilder::PARAM_INT)) |
|
| 329 | - ->where($query->expr()->eq('id', $query->createNamedParameter($job->getId(), IQueryBuilder::PARAM_INT))); |
|
| 330 | - $query->execute(); |
|
| 331 | - } |
|
| 39 | + /** @var IDBConnection */ |
|
| 40 | + protected $connection; |
|
| 41 | + |
|
| 42 | + /**@var IConfig */ |
|
| 43 | + protected $config; |
|
| 44 | + |
|
| 45 | + /**@var ITimeFactory */ |
|
| 46 | + protected $timeFactory; |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * @param IDBConnection $connection |
|
| 50 | + * @param IConfig $config |
|
| 51 | + * @param ITimeFactory $timeFactory |
|
| 52 | + */ |
|
| 53 | + public function __construct(IDBConnection $connection, IConfig $config, ITimeFactory $timeFactory) { |
|
| 54 | + $this->connection = $connection; |
|
| 55 | + $this->config = $config; |
|
| 56 | + $this->timeFactory = $timeFactory; |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * @param IJob|string $job |
|
| 61 | + * @param mixed $argument |
|
| 62 | + */ |
|
| 63 | + public function add($job, $argument = null) { |
|
| 64 | + if (!$this->has($job, $argument)) { |
|
| 65 | + if ($job instanceof IJob) { |
|
| 66 | + $class = get_class($job); |
|
| 67 | + } else { |
|
| 68 | + $class = $job; |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + $argument = json_encode($argument); |
|
| 72 | + if (strlen($argument) > 4000) { |
|
| 73 | + throw new \InvalidArgumentException('Background job arguments can\'t exceed 4000 characters (json encoded)'); |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + $query = $this->connection->getQueryBuilder(); |
|
| 77 | + $query->insert('jobs') |
|
| 78 | + ->values([ |
|
| 79 | + 'class' => $query->createNamedParameter($class), |
|
| 80 | + 'argument' => $query->createNamedParameter($argument), |
|
| 81 | + 'last_run' => $query->createNamedParameter(0, IQueryBuilder::PARAM_INT), |
|
| 82 | + 'last_checked' => $query->createNamedParameter($this->timeFactory->getTime(), IQueryBuilder::PARAM_INT), |
|
| 83 | + ]); |
|
| 84 | + $query->execute(); |
|
| 85 | + } |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * @param IJob|string $job |
|
| 90 | + * @param mixed $argument |
|
| 91 | + */ |
|
| 92 | + public function remove($job, $argument = null) { |
|
| 93 | + if ($job instanceof IJob) { |
|
| 94 | + $class = get_class($job); |
|
| 95 | + } else { |
|
| 96 | + $class = $job; |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + $query = $this->connection->getQueryBuilder(); |
|
| 100 | + $query->delete('jobs') |
|
| 101 | + ->where($query->expr()->eq('class', $query->createNamedParameter($class))); |
|
| 102 | + if (!is_null($argument)) { |
|
| 103 | + $argument = json_encode($argument); |
|
| 104 | + $query->andWhere($query->expr()->eq('argument', $query->createNamedParameter($argument))); |
|
| 105 | + } |
|
| 106 | + $query->execute(); |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * @param int $id |
|
| 111 | + */ |
|
| 112 | + protected function removeById($id) { |
|
| 113 | + $query = $this->connection->getQueryBuilder(); |
|
| 114 | + $query->delete('jobs') |
|
| 115 | + ->where($query->expr()->eq('id', $query->createNamedParameter($id, IQueryBuilder::PARAM_INT))); |
|
| 116 | + $query->execute(); |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * check if a job is in the list |
|
| 121 | + * |
|
| 122 | + * @param IJob|string $job |
|
| 123 | + * @param mixed $argument |
|
| 124 | + * @return bool |
|
| 125 | + */ |
|
| 126 | + public function has($job, $argument) { |
|
| 127 | + if ($job instanceof IJob) { |
|
| 128 | + $class = get_class($job); |
|
| 129 | + } else { |
|
| 130 | + $class = $job; |
|
| 131 | + } |
|
| 132 | + $argument = json_encode($argument); |
|
| 133 | + |
|
| 134 | + $query = $this->connection->getQueryBuilder(); |
|
| 135 | + $query->select('id') |
|
| 136 | + ->from('jobs') |
|
| 137 | + ->where($query->expr()->eq('class', $query->createNamedParameter($class))) |
|
| 138 | + ->andWhere($query->expr()->eq('argument', $query->createNamedParameter($argument))) |
|
| 139 | + ->setMaxResults(1); |
|
| 140 | + |
|
| 141 | + $result = $query->execute(); |
|
| 142 | + $row = $result->fetch(); |
|
| 143 | + $result->closeCursor(); |
|
| 144 | + |
|
| 145 | + return (bool) $row; |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + /** |
|
| 149 | + * get all jobs in the list |
|
| 150 | + * |
|
| 151 | + * @return IJob[] |
|
| 152 | + * @deprecated 9.0.0 - This method is dangerous since it can cause load and |
|
| 153 | + * memory problems when creating too many instances. |
|
| 154 | + */ |
|
| 155 | + public function getAll() { |
|
| 156 | + $query = $this->connection->getQueryBuilder(); |
|
| 157 | + $query->select('*') |
|
| 158 | + ->from('jobs'); |
|
| 159 | + $result = $query->execute(); |
|
| 160 | + |
|
| 161 | + $jobs = []; |
|
| 162 | + while ($row = $result->fetch()) { |
|
| 163 | + $job = $this->buildJob($row); |
|
| 164 | + if ($job) { |
|
| 165 | + $jobs[] = $job; |
|
| 166 | + } |
|
| 167 | + } |
|
| 168 | + $result->closeCursor(); |
|
| 169 | + |
|
| 170 | + return $jobs; |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + /** |
|
| 174 | + * get the next job in the list |
|
| 175 | + * |
|
| 176 | + * @return IJob|null |
|
| 177 | + */ |
|
| 178 | + public function getNext() { |
|
| 179 | + $query = $this->connection->getQueryBuilder(); |
|
| 180 | + $query->select('*') |
|
| 181 | + ->from('jobs') |
|
| 182 | + ->where($query->expr()->lte('reserved_at', $query->createNamedParameter($this->timeFactory->getTime() - 12 * 3600, IQueryBuilder::PARAM_INT))) |
|
| 183 | + ->orderBy('last_checked', 'ASC') |
|
| 184 | + ->setMaxResults(1); |
|
| 185 | + |
|
| 186 | + $update = $this->connection->getQueryBuilder(); |
|
| 187 | + $update->update('jobs') |
|
| 188 | + ->set('reserved_at', $update->createNamedParameter($this->timeFactory->getTime())) |
|
| 189 | + ->set('last_checked', $update->createNamedParameter($this->timeFactory->getTime())) |
|
| 190 | + ->where($update->expr()->eq('id', $update->createParameter('jobid'))) |
|
| 191 | + ->andWhere($update->expr()->eq('reserved_at', $update->createParameter('reserved_at'))) |
|
| 192 | + ->andWhere($update->expr()->eq('last_checked', $update->createParameter('last_checked'))); |
|
| 193 | + |
|
| 194 | + $result = $query->execute(); |
|
| 195 | + $row = $result->fetch(); |
|
| 196 | + $result->closeCursor(); |
|
| 197 | + |
|
| 198 | + if ($row) { |
|
| 199 | + $update->setParameter('jobid', $row['id']); |
|
| 200 | + $update->setParameter('reserved_at', $row['reserved_at']); |
|
| 201 | + $update->setParameter('last_checked', $row['last_checked']); |
|
| 202 | + $count = $update->execute(); |
|
| 203 | + |
|
| 204 | + if ($count === 0) { |
|
| 205 | + // Background job already executed elsewhere, try again. |
|
| 206 | + return $this->getNext(); |
|
| 207 | + } |
|
| 208 | + $job = $this->buildJob($row); |
|
| 209 | + |
|
| 210 | + if ($job === null) { |
|
| 211 | + // Background job from disabled app, try again. |
|
| 212 | + return $this->getNext(); |
|
| 213 | + } |
|
| 214 | + |
|
| 215 | + return $job; |
|
| 216 | + } else { |
|
| 217 | + return null; |
|
| 218 | + } |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + /** |
|
| 222 | + * @param int $id |
|
| 223 | + * @return IJob|null |
|
| 224 | + */ |
|
| 225 | + public function getById($id) { |
|
| 226 | + $query = $this->connection->getQueryBuilder(); |
|
| 227 | + $query->select('*') |
|
| 228 | + ->from('jobs') |
|
| 229 | + ->where($query->expr()->eq('id', $query->createNamedParameter($id, IQueryBuilder::PARAM_INT))); |
|
| 230 | + $result = $query->execute(); |
|
| 231 | + $row = $result->fetch(); |
|
| 232 | + $result->closeCursor(); |
|
| 233 | + |
|
| 234 | + if ($row) { |
|
| 235 | + return $this->buildJob($row); |
|
| 236 | + } else { |
|
| 237 | + return null; |
|
| 238 | + } |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + /** |
|
| 242 | + * get the job object from a row in the db |
|
| 243 | + * |
|
| 244 | + * @param array $row |
|
| 245 | + * @return IJob|null |
|
| 246 | + */ |
|
| 247 | + private function buildJob($row) { |
|
| 248 | + try { |
|
| 249 | + try { |
|
| 250 | + // Try to load the job as a service |
|
| 251 | + /** @var IJob $job */ |
|
| 252 | + $job = \OC::$server->query($row['class']); |
|
| 253 | + } catch (QueryException $e) { |
|
| 254 | + if (class_exists($row['class'])) { |
|
| 255 | + $class = $row['class']; |
|
| 256 | + $job = new $class(); |
|
| 257 | + } else { |
|
| 258 | + // job from disabled app or old version of an app, no need to do anything |
|
| 259 | + return null; |
|
| 260 | + } |
|
| 261 | + } |
|
| 262 | + |
|
| 263 | + $job->setId($row['id']); |
|
| 264 | + $job->setLastRun($row['last_run']); |
|
| 265 | + $job->setArgument(json_decode($row['argument'], true)); |
|
| 266 | + return $job; |
|
| 267 | + } catch (AutoloadNotAllowedException $e) { |
|
| 268 | + // job is from a disabled app, ignore |
|
| 269 | + return null; |
|
| 270 | + } |
|
| 271 | + } |
|
| 272 | + |
|
| 273 | + /** |
|
| 274 | + * set the job that was last ran |
|
| 275 | + * |
|
| 276 | + * @param IJob $job |
|
| 277 | + */ |
|
| 278 | + public function setLastJob(IJob $job) { |
|
| 279 | + $this->unlockJob($job); |
|
| 280 | + $this->config->setAppValue('backgroundjob', 'lastjob', $job->getId()); |
|
| 281 | + } |
|
| 282 | + |
|
| 283 | + /** |
|
| 284 | + * Remove the reservation for a job |
|
| 285 | + * |
|
| 286 | + * @param IJob $job |
|
| 287 | + */ |
|
| 288 | + public function unlockJob(IJob $job) { |
|
| 289 | + $query = $this->connection->getQueryBuilder(); |
|
| 290 | + $query->update('jobs') |
|
| 291 | + ->set('reserved_at', $query->expr()->literal(0, IQueryBuilder::PARAM_INT)) |
|
| 292 | + ->where($query->expr()->eq('id', $query->createNamedParameter($job->getId(), IQueryBuilder::PARAM_INT))); |
|
| 293 | + $query->execute(); |
|
| 294 | + } |
|
| 295 | + |
|
| 296 | + /** |
|
| 297 | + * get the id of the last ran job |
|
| 298 | + * |
|
| 299 | + * @return int |
|
| 300 | + * @deprecated 9.1.0 - The functionality behind the value is deprecated, it |
|
| 301 | + * only tells you which job finished last, but since we now allow multiple |
|
| 302 | + * executors to run in parallel, it's not used to calculate the next job. |
|
| 303 | + */ |
|
| 304 | + public function getLastJob() { |
|
| 305 | + return (int) $this->config->getAppValue('backgroundjob', 'lastjob', 0); |
|
| 306 | + } |
|
| 307 | + |
|
| 308 | + /** |
|
| 309 | + * set the lastRun of $job to now |
|
| 310 | + * |
|
| 311 | + * @param IJob $job |
|
| 312 | + */ |
|
| 313 | + public function setLastRun(IJob $job) { |
|
| 314 | + $query = $this->connection->getQueryBuilder(); |
|
| 315 | + $query->update('jobs') |
|
| 316 | + ->set('last_run', $query->createNamedParameter(time(), IQueryBuilder::PARAM_INT)) |
|
| 317 | + ->where($query->expr()->eq('id', $query->createNamedParameter($job->getId(), IQueryBuilder::PARAM_INT))); |
|
| 318 | + $query->execute(); |
|
| 319 | + } |
|
| 320 | + |
|
| 321 | + /** |
|
| 322 | + * @param IJob $job |
|
| 323 | + * @param $timeTaken |
|
| 324 | + */ |
|
| 325 | + public function setExecutionTime(IJob $job, $timeTaken) { |
|
| 326 | + $query = $this->connection->getQueryBuilder(); |
|
| 327 | + $query->update('jobs') |
|
| 328 | + ->set('execution_duration', $query->createNamedParameter($timeTaken, IQueryBuilder::PARAM_INT)) |
|
| 329 | + ->where($query->expr()->eq('id', $query->createNamedParameter($job->getId(), IQueryBuilder::PARAM_INT))); |
|
| 330 | + $query->execute(); |
|
| 331 | + } |
|
| 332 | 332 | } |
@@ -32,133 +32,133 @@ |
||
| 32 | 32 | |
| 33 | 33 | // Show warning if a PHP version below 5.6.0 is used |
| 34 | 34 | if (version_compare(PHP_VERSION, '5.6.0') === -1) { |
| 35 | - echo 'This version of Nextcloud requires at least PHP 5.6.0<br/>'; |
|
| 36 | - echo 'You are currently running ' . PHP_VERSION . '. Please update your PHP version.'; |
|
| 37 | - return; |
|
| 35 | + echo 'This version of Nextcloud requires at least PHP 5.6.0<br/>'; |
|
| 36 | + echo 'You are currently running ' . PHP_VERSION . '. Please update your PHP version.'; |
|
| 37 | + return; |
|
| 38 | 38 | } |
| 39 | 39 | |
| 40 | 40 | try { |
| 41 | 41 | |
| 42 | - require_once __DIR__ . '/lib/base.php'; |
|
| 43 | - |
|
| 44 | - if (\OCP\Util::needUpgrade()) { |
|
| 45 | - \OCP\Util::writeLog('cron', 'Update required, skipping cron', \OCP\Util::DEBUG); |
|
| 46 | - exit; |
|
| 47 | - } |
|
| 48 | - if (\OC::$server->getSystemConfig()->getValue('maintenance', false)) { |
|
| 49 | - \OCP\Util::writeLog('cron', 'We are in maintenance mode, skipping cron', \OCP\Util::DEBUG); |
|
| 50 | - exit; |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - // load all apps to get all api routes properly setup |
|
| 54 | - OC_App::loadApps(); |
|
| 55 | - |
|
| 56 | - \OC::$server->getSession()->close(); |
|
| 57 | - |
|
| 58 | - // initialize a dummy memory session |
|
| 59 | - $session = new \OC\Session\Memory(''); |
|
| 60 | - $cryptoWrapper = \OC::$server->getSessionCryptoWrapper(); |
|
| 61 | - $session = $cryptoWrapper->wrapSession($session); |
|
| 62 | - \OC::$server->setSession($session); |
|
| 63 | - |
|
| 64 | - $logger = \OC::$server->getLogger(); |
|
| 65 | - $config = \OC::$server->getConfig(); |
|
| 66 | - |
|
| 67 | - // Don't do anything if ownCloud has not been installed |
|
| 68 | - if (!$config->getSystemValue('installed', false)) { |
|
| 69 | - exit(0); |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - \OC::$server->getTempManager()->cleanOld(); |
|
| 73 | - |
|
| 74 | - // Exit if background jobs are disabled! |
|
| 75 | - $appMode = \OCP\BackgroundJob::getExecutionType(); |
|
| 76 | - if ($appMode == 'none') { |
|
| 77 | - if (OC::$CLI) { |
|
| 78 | - echo 'Background Jobs are disabled!' . PHP_EOL; |
|
| 79 | - } else { |
|
| 80 | - OC_JSON::error(array('data' => array('message' => 'Background jobs disabled!'))); |
|
| 81 | - } |
|
| 82 | - exit(1); |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - if (OC::$CLI) { |
|
| 86 | - // set to run indefinitely if needed |
|
| 87 | - if (strpos(@ini_get('disable_functions'), 'set_time_limit') === false) { |
|
| 88 | - @set_time_limit(0); |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - // the cron job must be executed with the right user |
|
| 92 | - if (!function_exists('posix_getuid')) { |
|
| 93 | - echo "The posix extensions are required - see http://php.net/manual/en/book.posix.php" . PHP_EOL; |
|
| 94 | - exit(1); |
|
| 95 | - } |
|
| 96 | - $user = posix_getpwuid(posix_getuid()); |
|
| 97 | - $configUser = posix_getpwuid(fileowner(OC::$configDir . 'config.php')); |
|
| 98 | - if ($user['name'] !== $configUser['name']) { |
|
| 99 | - echo "Console has to be executed with the same user as the web server is operated" . PHP_EOL; |
|
| 100 | - echo "Current user: " . $user['name'] . PHP_EOL; |
|
| 101 | - echo "Web server user: " . $configUser['name'] . PHP_EOL; |
|
| 102 | - exit(1); |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - // We call ownCloud from the CLI (aka cron) |
|
| 106 | - if ($appMode != 'cron') { |
|
| 107 | - \OCP\BackgroundJob::setExecutionType('cron'); |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - // Work |
|
| 111 | - $jobList = \OC::$server->getJobList(); |
|
| 112 | - |
|
| 113 | - // We only ask for jobs for 14 minutes, because after 15 minutes the next |
|
| 114 | - // system cron task should spawn. |
|
| 115 | - $endTime = time() + 14 * 60; |
|
| 116 | - |
|
| 117 | - $executedJobs = []; |
|
| 118 | - while ($job = $jobList->getNext()) { |
|
| 119 | - if (isset($executedJobs[$job->getId()])) { |
|
| 120 | - $jobList->unlockJob($job); |
|
| 121 | - break; |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - $job->execute($jobList, $logger); |
|
| 125 | - // clean up after unclean jobs |
|
| 126 | - \OC_Util::tearDownFS(); |
|
| 127 | - |
|
| 128 | - $jobList->setLastJob($job); |
|
| 129 | - $executedJobs[$job->getId()] = true; |
|
| 130 | - unset($job); |
|
| 131 | - |
|
| 132 | - if (time() > $endTime) { |
|
| 133 | - break; |
|
| 134 | - } |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - } else { |
|
| 138 | - // We call cron.php from some website |
|
| 139 | - if ($appMode == 'cron') { |
|
| 140 | - // Cron is cron :-P |
|
| 141 | - OC_JSON::error(array('data' => array('message' => 'Backgroundjobs are using system cron!'))); |
|
| 142 | - } else { |
|
| 143 | - // Work and success :-) |
|
| 144 | - $jobList = \OC::$server->getJobList(); |
|
| 145 | - $job = $jobList->getNext(); |
|
| 146 | - if ($job != null) { |
|
| 147 | - $job->execute($jobList, $logger); |
|
| 148 | - $jobList->setLastJob($job); |
|
| 149 | - } |
|
| 150 | - OC_JSON::success(); |
|
| 151 | - } |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - // Log the successful cron execution |
|
| 155 | - if (\OC::$server->getConfig()->getSystemValue('cron_log', true)) { |
|
| 156 | - \OC::$server->getConfig()->setAppValue('core', 'lastcron', time()); |
|
| 157 | - } |
|
| 158 | - exit(); |
|
| 42 | + require_once __DIR__ . '/lib/base.php'; |
|
| 43 | + |
|
| 44 | + if (\OCP\Util::needUpgrade()) { |
|
| 45 | + \OCP\Util::writeLog('cron', 'Update required, skipping cron', \OCP\Util::DEBUG); |
|
| 46 | + exit; |
|
| 47 | + } |
|
| 48 | + if (\OC::$server->getSystemConfig()->getValue('maintenance', false)) { |
|
| 49 | + \OCP\Util::writeLog('cron', 'We are in maintenance mode, skipping cron', \OCP\Util::DEBUG); |
|
| 50 | + exit; |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + // load all apps to get all api routes properly setup |
|
| 54 | + OC_App::loadApps(); |
|
| 55 | + |
|
| 56 | + \OC::$server->getSession()->close(); |
|
| 57 | + |
|
| 58 | + // initialize a dummy memory session |
|
| 59 | + $session = new \OC\Session\Memory(''); |
|
| 60 | + $cryptoWrapper = \OC::$server->getSessionCryptoWrapper(); |
|
| 61 | + $session = $cryptoWrapper->wrapSession($session); |
|
| 62 | + \OC::$server->setSession($session); |
|
| 63 | + |
|
| 64 | + $logger = \OC::$server->getLogger(); |
|
| 65 | + $config = \OC::$server->getConfig(); |
|
| 66 | + |
|
| 67 | + // Don't do anything if ownCloud has not been installed |
|
| 68 | + if (!$config->getSystemValue('installed', false)) { |
|
| 69 | + exit(0); |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + \OC::$server->getTempManager()->cleanOld(); |
|
| 73 | + |
|
| 74 | + // Exit if background jobs are disabled! |
|
| 75 | + $appMode = \OCP\BackgroundJob::getExecutionType(); |
|
| 76 | + if ($appMode == 'none') { |
|
| 77 | + if (OC::$CLI) { |
|
| 78 | + echo 'Background Jobs are disabled!' . PHP_EOL; |
|
| 79 | + } else { |
|
| 80 | + OC_JSON::error(array('data' => array('message' => 'Background jobs disabled!'))); |
|
| 81 | + } |
|
| 82 | + exit(1); |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + if (OC::$CLI) { |
|
| 86 | + // set to run indefinitely if needed |
|
| 87 | + if (strpos(@ini_get('disable_functions'), 'set_time_limit') === false) { |
|
| 88 | + @set_time_limit(0); |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + // the cron job must be executed with the right user |
|
| 92 | + if (!function_exists('posix_getuid')) { |
|
| 93 | + echo "The posix extensions are required - see http://php.net/manual/en/book.posix.php" . PHP_EOL; |
|
| 94 | + exit(1); |
|
| 95 | + } |
|
| 96 | + $user = posix_getpwuid(posix_getuid()); |
|
| 97 | + $configUser = posix_getpwuid(fileowner(OC::$configDir . 'config.php')); |
|
| 98 | + if ($user['name'] !== $configUser['name']) { |
|
| 99 | + echo "Console has to be executed with the same user as the web server is operated" . PHP_EOL; |
|
| 100 | + echo "Current user: " . $user['name'] . PHP_EOL; |
|
| 101 | + echo "Web server user: " . $configUser['name'] . PHP_EOL; |
|
| 102 | + exit(1); |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + // We call ownCloud from the CLI (aka cron) |
|
| 106 | + if ($appMode != 'cron') { |
|
| 107 | + \OCP\BackgroundJob::setExecutionType('cron'); |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + // Work |
|
| 111 | + $jobList = \OC::$server->getJobList(); |
|
| 112 | + |
|
| 113 | + // We only ask for jobs for 14 minutes, because after 15 minutes the next |
|
| 114 | + // system cron task should spawn. |
|
| 115 | + $endTime = time() + 14 * 60; |
|
| 116 | + |
|
| 117 | + $executedJobs = []; |
|
| 118 | + while ($job = $jobList->getNext()) { |
|
| 119 | + if (isset($executedJobs[$job->getId()])) { |
|
| 120 | + $jobList->unlockJob($job); |
|
| 121 | + break; |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + $job->execute($jobList, $logger); |
|
| 125 | + // clean up after unclean jobs |
|
| 126 | + \OC_Util::tearDownFS(); |
|
| 127 | + |
|
| 128 | + $jobList->setLastJob($job); |
|
| 129 | + $executedJobs[$job->getId()] = true; |
|
| 130 | + unset($job); |
|
| 131 | + |
|
| 132 | + if (time() > $endTime) { |
|
| 133 | + break; |
|
| 134 | + } |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + } else { |
|
| 138 | + // We call cron.php from some website |
|
| 139 | + if ($appMode == 'cron') { |
|
| 140 | + // Cron is cron :-P |
|
| 141 | + OC_JSON::error(array('data' => array('message' => 'Backgroundjobs are using system cron!'))); |
|
| 142 | + } else { |
|
| 143 | + // Work and success :-) |
|
| 144 | + $jobList = \OC::$server->getJobList(); |
|
| 145 | + $job = $jobList->getNext(); |
|
| 146 | + if ($job != null) { |
|
| 147 | + $job->execute($jobList, $logger); |
|
| 148 | + $jobList->setLastJob($job); |
|
| 149 | + } |
|
| 150 | + OC_JSON::success(); |
|
| 151 | + } |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + // Log the successful cron execution |
|
| 155 | + if (\OC::$server->getConfig()->getSystemValue('cron_log', true)) { |
|
| 156 | + \OC::$server->getConfig()->setAppValue('core', 'lastcron', time()); |
|
| 157 | + } |
|
| 158 | + exit(); |
|
| 159 | 159 | |
| 160 | 160 | } catch (Exception $ex) { |
| 161 | - \OCP\Util::writeLog('cron', $ex->getMessage(), \OCP\Util::FATAL); |
|
| 161 | + \OCP\Util::writeLog('cron', $ex->getMessage(), \OCP\Util::FATAL); |
|
| 162 | 162 | } catch (Error $ex) { |
| 163 | - \OCP\Util::writeLog('cron', $ex->getMessage(), \OCP\Util::FATAL); |
|
| 163 | + \OCP\Util::writeLog('cron', $ex->getMessage(), \OCP\Util::FATAL); |
|
| 164 | 164 | } |
@@ -33,13 +33,13 @@ discard block |
||
| 33 | 33 | // Show warning if a PHP version below 5.6.0 is used |
| 34 | 34 | if (version_compare(PHP_VERSION, '5.6.0') === -1) { |
| 35 | 35 | echo 'This version of Nextcloud requires at least PHP 5.6.0<br/>'; |
| 36 | - echo 'You are currently running ' . PHP_VERSION . '. Please update your PHP version.'; |
|
| 36 | + echo 'You are currently running '.PHP_VERSION.'. Please update your PHP version.'; |
|
| 37 | 37 | return; |
| 38 | 38 | } |
| 39 | 39 | |
| 40 | 40 | try { |
| 41 | 41 | |
| 42 | - require_once __DIR__ . '/lib/base.php'; |
|
| 42 | + require_once __DIR__.'/lib/base.php'; |
|
| 43 | 43 | |
| 44 | 44 | if (\OCP\Util::needUpgrade()) { |
| 45 | 45 | \OCP\Util::writeLog('cron', 'Update required, skipping cron', \OCP\Util::DEBUG); |
@@ -75,7 +75,7 @@ discard block |
||
| 75 | 75 | $appMode = \OCP\BackgroundJob::getExecutionType(); |
| 76 | 76 | if ($appMode == 'none') { |
| 77 | 77 | if (OC::$CLI) { |
| 78 | - echo 'Background Jobs are disabled!' . PHP_EOL; |
|
| 78 | + echo 'Background Jobs are disabled!'.PHP_EOL; |
|
| 79 | 79 | } else { |
| 80 | 80 | OC_JSON::error(array('data' => array('message' => 'Background jobs disabled!'))); |
| 81 | 81 | } |
@@ -90,15 +90,15 @@ discard block |
||
| 90 | 90 | |
| 91 | 91 | // the cron job must be executed with the right user |
| 92 | 92 | if (!function_exists('posix_getuid')) { |
| 93 | - echo "The posix extensions are required - see http://php.net/manual/en/book.posix.php" . PHP_EOL; |
|
| 93 | + echo "The posix extensions are required - see http://php.net/manual/en/book.posix.php".PHP_EOL; |
|
| 94 | 94 | exit(1); |
| 95 | 95 | } |
| 96 | 96 | $user = posix_getpwuid(posix_getuid()); |
| 97 | - $configUser = posix_getpwuid(fileowner(OC::$configDir . 'config.php')); |
|
| 97 | + $configUser = posix_getpwuid(fileowner(OC::$configDir.'config.php')); |
|
| 98 | 98 | if ($user['name'] !== $configUser['name']) { |
| 99 | - echo "Console has to be executed with the same user as the web server is operated" . PHP_EOL; |
|
| 100 | - echo "Current user: " . $user['name'] . PHP_EOL; |
|
| 101 | - echo "Web server user: " . $configUser['name'] . PHP_EOL; |
|
| 99 | + echo "Console has to be executed with the same user as the web server is operated".PHP_EOL; |
|
| 100 | + echo "Current user: ".$user['name'].PHP_EOL; |
|
| 101 | + echo "Web server user: ".$configUser['name'].PHP_EOL; |
|
| 102 | 102 | exit(1); |
| 103 | 103 | } |
| 104 | 104 | |
@@ -28,72 +28,72 @@ |
||
| 28 | 28 | use OCP\ILogger; |
| 29 | 29 | |
| 30 | 30 | abstract class Job implements IJob { |
| 31 | - /** |
|
| 32 | - * @var int $id |
|
| 33 | - */ |
|
| 34 | - protected $id; |
|
| 31 | + /** |
|
| 32 | + * @var int $id |
|
| 33 | + */ |
|
| 34 | + protected $id; |
|
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * @var int $lastRun |
|
| 38 | - */ |
|
| 39 | - protected $lastRun; |
|
| 36 | + /** |
|
| 37 | + * @var int $lastRun |
|
| 38 | + */ |
|
| 39 | + protected $lastRun; |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * @var mixed $argument |
|
| 43 | - */ |
|
| 44 | - protected $argument; |
|
| 41 | + /** |
|
| 42 | + * @var mixed $argument |
|
| 43 | + */ |
|
| 44 | + protected $argument; |
|
| 45 | 45 | |
| 46 | - /** |
|
| 47 | - * @param JobList $jobList |
|
| 48 | - * @param ILogger $logger |
|
| 49 | - */ |
|
| 50 | - public function execute($jobList, ILogger $logger = null) { |
|
| 51 | - $jobList->setLastRun($this); |
|
| 52 | - if ($logger === null) { |
|
| 53 | - $logger = \OC::$server->getLogger(); |
|
| 54 | - } |
|
| 46 | + /** |
|
| 47 | + * @param JobList $jobList |
|
| 48 | + * @param ILogger $logger |
|
| 49 | + */ |
|
| 50 | + public function execute($jobList, ILogger $logger = null) { |
|
| 51 | + $jobList->setLastRun($this); |
|
| 52 | + if ($logger === null) { |
|
| 53 | + $logger = \OC::$server->getLogger(); |
|
| 54 | + } |
|
| 55 | 55 | |
| 56 | - try { |
|
| 57 | - $jobStartTime = time(); |
|
| 58 | - $logger->debug('Run ' . get_class($this) . ' job with ID ' . $this->getId(), ['app' => 'cron']); |
|
| 59 | - $this->run($this->argument); |
|
| 60 | - $timeTaken = time() - $jobStartTime; |
|
| 56 | + try { |
|
| 57 | + $jobStartTime = time(); |
|
| 58 | + $logger->debug('Run ' . get_class($this) . ' job with ID ' . $this->getId(), ['app' => 'cron']); |
|
| 59 | + $this->run($this->argument); |
|
| 60 | + $timeTaken = time() - $jobStartTime; |
|
| 61 | 61 | |
| 62 | - $logger->debug('Finished ' . get_class($this) . ' job with ID ' . $this->getId() . ' in ' . $timeTaken . ' seconds', ['app' => 'cron']); |
|
| 63 | - $jobList->setExecutionTime($this, $timeTaken); |
|
| 64 | - } catch (\Exception $e) { |
|
| 65 | - if ($logger) { |
|
| 66 | - $logger->logException($e, [ |
|
| 67 | - 'app' => 'core', |
|
| 68 | - 'message' => 'Error while running background job (class: ' . get_class($this) . ', arguments: ' . print_r($this->argument, true) . ')' |
|
| 69 | - ]); |
|
| 70 | - } |
|
| 71 | - } |
|
| 72 | - } |
|
| 62 | + $logger->debug('Finished ' . get_class($this) . ' job with ID ' . $this->getId() . ' in ' . $timeTaken . ' seconds', ['app' => 'cron']); |
|
| 63 | + $jobList->setExecutionTime($this, $timeTaken); |
|
| 64 | + } catch (\Exception $e) { |
|
| 65 | + if ($logger) { |
|
| 66 | + $logger->logException($e, [ |
|
| 67 | + 'app' => 'core', |
|
| 68 | + 'message' => 'Error while running background job (class: ' . get_class($this) . ', arguments: ' . print_r($this->argument, true) . ')' |
|
| 69 | + ]); |
|
| 70 | + } |
|
| 71 | + } |
|
| 72 | + } |
|
| 73 | 73 | |
| 74 | - abstract protected function run($argument); |
|
| 74 | + abstract protected function run($argument); |
|
| 75 | 75 | |
| 76 | - public function setId($id) { |
|
| 77 | - $this->id = $id; |
|
| 78 | - } |
|
| 76 | + public function setId($id) { |
|
| 77 | + $this->id = $id; |
|
| 78 | + } |
|
| 79 | 79 | |
| 80 | - public function setLastRun($lastRun) { |
|
| 81 | - $this->lastRun = $lastRun; |
|
| 82 | - } |
|
| 80 | + public function setLastRun($lastRun) { |
|
| 81 | + $this->lastRun = $lastRun; |
|
| 82 | + } |
|
| 83 | 83 | |
| 84 | - public function setArgument($argument) { |
|
| 85 | - $this->argument = $argument; |
|
| 86 | - } |
|
| 84 | + public function setArgument($argument) { |
|
| 85 | + $this->argument = $argument; |
|
| 86 | + } |
|
| 87 | 87 | |
| 88 | - public function getId() { |
|
| 89 | - return $this->id; |
|
| 90 | - } |
|
| 88 | + public function getId() { |
|
| 89 | + return $this->id; |
|
| 90 | + } |
|
| 91 | 91 | |
| 92 | - public function getLastRun() { |
|
| 93 | - return $this->lastRun; |
|
| 94 | - } |
|
| 92 | + public function getLastRun() { |
|
| 93 | + return $this->lastRun; |
|
| 94 | + } |
|
| 95 | 95 | |
| 96 | - public function getArgument() { |
|
| 97 | - return $this->argument; |
|
| 98 | - } |
|
| 96 | + public function getArgument() { |
|
| 97 | + return $this->argument; |
|
| 98 | + } |
|
| 99 | 99 | } |
@@ -55,17 +55,17 @@ |
||
| 55 | 55 | |
| 56 | 56 | try { |
| 57 | 57 | $jobStartTime = time(); |
| 58 | - $logger->debug('Run ' . get_class($this) . ' job with ID ' . $this->getId(), ['app' => 'cron']); |
|
| 58 | + $logger->debug('Run '.get_class($this).' job with ID '.$this->getId(), ['app' => 'cron']); |
|
| 59 | 59 | $this->run($this->argument); |
| 60 | 60 | $timeTaken = time() - $jobStartTime; |
| 61 | 61 | |
| 62 | - $logger->debug('Finished ' . get_class($this) . ' job with ID ' . $this->getId() . ' in ' . $timeTaken . ' seconds', ['app' => 'cron']); |
|
| 62 | + $logger->debug('Finished '.get_class($this).' job with ID '.$this->getId().' in '.$timeTaken.' seconds', ['app' => 'cron']); |
|
| 63 | 63 | $jobList->setExecutionTime($this, $timeTaken); |
| 64 | 64 | } catch (\Exception $e) { |
| 65 | 65 | if ($logger) { |
| 66 | 66 | $logger->logException($e, [ |
| 67 | 67 | 'app' => 'core', |
| 68 | - 'message' => 'Error while running background job (class: ' . get_class($this) . ', arguments: ' . print_r($this->argument, true) . ')' |
|
| 68 | + 'message' => 'Error while running background job (class: '.get_class($this).', arguments: '.print_r($this->argument, true).')' |
|
| 69 | 69 | ]); |
| 70 | 70 | } |
| 71 | 71 | } |
@@ -32,100 +32,100 @@ |
||
| 32 | 32 | * @since 7.0.0 |
| 33 | 33 | */ |
| 34 | 34 | interface IJobList { |
| 35 | - /** |
|
| 36 | - * Add a job to the list |
|
| 37 | - * |
|
| 38 | - * @param \OCP\BackgroundJob\IJob|string $job |
|
| 39 | - * @param mixed $argument The argument to be passed to $job->run() when the job is exectured |
|
| 40 | - * @since 7.0.0 |
|
| 41 | - */ |
|
| 42 | - public function add($job, $argument = null); |
|
| 35 | + /** |
|
| 36 | + * Add a job to the list |
|
| 37 | + * |
|
| 38 | + * @param \OCP\BackgroundJob\IJob|string $job |
|
| 39 | + * @param mixed $argument The argument to be passed to $job->run() when the job is exectured |
|
| 40 | + * @since 7.0.0 |
|
| 41 | + */ |
|
| 42 | + public function add($job, $argument = null); |
|
| 43 | 43 | |
| 44 | - /** |
|
| 45 | - * Remove a job from the list |
|
| 46 | - * |
|
| 47 | - * @param \OCP\BackgroundJob\IJob|string $job |
|
| 48 | - * @param mixed $argument |
|
| 49 | - * @since 7.0.0 |
|
| 50 | - */ |
|
| 51 | - public function remove($job, $argument = null); |
|
| 44 | + /** |
|
| 45 | + * Remove a job from the list |
|
| 46 | + * |
|
| 47 | + * @param \OCP\BackgroundJob\IJob|string $job |
|
| 48 | + * @param mixed $argument |
|
| 49 | + * @since 7.0.0 |
|
| 50 | + */ |
|
| 51 | + public function remove($job, $argument = null); |
|
| 52 | 52 | |
| 53 | - /** |
|
| 54 | - * check if a job is in the list |
|
| 55 | - * |
|
| 56 | - * @param \OCP\BackgroundJob\IJob|string $job |
|
| 57 | - * @param mixed $argument |
|
| 58 | - * @return bool |
|
| 59 | - * @since 7.0.0 |
|
| 60 | - */ |
|
| 61 | - public function has($job, $argument); |
|
| 53 | + /** |
|
| 54 | + * check if a job is in the list |
|
| 55 | + * |
|
| 56 | + * @param \OCP\BackgroundJob\IJob|string $job |
|
| 57 | + * @param mixed $argument |
|
| 58 | + * @return bool |
|
| 59 | + * @since 7.0.0 |
|
| 60 | + */ |
|
| 61 | + public function has($job, $argument); |
|
| 62 | 62 | |
| 63 | - /** |
|
| 64 | - * get all jobs in the list |
|
| 65 | - * |
|
| 66 | - * @return \OCP\BackgroundJob\IJob[] |
|
| 67 | - * @since 7.0.0 |
|
| 68 | - * @deprecated 9.0.0 - This method is dangerous since it can cause load and |
|
| 69 | - * memory problems when creating too many instances. |
|
| 70 | - */ |
|
| 71 | - public function getAll(); |
|
| 63 | + /** |
|
| 64 | + * get all jobs in the list |
|
| 65 | + * |
|
| 66 | + * @return \OCP\BackgroundJob\IJob[] |
|
| 67 | + * @since 7.0.0 |
|
| 68 | + * @deprecated 9.0.0 - This method is dangerous since it can cause load and |
|
| 69 | + * memory problems when creating too many instances. |
|
| 70 | + */ |
|
| 71 | + public function getAll(); |
|
| 72 | 72 | |
| 73 | - /** |
|
| 74 | - * get the next job in the list |
|
| 75 | - * |
|
| 76 | - * @return \OCP\BackgroundJob\IJob|null |
|
| 77 | - * @since 7.0.0 |
|
| 78 | - */ |
|
| 79 | - public function getNext(); |
|
| 73 | + /** |
|
| 74 | + * get the next job in the list |
|
| 75 | + * |
|
| 76 | + * @return \OCP\BackgroundJob\IJob|null |
|
| 77 | + * @since 7.0.0 |
|
| 78 | + */ |
|
| 79 | + public function getNext(); |
|
| 80 | 80 | |
| 81 | - /** |
|
| 82 | - * @param int $id |
|
| 83 | - * @return \OCP\BackgroundJob\IJob|null |
|
| 84 | - * @since 7.0.0 |
|
| 85 | - */ |
|
| 86 | - public function getById($id); |
|
| 81 | + /** |
|
| 82 | + * @param int $id |
|
| 83 | + * @return \OCP\BackgroundJob\IJob|null |
|
| 84 | + * @since 7.0.0 |
|
| 85 | + */ |
|
| 86 | + public function getById($id); |
|
| 87 | 87 | |
| 88 | - /** |
|
| 89 | - * set the job that was last ran to the current time |
|
| 90 | - * |
|
| 91 | - * @param \OCP\BackgroundJob\IJob $job |
|
| 92 | - * @since 7.0.0 |
|
| 93 | - */ |
|
| 94 | - public function setLastJob(IJob $job); |
|
| 88 | + /** |
|
| 89 | + * set the job that was last ran to the current time |
|
| 90 | + * |
|
| 91 | + * @param \OCP\BackgroundJob\IJob $job |
|
| 92 | + * @since 7.0.0 |
|
| 93 | + */ |
|
| 94 | + public function setLastJob(IJob $job); |
|
| 95 | 95 | |
| 96 | - /** |
|
| 97 | - * Remove the reservation for a job |
|
| 98 | - * |
|
| 99 | - * @param IJob $job |
|
| 100 | - * @since 9.1.0 |
|
| 101 | - */ |
|
| 102 | - public function unlockJob(IJob $job); |
|
| 96 | + /** |
|
| 97 | + * Remove the reservation for a job |
|
| 98 | + * |
|
| 99 | + * @param IJob $job |
|
| 100 | + * @since 9.1.0 |
|
| 101 | + */ |
|
| 102 | + public function unlockJob(IJob $job); |
|
| 103 | 103 | |
| 104 | - /** |
|
| 105 | - * get the id of the last ran job |
|
| 106 | - * |
|
| 107 | - * @return int |
|
| 108 | - * @since 7.0.0 |
|
| 109 | - * @deprecated 9.1.0 - The functionality behind the value is deprecated, it |
|
| 110 | - * only tells you which job finished last, but since we now allow multiple |
|
| 111 | - * executors to run in parallel, it's not used to calculate the next job. |
|
| 112 | - */ |
|
| 113 | - public function getLastJob(); |
|
| 104 | + /** |
|
| 105 | + * get the id of the last ran job |
|
| 106 | + * |
|
| 107 | + * @return int |
|
| 108 | + * @since 7.0.0 |
|
| 109 | + * @deprecated 9.1.0 - The functionality behind the value is deprecated, it |
|
| 110 | + * only tells you which job finished last, but since we now allow multiple |
|
| 111 | + * executors to run in parallel, it's not used to calculate the next job. |
|
| 112 | + */ |
|
| 113 | + public function getLastJob(); |
|
| 114 | 114 | |
| 115 | - /** |
|
| 116 | - * set the lastRun of $job to now |
|
| 117 | - * |
|
| 118 | - * @param IJob $job |
|
| 119 | - * @since 7.0.0 |
|
| 120 | - */ |
|
| 121 | - public function setLastRun(IJob $job); |
|
| 115 | + /** |
|
| 116 | + * set the lastRun of $job to now |
|
| 117 | + * |
|
| 118 | + * @param IJob $job |
|
| 119 | + * @since 7.0.0 |
|
| 120 | + */ |
|
| 121 | + public function setLastRun(IJob $job); |
|
| 122 | 122 | |
| 123 | - /** |
|
| 124 | - * set the run duration of $job |
|
| 125 | - * |
|
| 126 | - * @param IJob $job |
|
| 127 | - * @param $timeTaken |
|
| 128 | - * @since 12.0.0 |
|
| 129 | - */ |
|
| 130 | - public function setExecutionTime(IJob $job, $timeTaken); |
|
| 123 | + /** |
|
| 124 | + * set the run duration of $job |
|
| 125 | + * |
|
| 126 | + * @param IJob $job |
|
| 127 | + * @param $timeTaken |
|
| 128 | + * @since 12.0.0 |
|
| 129 | + */ |
|
| 130 | + public function setExecutionTime(IJob $job, $timeTaken); |
|
| 131 | 131 | } |