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
98 lines (84 loc) · 2.08 KB
/
Copy pathQueueController.php
File metadata and controls
98 lines (84 loc) · 2.08 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
<?php
App::uses('QueueAppController', 'Queue.Controller');
class QueueController extends QueueAppController {
public $uses = ['Queue.QueuedTask'];
/**
* QueueController::beforeFilter()
*
* @return void
*/
public function beforeFilter() {
$this->QueuedTask->initConfig();
parent::beforeFilter();
}
/**
* Admin center.
* Manage queues from admin backend (without the need to open ssh console window).
*
* @return void
*/
public function admin_index() {
$status = $this->_status();
$current = $this->QueuedTask->getLength();
$pendingDetails = $this->QueuedTask->getPendingStats();
$data = $this->QueuedTask->getStats();
$this->set(compact('current', 'data', 'pendingDetails', 'status'));
$this->helpers[] = 'Tools.Format';
$this->helpers[] = 'Tools.Datetime';
}
/**
* Truncate the queue list / table.
*
* @return void
* @throws MethodNotAllowedException when not posted
*/
public function admin_reset() {
$this->request->allowMethod('post');
$res = $this->QueuedTask->truncate();
if ($res) {
$message = __d('queue', 'OK');
$class = 'success';
} else {
$message = __d('queue', 'Error');
$class = 'error';
}
if (isset($this->Flash)) {
$this->Flash->$class($message);
} else {
$this->Session->setFlash($message, 'default', ['class' => $class]);
}
return $this->redirect(['action' => 'index']);
}
/**
* QueueController::_status()
*
* If pid loggin is enabled, will return an array with
* - time: int Timestamp
* - workers: int Count of currently running workers
*
* @return array Status array
*/
protected function _status() {
if (!($pidFilePath = Configure::read('Queue.pidfilepath'))) {
return [];
}
$file = $pidFilePath . 'queue.pid';
if (!file_exists($file)) {
return [];
}
$sleepTime = Configure::read('Queue.sleeptime');
$thresholdTime = time() - $sleepTime;
$count = 0;
foreach (glob($pidFilePath . 'queue_*.pid') as $filename) {
$time = filemtime($filename);
if ($time >= $thresholdTime) {
$count++;
}
}
$res = [
'time' => filemtime($file),
'workers' => $count,
];
return $res;
}
}