-
Notifications
You must be signed in to change notification settings - Fork 139
Expand file tree
/
Copy pathQueueController.php
More file actions
331 lines (279 loc) · 9.37 KB
/
Copy pathQueueController.php
File metadata and controls
331 lines (279 loc) · 9.37 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
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
329
330
331
<?php
declare(strict_types=1);
namespace Queue\Controller\Admin;
use Cake\Core\App;
use Cake\Core\Configure;
use Cake\Http\Exception\NotFoundException;
use Queue\Queue\AddFromBackendInterface;
use Queue\Queue\AddInterface;
use Queue\Queue\TaskFinder;
/**
* @property \Queue\Model\Table\QueuedJobsTable $QueuedJobs
* @property \Queue\Model\Table\QueueProcessesTable $QueueProcesses
*/
class QueueController extends QueueAppController {
/**
* @var string|null
*/
protected ?string $defaultTable = 'Queue.QueuedJobs';
/**
* @return void
*/
public function initialize(): void {
parent::initialize();
// Set connection for multi-connection support
if ($this->activeConnection !== 'default') {
$this->QueuedJobs->setConnection($this->getActiveConnectionObject());
}
}
/**
* Admin center.
* Manage queues from admin backend (without the need to open ssh console window).
*
* @return \Cake\Http\Response|null|void
*/
public function index() {
$QueueProcesses = $this->fetchTable('Queue.QueueProcesses');
if ($this->activeConnection !== 'default') {
$QueueProcesses->setConnection($this->getActiveConnectionObject());
}
$status = $QueueProcesses->status();
$current = $this->QueuedJobs->getLength();
// Cap how many pending/scheduled rows we materialise and pass to the
// view. Without a cap a backlog of thousands of pending jobs makes
// the dashboard explode: heavy per-row HTML in the response, and
// DebugKit's Variables panel serialising every entity for its
// snapshot can OOM the request. Aggregate tile counts on the
// dashboard keep using DB-side count() queries so they reflect the
// true totals regardless of the visible-list cap.
$detailsLimit = (int)Configure::read('Queue.adminDetailsLimit', 200);
// +1 row past the cap so we can detect truncation without a second
// count query in the small-backlog case.
$pendingDetails = $this->QueuedJobs->getPendingStats()->limit($detailsLimit + 1)->toArray();
$pendingDetailsTruncated = count($pendingDetails) > $detailsLimit;
if ($pendingDetailsTruncated) {
array_pop($pendingDetails);
}
$new = $this->QueuedJobs->getNewCount();
$scheduledDetails = $this->QueuedJobs->getScheduledStats()->limit($detailsLimit + 1)->toArray();
$scheduledDetailsTruncated = count($scheduledDetails) > $detailsLimit;
if ($scheduledDetailsTruncated) {
array_pop($scheduledDetails);
}
$data = $this->QueuedJobs->getStats();
$taskFinder = new TaskFinder();
$tasks = $taskFinder->all();
$addableTasks = $taskFinder->allAddable(AddFromBackendInterface::class);
$taskDescriptions = [];
foreach ($tasks as $task => $className) {
/** @var \Queue\Queue\Task $taskObject */
$taskObject = new $className();
$taskDescriptions[$task] = $taskObject->description();
}
$servers = $QueueProcesses->serverList();
$workers = $status ? $status['workers'] : 0;
// True totals from DB. When the visible list is truncated we need
// these for the "showing N of M" hint; when it isn't, they also
// happen to match $pendingDetails count (small extra query that
// avoids one branch in the derivation below).
$totalPending = $pendingDetailsTruncated
? $this->QueuedJobs->getPendingCount()
: count($pendingDetails);
$scheduledJobs = $scheduledDetailsTruncated
? $this->QueuedJobs->getScheduledCount()
: count($scheduledDetails);
$runningJobs = $this->QueuedJobs->find()
->where([
'completed IS' => null,
'fetched IS NOT' => null,
'failure_message IS' => null,
])
->count();
$failedJobs = $this->QueuedJobs->find()
->where([
'completed IS' => null,
'failure_message IS NOT' => null,
])
->count();
// Aborted jobs (retries exhausted) carry a failure_message but are
// terminal and already excluded from getPendingCount()/getPendingStats().
// Subtract only the still-retriable failures here so aborted rows are not
// removed twice (which would under-count, or zero out, real pending work).
$retriableFailedJobs = $this->QueuedJobs->find()
->where([
'completed IS' => null,
'failure_message IS NOT' => null,
'OR' => [
'status IS' => null,
'status !=' => $this->QueuedJobs::STATUS_ABORTED,
],
])
->count();
// Pending = total pending minus running and still-retriable failed (to avoid double counting)
$pendingJobs = max(0, $totalPending - $runningJobs - $retriableFailedJobs);
$configurations = (array)Configure::read('Queue');
$this->set(compact(
'new',
'current',
'data',
'pendingDetails',
'scheduledDetails',
'pendingDetailsTruncated',
'scheduledDetailsTruncated',
'detailsLimit',
'totalPending',
'status',
'tasks',
'addableTasks',
'taskDescriptions',
'servers',
'workers',
'pendingJobs',
'scheduledJobs',
'runningJobs',
'failedJobs',
'configurations',
));
}
/**
* @throws \Cake\Http\Exception\NotFoundException
*
* @return \Cake\Http\Response|null
*/
public function addJob() {
$this->request->allowMethod('post');
$job = (string)$this->request->getQuery('task');
if (!$job) {
throw new NotFoundException();
}
/** @var class-string<\Queue\Queue\Task> $className */
$className = App::className($job, 'Queue/Task', 'Task');
if (!$className) {
throw new NotFoundException('Class not found for job `' . $job . '`');
}
$object = new $className();
// Measure the QueuedJobs count before invoking the task so we can
// tell whether the task actually queued a job. AddInterface::add()
// returns void and may silently no-op when required config is
// missing (e.g. EmailTask without Config.adminEmail). Without this
// check the controller would falsely report success.
$before = $this->QueuedJobs->find()->count();
if ($object instanceof AddInterface) {
$object->add(null);
} else {
$this->QueuedJobs->createJob($job);
}
$after = $this->QueuedJobs->find()->count();
if ($after > $before) {
$this->Flash->success('Job ' . $job . ' added');
} else {
$this->Flash->error(
'Job ' . $job . ' could not be added — the task did not create a job. '
. 'Check configuration (e.g. Config.adminEmail for Queue.Email) and server logs.',
);
}
return $this->refererRedirect(['action' => 'index']);
}
/**
* @param int|null $id
*
* @throws \Cake\Http\Exception\NotFoundException
*
* @return \Cake\Http\Response|null
*/
public function resetJob(?int $id = null) {
$this->request->allowMethod('post');
if (!$id) {
throw new NotFoundException();
}
$this->QueuedJobs->reset($id);
$this->Flash->success('Job # ' . $id . ' re-added');
return $this->refererRedirect($this->referer(['action' => 'index'], true));
}
/**
* @param int|null $id
*
* @return \Cake\Http\Response|null
*/
public function removeJob(?int $id = null) {
$this->request->allowMethod('post');
$queuedJob = $this->QueuedJobs->get($id);
$this->QueuedJobs->delete($queuedJob);
$this->Flash->success('Job # ' . $id . ' deleted');
return $this->refererRedirect(['action' => 'index']);
}
/**
* @return \Cake\Http\Response|null|void
*/
public function processes() {
$QueueProcesses = $this->fetchTable('Queue.QueueProcesses');
if ($this->activeConnection !== 'default') {
$QueueProcesses->setConnection($this->getActiveConnectionObject());
}
if ($this->request->is('post') && $this->request->getQuery('end')) {
$pid = (string)$this->request->getQuery('end');
$QueueProcesses->endProcess($pid);
return $this->redirect(['action' => 'processes']);
}
if ($this->request->is('post') && $this->request->getQuery('kill')) {
$pid = (string)$this->request->getQuery('kill');
$QueueProcesses->terminateProcess($pid);
return $this->redirect(['action' => 'processes']);
}
$processes = $QueueProcesses->getProcesses();
$terminated = $QueueProcesses->find()->where(['terminate' => true])->all()->toArray();
$key = $QueueProcesses->buildServerString();
$this->set(compact('terminated', 'processes', 'key'));
}
/**
* Mark all failed jobs as ready for re-run.
*
* @return \Cake\Http\Response|null
*/
public function reset() {
$this->request->allowMethod('post');
$resetted = $this->QueuedJobs->reset(null, (bool)$this->request->getQuery('full'));
$message = __d('queue', '{0} jobs reset for re-run', $resetted);
$this->Flash->success($message);
return $this->redirect(['action' => 'index']);
}
/**
* Remove all failed jobs.
*
* @return \Cake\Http\Response|null
*/
public function flush() {
$this->request->allowMethod('post');
$count = $this->QueuedJobs->flushFailedJobs();
$message = __d('queue', '{0} jobs removed', $count);
$this->Flash->success($message);
return $this->redirect(['action' => 'index']);
}
/**
* Truncate the queue list / table.
*
* @return \Cake\Http\Response|null
*/
public function hardReset() {
$this->request->allowMethod('post');
$this->QueuedJobs->truncate();
$message = __d('queue', 'OK');
$this->Flash->success($message);
return $this->redirect(['action' => 'index']);
}
/**
* @param array<mixed>|string $default
*
* @return \Cake\Http\Response|null
*/
protected function refererRedirect(array|string $default) {
$url = $this->request->getQuery('redirect');
if (is_array($url)) {
throw new NotFoundException('Invalid array in query string');
}
if ($url && (mb_substr((string)$url, 0, 1) !== '/' || mb_substr((string)$url, 0, 2) === '//')) {
$url = null;
}
return $this->redirect($url ?: $default);
}
}