-
Notifications
You must be signed in to change notification settings - Fork 142
/
CronjobScheduler.class.php
328 lines (290 loc) · 11.2 KB
/
CronjobScheduler.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
<?php
namespace wcf\system\cronjob;
use wcf\data\cronjob\Cronjob;
use wcf\data\cronjob\CronjobEditor;
use wcf\data\cronjob\log\CronjobLogEditor;
use wcf\data\user\User;
use wcf\system\cache\builder\CronjobCacheBuilder;
use wcf\system\exception\ClassNotFoundException;
use wcf\system\exception\ImplementationException;
use wcf\system\exception\SystemException;
use wcf\system\session\SessionHandler;
use wcf\system\SingletonFactory;
use wcf\system\WCF;
/**
* Provides functions to execute cronjobs.
*
* @author Alexander Ebert
* @copyright 2001-2019 WoltLab GmbH
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
*/
final class CronjobScheduler extends SingletonFactory
{
/**
* cached times of the next and after next cronjob execution
* @var int[]
*/
protected $cache = [];
/**
* @inheritDoc
*/
protected function init()
{
$this->loadCache();
}
/**
* Executes outstanding cronjobs.
*/
public function executeCronjobs()
{
// break if there are no outstanding cronjobs
if ($this->cache['nextExec'] > TIME_NOW && $this->cache['afterNextExec'] > TIME_NOW) {
return;
}
$this->resetFailedCronjobs();
// get outstanding cronjobs
$cronjobEditors = $this->loadCronjobs();
// clear cache
self::clearCache();
$user = WCF::getUser();
try {
SessionHandler::getInstance()->changeUser(new User(null), true);
foreach ($cronjobEditors as $cronjobEditor) {
// mark cronjob as being executed
$cronjobEditor->update([
'state' => Cronjob::EXECUTING,
]);
// create log entry
$log = CronjobLogEditor::create([
'cronjobID' => $cronjobEditor->cronjobID,
'execTime' => TIME_NOW,
]);
$logEditor = new CronjobLogEditor($log);
// check if all required options are set for cronjob to be executed
// note: a general log is created to avoid confusion why a cronjob
// apparently is not executed while that is indeed the correct internal
// behavior
if ($cronjobEditor->validateOptions()) {
try {
$this->executeCronjob($cronjobEditor, $logEditor);
} catch (\Throwable $e) {
$this->logResult($logEditor, $e);
} catch (\Exception $e) {
$this->logResult($logEditor, $e);
}
} else {
$this->logResult($logEditor);
}
// mark cronjob as done
$cronjobEditor->update([
'failCount' => 0,
'state' => Cronjob::READY,
]);
}
} finally {
SessionHandler::getInstance()->changeUser($user, true);
}
}
/**
* Returns the next execution time.
*
* @return int
*/
public function getNextExec()
{
return $this->cache['nextExec'];
}
/**
* Resets any cronjobs that have previously failed to execute. Cronjobs that have failed too often will
* be disabled automatically.
*/
private function resetFailedCronjobs()
{
WCF::getDB()->beginTransaction();
/** @noinspection PhpUnusedLocalVariableInspection */
$committed = false;
try {
$sql = "SELECT *
FROM wcf" . WCF_N . "_cronjob
WHERE state <> ?
AND isDisabled = ?
AND afterNextExec <= ?
FOR UPDATE";
$statement = WCF::getDB()->prepareStatement($sql);
$statement->execute([
Cronjob::READY,
0,
TIME_NOW,
]);
/** @var Cronjob $cronjob */
while ($cronjob = $statement->fetchObject(Cronjob::class)) {
// In any case: Reset the state to READY.
$data['state'] = Cronjob::READY;
switch ($cronjob->state) {
case Cronjob::EXECUTING:
// The cronjob spent two periods in the EXECUTING state.
// We must assume it crashed.
$data['failCount'] = $cronjob->failCount + 1;
// The cronjob exceeded the maximum fail count.
// Cronjobs that can be disabled, should be disabled.
if ($data['failCount'] >= Cronjob::MAX_FAIL_COUNT) {
if ($cronjob->canBeDisabled) {
$data['isDisabled'] = 1;
$data['failCount'] = 0;
} else {
// Reset failCount for cronjobs, which can't be disabled to
// MAX_FAIL_COUNT - 1, because the column has a max length
// which should not be reached.
$data['failCount'] = Cronjob::MAX_FAIL_COUNT - 1;
}
}
// no break
case Cronjob::PENDING:
// The cronjob spent two periods in the PENDING state.
// We must assume a previous cronjob in the same request hosed
// the whole process (e.g. by exceeding the memory limit).
// This is not the fault of this cronjob, thus the fail counter
// is not being increased.
$log = CronjobLogEditor::create([
'cronjobID' => $cronjob->cronjobID,
'execTime' => TIME_NOW,
]);
$logEditor = new CronjobLogEditor($log);
$errorMessage = \sprintf(
"The cronjob '%s' (ID %d) appears to have failed. (nextExec %d, afterNextExec %d, now %d)",
$cronjob->cronjobName,
$cronjob->cronjobID,
$cronjob->nextExec,
$cronjob->afterNextExec,
TIME_NOW
);
$this->logResult($logEditor, new \Exception($errorMessage));
break;
default:
throw new \LogicException('Unreachable');
}
// Schedule the cronjob for execution at the next regular execution date. The previous
// implementation was executing the cronjob immediately, which may be undesirable if
// the cronjob is expected to be executed in a specific time window only.
$data['nextExec'] = $cronjob->getNextExec(TIME_NOW);
$data['afterNextExec'] = $cronjob->getNextExec($data['nextExec'] + 120);
(new CronjobEditor($cronjob))->update($data);
}
WCF::getDB()->commitTransaction();
$committed = true;
} finally {
if (!$committed) {
WCF::getDB()->rollBackTransaction();
}
}
}
/**
* Loads outstanding cronjobs.
*/
private function loadCronjobs()
{
WCF::getDB()->beginTransaction();
/** @noinspection PhpUnusedLocalVariableInspection */
$committed = false;
try {
$sql = "SELECT *
FROM wcf" . WCF_N . "_cronjob
WHERE isDisabled = ?
AND state = ?
AND nextExec <= ?
ORDER BY failCount ASC
FOR UPDATE";
$statement = WCF::getDB()->prepareStatement($sql);
$statement->execute([
0,
Cronjob::READY,
TIME_NOW,
]);
$cronjobEditors = [];
/** @var Cronjob $cronjob */
while ($cronjob = $statement->fetchObject(Cronjob::class)) {
$cronjobEditor = new CronjobEditor($cronjob);
// Mark the cronjob as pending to prevent concurrent requests from executing it.
$data = ['state' => Cronjob::PENDING];
// Update next execution time.
// This needs to be done before executing the job to allow for a proper detection of
// stuck jobs. If the timestamps are updated after executing then concurrent requests
// might believe that a cronjob is stuck, despite the cronjob just having started just
// a few milliseconds before.
$data['nextExec'] = $cronjob->getNextExec();
$data['afterNextExec'] = $cronjob->getNextExec($data['nextExec'] + 120);
$data['lastExec'] = TIME_NOW;
$cronjobEditor->update($data);
$cronjobEditors[] = $cronjobEditor;
}
WCF::getDB()->commitTransaction();
$committed = true;
return $cronjobEditors;
} finally {
if (!$committed) {
WCF::getDB()->rollBackTransaction();
}
}
}
/**
* Executes a cronjob.
*
* @throws SystemException
*/
private function executeCronjob(CronjobEditor $cronjobEditor, CronjobLogEditor $logEditor)
{
$className = $cronjobEditor->className;
if (!\class_exists($className)) {
throw new ClassNotFoundException($className);
}
// verify class signature
if (!\is_subclass_of($className, ICronjob::class)) {
throw new ImplementationException($className, ICronjob::class);
}
// execute cronjob
/** @var ICronjob $cronjob */
$cronjob = new $className();
$cronjob->execute($cronjobEditor->getDecoratedObject());
$this->logResult($logEditor);
}
/**
* Logs cronjob exec success or failure.
*
* @param \Throwable $exception
*/
private function logResult(CronjobLogEditor $logEditor, $exception = null)
{
if ($exception !== null) {
\wcf\functions\exception\logThrowable($exception);
$errString = \implode("\n", [
$exception->getMessage(),
$exception->getCode(),
$exception->getFile(),
$exception->getLine(),
$exception->getTraceAsString(),
]);
$logEditor->update([
'success' => 0,
'error' => $errString,
]);
} else {
$logEditor->update([
'success' => 1,
]);
}
}
/**
* Loads the cached data for cronjob execution.
*/
private function loadCache()
{
$this->cache = CronjobCacheBuilder::getInstance()->getData();
}
/**
* Clears the cronjob data cache.
*/
public static function clearCache()
{
CronjobCacheBuilder::getInstance()->reset();
}
}