Passed
Push — master ( f66003...c58f8d )
by Joas
14:36 queued 13s
created

TimedJob::isTimeSensitive()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @copyright Copyright (c) 2018, Roeland Jago Douma <[email protected]>
7
 *
8
 * @author Christoph Wurst <[email protected]>
9
 * @author Roeland Jago Douma <[email protected]>
10
 *
11
 * @license GNU AGPL version 3 or any later version
12
 *
13
 * This program is free software: you can redistribute it and/or modify
14
 * it under the terms of the GNU Affero General Public License as
15
 * published by the Free Software Foundation, either version 3 of the
16
 * License, or (at your option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
 * GNU Affero General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU Affero General Public License
24
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25
 *
26
 */
27
namespace OCP\BackgroundJob;
28
29
use OC\BackgroundJob\JobList;
30
use OCP\ILogger;
31
32
/**
33
 * Simple base class to extend to run periodic background jobs.
34
 * Call setInterval with your desired interval in seconds from the constructor.
35
 *
36
 * @since 15.0.0
37
 */
38
abstract class TimedJob extends Job {
39
	/** @var int */
40
	protected $interval = 0;
41
	/** @var int */
42
	protected $timeSensitivity = IJob::TIME_SENSITIVE;
43
44
	/**
45
	 * set the interval for the job
46
	 *
47
	 * @param int $seconds the time to pass between two runs of the same job in seconds
48
	 *
49
	 * @since 15.0.0
50
	 */
51
	public function setInterval(int $seconds) {
52
		$this->interval = $seconds;
53
	}
54
55
	/**
56
	 * Whether the background job is time sensitive and needs to run soon after
57
	 * the scheduled interval, of if it is okay to be delayed until a later time.
58
	 *
59
	 * @return bool
60
	 * @since 24.0.0
61
	 */
62
	public function isTimeSensitive(): bool {
63
		return $this->timeSensitivity === IJob::TIME_SENSITIVE;
64
	}
65
66
	/**
67
	 * If your background job is not time sensitive (sending instant email
68
	 * notifications, etc.) it would be nice to set it to IJob::TIME_INSENSITIVE
69
	 * This way the execution can be delayed during high usage times.
70
	 *
71
	 * @param int $sensitivity
72
	 * @psalm-param IJob::TIME_* $sensitivity
73
	 * @return void
74
	 * @since 24.0.0
75
	 */
76
	public function setTimeSensitivity(int $sensitivity): void {
77
		if ($sensitivity !== IJob::TIME_SENSITIVE &&
78
			$sensitivity !== IJob::TIME_INSENSITIVE) {
79
			throw new \InvalidArgumentException('Invalid sensitivity');
80
		}
81
82
		$this->timeSensitivity = $sensitivity;
83
	}
84
85
	/**
86
	 * run the job if the last run is is more than the interval ago
87
	 *
88
	 * @param JobList $jobList
89
	 * @param ILogger|null $logger
90
	 *
91
	 * @since 15.0.0
92
	 */
93
	final public function execute($jobList, ILogger $logger = null) {
94
		if (($this->time->getTime() - $this->lastRun) > $this->interval) {
95
			parent::execute($jobList, $logger);
96
		}
97
	}
98
}
99