-
Notifications
You must be signed in to change notification settings - Fork 148
Expand file tree
/
Copy pathBackgroundQueueHandler.class.php
More file actions
290 lines (258 loc) · 9.6 KB
/
BackgroundQueueHandler.class.php
File metadata and controls
290 lines (258 loc) · 9.6 KB
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
<?php
namespace wcf\system\background;
use wcf\data\user\User;
use wcf\system\background\job\AbstractBackgroundJob;
use wcf\system\background\job\AbstractUniqueBackgroundJob;
use wcf\system\database\util\PreparedStatementConditionBuilder;
use wcf\system\exception\ParentClassException;
use wcf\system\session\SessionHandler;
use wcf\system\SingletonFactory;
use wcf\system\WCF;
/**
* Manages the background queue.
*
* @author Tim Duesterhus
* @copyright 2001-2022 WoltLab GmbH
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
* @since 3.0
*/
final class BackgroundQueueHandler extends SingletonFactory
{
public const FORCE_CHECK_HTTP_HEADER_NAME = 'woltlab-background-queue-check';
public const FORCE_CHECK_HTTP_HEADER_VALUE = 'yes';
private bool $hasPendingCheck = false;
/**
* Forces checking whether a background queue item is due.
* This means that the AJAX request to BackgroundQueuePerformAction is triggered.
*/
public function forceCheck(): void
{
WCF::getSession()->register('forceBackgroundQueuePerform', true);
WCF::getTPL()->assign([
'forceBackgroundQueuePerform' => true,
]);
$this->hasPendingCheck = true;
}
/**
* Enqueues the given job(s) for execution in the specified number of
* seconds. Defaults to "as soon as possible" (0 seconds).
*
* @param AbstractBackgroundJob|AbstractBackgroundJob[] $jobs
* @param $time Minimum number of seconds to wait before performing the job.
* @see \wcf\system\background\BackgroundQueueHandler::enqueueAt()
*/
public function enqueueIn(AbstractBackgroundJob|array $jobs, int $time = 0): void
{
$this->enqueueAt($jobs, TIME_NOW + $time);
}
/**
* Enqueues the given job(s) for execution at the given time.
* Note: The time is a minimum time. Depending on the size of
* the queue the job can be performed later as well!
*
* @param AbstractBackgroundJob|AbstractBackgroundJob[] $jobs
* @param $time Earliest time to consider the job for execution.
* @throws \InvalidArgumentException
*/
public function enqueueAt(AbstractBackgroundJob|array $jobs, int $time): void
{
if ($time < TIME_NOW) {
throw new \InvalidArgumentException("You may not schedule a job in the past (" . $time . " is smaller than the current timestamp " . TIME_NOW . ").");
}
if (!\is_array($jobs)) {
$jobs = [$jobs];
}
$identifiers = [];
foreach ($jobs as $job) {
if (!($job instanceof AbstractBackgroundJob)) {
throw new ParentClassException(\get_class($job), AbstractBackgroundJob::class);
}
if ($job instanceof AbstractUniqueBackgroundJob) {
$identifiers[] = $job->identifier();
}
}
if ($identifiers !== []) {
$conditions = new PreparedStatementConditionBuilder();
$conditions->add("identifier IN (?)", [$identifiers]);
$sql = "SELECT DISTINCT identifier
FROM wcf1_background_job
{$conditions}";
$statement = WCF::getDB()->prepare($sql);
$statement->execute($conditions->getParameters());
$existingJobs = $statement->fetchAll(\PDO::FETCH_COLUMN);
$jobs = \array_filter(
$jobs,
function ($job) use ($existingJobs) {
if ($job instanceof AbstractUniqueBackgroundJob && \in_array($job->identifier(), $existingJobs)) {
return false;
}
return true;
}
);
if ($jobs === []) {
return;
}
}
$sql = "INSERT INTO wcf1_background_job
(job, time, identifier)
VALUES (?, ?, ?)";
$statement = WCF::getDB()->prepare($sql);
foreach ($jobs as $job) {
$identifier = null;
if ($job instanceof AbstractUniqueBackgroundJob) {
$identifier = $job->identifier();
}
$statement->execute([
\serialize($job),
$time,
$identifier,
]);
}
}
/**
* Immediately performs the given job.
* This method automatically handles requeuing in case of failure.
*
* This method is used internally by performNextJob(), but it can
* be useful if you wish immediate execution of a certain job, but
* don't want to miss the automated error handling mechanism of the
* queue.
*
* @param $debugSynchronousExecution Disables fail-safe mechanisms, errors will no longer be suppressed.
* @throws \Throwable
*/
public function performJob(AbstractBackgroundJob $job, bool $debugSynchronousExecution = false): void
{
$user = WCF::getUser();
try {
SessionHandler::getInstance()->changeUser(new User(null), true);
if (!WCF::debugModeIsEnabled()) {
\ob_start();
}
$job->perform();
} catch (\Throwable $e) {
// do not suppress exceptions for debugging purposes, see https://github.com/WoltLab/WCF/issues/2501
if ($debugSynchronousExecution) {
throw $e;
}
$job->fail();
if ($job->getFailures() <= $job::MAX_FAILURES) {
$this->enqueueIn($job, $job->retryAfter());
if (WCF::debugModeIsEnabled()) {
\wcf\functions\exception\logThrowable($e);
}
} else {
$job->onFinalFailure();
// job failed too often: log
\wcf\functions\exception\logThrowable($e);
}
} finally {
if (!WCF::debugModeIsEnabled()) {
\ob_end_clean();
}
SessionHandler::getInstance()->changeUser($user, true);
}
}
/**
* Performs the (single) job that is due next.
* This method automatically handles requeuing in case of failure.
*
* @return bool true if this call attempted to execute a job regardless of its result
*/
public function performNextJob(): bool
{
WCF::getDB()->beginTransaction();
$committed = false;
try {
$sql = "SELECT jobID, job
FROM wcf1_background_job
WHERE status = ?
AND time <= ?
ORDER BY time ASC, jobID ASC
FOR UPDATE";
$statement = WCF::getDB()->prepare($sql, 1);
$statement->execute([
'ready',
TIME_NOW,
]);
$row = $statement->fetchSingleRow();
if (!$row) {
// nothing to do here
return false;
}
// lock job
$sql = "UPDATE wcf1_background_job
SET status = ?,
time = ?
WHERE jobID = ?
AND status = ?";
$statement = WCF::getDB()->prepare($sql);
$statement->execute([
'processing',
TIME_NOW,
$row['jobID'],
'ready',
]);
if ($statement->getAffectedRows() != 1) {
// somebody stole the job
// this cannot happen unless MySQL violates it's contract to lock the row
// -> silently ignore, there will be plenty of other opportunities to perform a job
return true;
}
WCF::getDB()->commitTransaction();
$committed = true;
} finally {
if (!$committed) {
WCF::getDB()->rollBackTransaction();
}
}
$job = null;
try {
// no shut up operator, exception will be caught
$job = \unserialize($row['job']);
if ($job) {
$this->performJob($job);
}
} catch (\Throwable $e) {
// job is completely broken: log
\wcf\functions\exception\logThrowable($e);
} finally {
// remove entry of processed job
$sql = "DELETE FROM wcf1_background_job
WHERE jobID = ?";
$statement = WCF::getDB()->prepare($sql);
$statement->execute([$row['jobID']]);
}
if ($job instanceof AbstractUniqueBackgroundJob && $job->queueAgain()) {
$this->enqueueIn($job->newInstance(), $job->retryAfter());
}
return true;
}
/**
* Returns how many items are due.
*
* Note: Do not rely on the return value being correct, some other process may
* have modified the queue contents, before this method returns. Think of it as an
* approximation to know whether you should spend some time to clear the queue.
*/
public function getRunnableCount(): int
{
$sql = "SELECT COUNT(*)
FROM wcf1_background_job
WHERE status = ?
AND time <= ?";
$statement = WCF::getDB()->prepare($sql);
$statement->execute(['ready', TIME_NOW]);
return $statement->fetchSingleColumn();
}
/**
* Indicates that the client should trigger a check for
* pending jobs in the background queue.
*
* @since 6.0
*/
public function hasPendingCheck(): bool
{
return $this->hasPendingCheck;
}
}