Passed
Push — master ( d96e2c...35e3d4 )
by Morris
24:18 queued 10:44
created

JobList::isAnyJobRunning()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 11
nc 2
nop 0
dl 0
loc 14
rs 9.9
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Joas Schilling <[email protected]>
6
 * @author Jörn Friedrich Dreyer <[email protected]>
7
 * @author Lukas Reschke <[email protected]>
8
 * @author Morris Jobke <[email protected]>
9
 * @author Noveen Sachdeva <[email protected]>
10
 * @author Robin Appelman <[email protected]>
11
 * @author Robin McCorkell <[email protected]>
12
 *
13
 * @license AGPL-3.0
14
 *
15
 * This code is free software: you can redistribute it and/or modify
16
 * it under the terms of the GNU Affero General Public License, version 3,
17
 * as published by the Free Software Foundation.
18
 *
19
 * This program is distributed in the hope that it will be useful,
20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22
 * GNU Affero General Public License for more details.
23
 *
24
 * You should have received a copy of the GNU Affero General Public License, version 3,
25
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
26
 *
27
 */
28
29
namespace OC\BackgroundJob;
30
31
use OCP\AppFramework\QueryException;
32
use OCP\AppFramework\Utility\ITimeFactory;
33
use OCP\BackgroundJob\IJob;
34
use OCP\BackgroundJob\IJobList;
35
use OCP\AutoloadNotAllowedException;
36
use OCP\DB\QueryBuilder\IQueryBuilder;
37
use OCP\IConfig;
38
use OCP\IDBConnection;
39
40
class JobList implements IJobList {
41
42
	/** @var IDBConnection */
43
	protected $connection;
44
45
	/**@var IConfig */
46
	protected $config;
47
48
	/**@var ITimeFactory */
49
	protected $timeFactory;
50
51
	/**
52
	 * @param IDBConnection $connection
53
	 * @param IConfig $config
54
	 * @param ITimeFactory $timeFactory
55
	 */
56
	public function __construct(IDBConnection $connection, IConfig $config, ITimeFactory $timeFactory) {
57
		$this->connection = $connection;
58
		$this->config = $config;
59
		$this->timeFactory = $timeFactory;
60
	}
61
62
	/**
63
	 * @param IJob|string $job
64
	 * @param mixed $argument
65
	 */
66
	public function add($job, $argument = null) {
67
		if (!$this->has($job, $argument)) {
68
			if ($job instanceof IJob) {
69
				$class = get_class($job);
70
			} else {
71
				$class = $job;
72
			}
73
74
			$argument = json_encode($argument);
75
			if (strlen($argument) > 4000) {
76
				throw new \InvalidArgumentException('Background job arguments can\'t exceed 4000 characters (json encoded)');
77
			}
78
79
			$query = $this->connection->getQueryBuilder();
80
			$query->insert('jobs')
81
				->values([
82
					'class' => $query->createNamedParameter($class),
83
					'argument' => $query->createNamedParameter($argument),
84
					'last_run' => $query->createNamedParameter(0, IQueryBuilder::PARAM_INT),
85
					'last_checked' => $query->createNamedParameter($this->timeFactory->getTime(), IQueryBuilder::PARAM_INT),
86
				]);
87
			$query->execute();
88
		}
89
	}
90
91
	/**
92
	 * @param IJob|string $job
93
	 * @param mixed $argument
94
	 */
95
	public function remove($job, $argument = null) {
96
		if ($job instanceof IJob) {
97
			$class = get_class($job);
98
		} else {
99
			$class = $job;
100
		}
101
102
		$query = $this->connection->getQueryBuilder();
103
		$query->delete('jobs')
104
			->where($query->expr()->eq('class', $query->createNamedParameter($class)));
105
		if (!is_null($argument)) {
106
			$argument = json_encode($argument);
107
			$query->andWhere($query->expr()->eq('argument', $query->createNamedParameter($argument)));
108
		}
109
		$query->execute();
110
	}
111
112
	/**
113
	 * @param int $id
114
	 */
115
	protected function removeById($id) {
116
		$query = $this->connection->getQueryBuilder();
117
		$query->delete('jobs')
118
			->where($query->expr()->eq('id', $query->createNamedParameter($id, IQueryBuilder::PARAM_INT)));
119
		$query->execute();
120
	}
121
122
	/**
123
	 * check if a job is in the list
124
	 *
125
	 * @param IJob|string $job
126
	 * @param mixed $argument
127
	 * @return bool
128
	 */
129
	public function has($job, $argument) {
130
		if ($job instanceof IJob) {
131
			$class = get_class($job);
132
		} else {
133
			$class = $job;
134
		}
135
		$argument = json_encode($argument);
136
137
		$query = $this->connection->getQueryBuilder();
138
		$query->select('id')
139
			->from('jobs')
140
			->where($query->expr()->eq('class', $query->createNamedParameter($class)))
141
			->andWhere($query->expr()->eq('argument', $query->createNamedParameter($argument)))
142
			->setMaxResults(1);
143
144
		$result = $query->execute();
145
		$row = $result->fetch();
146
		$result->closeCursor();
147
148
		return (bool) $row;
149
	}
150
151
	/**
152
	 * get all jobs in the list
153
	 *
154
	 * @return IJob[]
155
	 * @deprecated 9.0.0 - This method is dangerous since it can cause load and
156
	 * memory problems when creating too many instances.
157
	 */
158
	public function getAll() {
159
		$query = $this->connection->getQueryBuilder();
160
		$query->select('*')
161
			->from('jobs');
162
		$result = $query->execute();
163
164
		$jobs = [];
165
		while ($row = $result->fetch()) {
166
			$job = $this->buildJob($row);
167
			if ($job) {
168
				$jobs[] = $job;
169
			}
170
		}
171
		$result->closeCursor();
172
173
		return $jobs;
174
	}
175
176
	/**
177
	 * get the next job in the list
178
	 *
179
	 * @return IJob|null
180
	 * @suppress SqlInjectionChecker
181
	 */
182
	public function getNext() {
183
		$query = $this->connection->getQueryBuilder();
184
		$query->select('*')
185
			->from('jobs')
186
			->where($query->expr()->lte('reserved_at', $query->createNamedParameter($this->timeFactory->getTime() - 12 * 3600, IQueryBuilder::PARAM_INT)))
187
			->andWhere($query->expr()->lte('last_checked', $query->createNamedParameter($this->timeFactory->getTime(), IQueryBuilder::PARAM_INT)))
188
			->orderBy('last_checked', 'ASC')
189
			->setMaxResults(1);
190
191
		$update = $this->connection->getQueryBuilder();
192
		$update->update('jobs')
193
			->set('reserved_at', $update->createNamedParameter($this->timeFactory->getTime()))
194
			->set('last_checked', $update->createNamedParameter($this->timeFactory->getTime()))
195
			->where($update->expr()->eq('id', $update->createParameter('jobid')))
196
			->andWhere($update->expr()->eq('reserved_at', $update->createParameter('reserved_at')))
197
			->andWhere($update->expr()->eq('last_checked', $update->createParameter('last_checked')));
198
199
		$result = $query->execute();
200
		$row = $result->fetch();
201
		$result->closeCursor();
202
203
		if ($row) {
204
			$update->setParameter('jobid', $row['id']);
205
			$update->setParameter('reserved_at', $row['reserved_at']);
206
			$update->setParameter('last_checked', $row['last_checked']);
207
			$count = $update->execute();
208
209
			if ($count === 0) {
210
				// Background job already executed elsewhere, try again.
211
				return $this->getNext();
212
			}
213
			$job = $this->buildJob($row);
214
215
			if ($job === null) {
216
				// set the last_checked to 12h in the future to not check failing jobs all over again
217
				$reset = $this->connection->getQueryBuilder();
218
				$reset->update('jobs')
219
					->set('reserved_at', $reset->expr()->literal(0, IQueryBuilder::PARAM_INT))
220
					->set('last_checked', $reset->createNamedParameter($this->timeFactory->getTime() + 12 * 3600, IQueryBuilder::PARAM_INT))
221
					->where($reset->expr()->eq('id', $reset->createNamedParameter($row['id'], IQueryBuilder::PARAM_INT)));
222
				$reset->execute();
223
224
				// Background job from disabled app, try again.
225
				return $this->getNext();
226
			}
227
228
			return $job;
229
		} else {
230
			return null;
231
		}
232
	}
233
234
	/**
235
	 * @param int $id
236
	 * @return IJob|null
237
	 */
238
	public function getById($id) {
239
		$query = $this->connection->getQueryBuilder();
240
		$query->select('*')
241
			->from('jobs')
242
			->where($query->expr()->eq('id', $query->createNamedParameter($id, IQueryBuilder::PARAM_INT)));
243
		$result = $query->execute();
244
		$row = $result->fetch();
245
		$result->closeCursor();
246
247
		if ($row) {
248
			return $this->buildJob($row);
249
		} else {
250
			return null;
251
		}
252
	}
253
254
	/**
255
	 * get the job object from a row in the db
256
	 *
257
	 * @param array $row
258
	 * @return IJob|null
259
	 */
260
	private function buildJob($row) {
261
		try {
262
			try {
263
				// Try to load the job as a service
264
				/** @var IJob $job */
265
				$job = \OC::$server->query($row['class']);
266
			} catch (QueryException $e) {
267
				if (class_exists($row['class'])) {
268
					$class = $row['class'];
269
					$job = new $class();
270
				} else {
271
					// job from disabled app or old version of an app, no need to do anything
272
					return null;
273
				}
274
			}
275
276
			$job->setId($row['id']);
277
			$job->setLastRun($row['last_run']);
278
			$job->setArgument(json_decode($row['argument'], true));
279
			return $job;
280
		} catch (AutoloadNotAllowedException $e) {
281
			// job is from a disabled app, ignore
282
			return null;
283
		}
284
	}
285
286
	/**
287
	 * set the job that was last ran
288
	 *
289
	 * @param IJob $job
290
	 */
291
	public function setLastJob(IJob $job) {
292
		$this->unlockJob($job);
293
		$this->config->setAppValue('backgroundjob', 'lastjob', $job->getId());
294
	}
295
296
	/**
297
	 * Remove the reservation for a job
298
	 *
299
	 * @param IJob $job
300
	 * @suppress SqlInjectionChecker
301
	 */
302
	public function unlockJob(IJob $job) {
303
		$query = $this->connection->getQueryBuilder();
304
		$query->update('jobs')
305
			->set('reserved_at', $query->expr()->literal(0, IQueryBuilder::PARAM_INT))
306
			->where($query->expr()->eq('id', $query->createNamedParameter($job->getId(), IQueryBuilder::PARAM_INT)));
307
		$query->execute();
308
	}
309
310
	/**
311
	 * get the id of the last ran job
312
	 *
313
	 * @return int
314
	 * @deprecated 9.1.0 - The functionality behind the value is deprecated, it
315
	 *    only tells you which job finished last, but since we now allow multiple
316
	 *    executors to run in parallel, it's not used to calculate the next job.
317
	 */
318
	public function getLastJob() {
319
		return (int) $this->config->getAppValue('backgroundjob', 'lastjob', 0);
320
	}
321
322
	/**
323
	 * set the lastRun of $job to now
324
	 *
325
	 * @param IJob $job
326
	 */
327
	public function setLastRun(IJob $job) {
328
		$query = $this->connection->getQueryBuilder();
329
		$query->update('jobs')
330
			->set('last_run', $query->createNamedParameter(time(), IQueryBuilder::PARAM_INT))
331
			->where($query->expr()->eq('id', $query->createNamedParameter($job->getId(), IQueryBuilder::PARAM_INT)));
332
		$query->execute();
333
	}
334
335
	/**
336
	 * @param IJob $job
337
	 * @param $timeTaken
338
	 */
339
	public function setExecutionTime(IJob $job, $timeTaken) {
340
		$query = $this->connection->getQueryBuilder();
341
		$query->update('jobs')
342
			->set('execution_duration', $query->createNamedParameter($timeTaken, IQueryBuilder::PARAM_INT))
343
			->where($query->expr()->eq('id', $query->createNamedParameter($job->getId(), IQueryBuilder::PARAM_INT)));
344
		$query->execute();
345
	}
346
}
347