|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
/** |
|
4
|
|
|
* @copyright Copyright (c) 2018, Roeland Jago Douma <[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* @author Roeland Jago Douma <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* @license GNU AGPL version 3 or any later version |
|
9
|
|
|
* |
|
10
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
11
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
12
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
13
|
|
|
* License, or (at your option) any later version. |
|
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 |
|
21
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
22
|
|
|
* |
|
23
|
|
|
*/ |
|
24
|
|
|
|
|
25
|
|
|
namespace OCP\BackgroundJob; |
|
26
|
|
|
|
|
27
|
|
|
use OCP\AppFramework\Utility\ITimeFactory; |
|
28
|
|
|
use OCP\ILogger; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Base class for background jobs |
|
32
|
|
|
* |
|
33
|
|
|
* This is here if you want to do advanced stuff in your background jobs. |
|
34
|
|
|
* For the most common use cases have a look at QueuedJob and TimedJob |
|
35
|
|
|
* |
|
36
|
|
|
* @since 15.0.0 |
|
37
|
|
|
*/ |
|
38
|
|
|
abstract class Job implements IJob { |
|
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
|
|
View Code Duplication |
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); |
|
143
|
|
|
} |
|
144
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: