forked from dereuromark/cakephp-queue
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueueController.php
More file actions
273 lines (225 loc) · 6.76 KB
/
QueueController.php
File metadata and controls
273 lines (225 loc) · 6.76 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
<?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;
use Queue\Queue\TaskMetadata;
/**
* @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();
$pendingDetails = $this->QueuedJobs->getPendingStats()->toArray();
$new = 0;
foreach ($pendingDetails as $pendingDetail) {
if ($pendingDetail['fetched'] || $pendingDetail['attempts']) {
continue;
}
$new++;
}
$scheduledDetails = $this->QueuedJobs->getScheduledStats()->toArray();
$data = $this->QueuedJobs->getStats();
$taskFinder = new TaskFinder();
$tasks = $taskFinder->all();
$addableTasks = $taskFinder->allAddable(AddFromBackendInterface::class);
$taskDescriptions = [];
foreach ($tasks as $task => $className) {
$taskDescriptions[$task] = TaskMetadata::fromClass($className)->description;
}
$servers = $QueueProcesses->serverList();
$workers = $status ? $status['workers'] : 0;
$scheduledJobs = 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();
// Pending = total pending minus running and failed (to avoid double counting)
$pendingJobs = max(0, count($pendingDetails) - $runningJobs - $failedJobs);
$configurations = (array)Configure::read('Queue');
$this->set(compact(
'new',
'current',
'data',
'pendingDetails',
'scheduledDetails',
'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 . '`');
}
if (is_subclass_of($className, AddInterface::class)) {
$object = new $className();
$object->add(null);
} else {
$this->QueuedJobs->createJob($job);
}
$this->Flash->success('Job ' . $job . ' added');
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($url, 0, 1) !== '/' || mb_substr($url, 0, 2) === '//')) {
$url = null;
}
return $this->redirect($url ?: $default);
}
}