Completed
Push — master ( 10ec3f...4dab01 )
by Morris
15:18 queued 03:52
created

Job::getArgument()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Morris Jobke <[email protected]>
6
 * @author Robin Appelman <[email protected]>
7
 * @author Thomas Müller <[email protected]>
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
25
namespace OC\BackgroundJob;
26
27
use OCP\BackgroundJob\IJob;
28
use OCP\ILogger;
29
30
abstract class Job implements IJob {
31
	/**
32
	 * @var int $id
33
	 */
34
	protected $id;
35
36
	/**
37
	 * @var int $lastRun
38
	 */
39
	protected $lastRun;
40
41
	/**
42
	 * @var mixed $argument
43
	 */
44
	protected $argument;
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
		}
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;
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
	}
73
74
	abstract protected function run($argument);
75
76
	public function setId($id) {
77
		$this->id = $id;
78
	}
79
80
	public function setLastRun($lastRun) {
81
		$this->lastRun = $lastRun;
82
	}
83
84
	public function setArgument($argument) {
85
		$this->argument = $argument;
86
	}
87
88
	public function getId() {
89
		return $this->id;
90
	}
91
92
	public function getLastRun() {
93
		return $this->lastRun;
94
	}
95
96
	public function getArgument() {
97
		return $this->argument;
98
	}
99
}
100