-
Notifications
You must be signed in to change notification settings - Fork 139
Expand file tree
/
Copy pathInfoCommand.php
More file actions
166 lines (135 loc) · 4.21 KB
/
Copy pathInfoCommand.php
File metadata and controls
166 lines (135 loc) · 4.21 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
<?php
declare(strict_types=1);
namespace Queue\Command;
use Cake\Command\Command;
use Cake\Console\Arguments;
use Cake\Console\ConsoleIo;
use Cake\Console\ConsoleOptionParser;
use Cake\Core\Configure;
use Cake\I18n\Number;
use Queue\Queue\TaskFinder;
/**
* @property \Queue\Model\Table\QueuedJobsTable $QueuedJobs
*/
class InfoCommand extends Command {
/**
* @return string
*/
public static function getDescription(): string {
return 'Show queue status and available tasks.';
}
/**
* @var string|null
*/
protected ?string $defaultTable = 'Queue.QueuedJobs';
/**
* @inheritDoc
*/
public static function defaultName(): string {
return 'queue info';
}
/**
* @return \Cake\Console\ConsoleOptionParser
*/
public function getOptionParser(): ConsoleOptionParser {
$parser = parent::getOptionParser();
$parser->setDescription(
'Get list of available tasks as well as current settings and statistics.',
);
return $parser;
}
/**
* @param \Cake\Console\Arguments $args Arguments
* @param \Cake\Console\ConsoleIo $io ConsoleIo
*
* @return int|null|void
*/
public function execute(Arguments $args, ConsoleIo $io) {
$tasks = $this->getTasks();
$addableTasks = $this->getAddableTasks();
$io->out(count($tasks) . ' tasks available:');
foreach (array_keys($tasks) as $task) {
if (array_key_exists($task, $addableTasks)) {
$task .= ' <info>[addable via CLI]</info>';
}
$io->out(' * ' . $task);
}
$io->out();
$io->hr();
$io->out();
$io->out('Current Settings:');
$conf = (array)Configure::read('Queue');
// Map of old config names to new names for display
$configMapping = [
'defaultworkertimeout' => 'defaultRequeueTimeout',
'workermaxruntime' => 'workerLifetime',
'defaultworkerretries' => 'defaultJobRetries',
'workertimeout' => 'workerPhpTimeout',
];
foreach ($conf as $key => $val) {
if ($val === false) {
$val = 'no';
}
if ($val === true) {
$val = 'yes';
}
// Display new config name if it exists, otherwise use the original
$displayKey = $key;
if (isset($configMapping[$key])) {
$displayKey = $configMapping[$key] . ' (was: ' . $key . ')';
}
$io->out('* ' . $displayKey . ': ' . print_r($val, true));
}
$io->out();
$io->hr();
$io->out();
/** @var \Queue\Model\Table\QueuedJobsTable $QueuedJobs */
$QueuedJobs = $this->fetchTable('Queue.QueuedJobs');
$QueueProcesses = $this->fetchTable('Queue.QueueProcesses');
$io->out('Total unfinished jobs: ' . $QueuedJobs->getLength());
$status = $QueueProcesses->status();
$io->out('Current running workers: ' . ($status ? $status['workers'] : '-'));
$io->out('Last run: ' . ($status ? $status['time']->nice() : '-'));
$io->out('Server name: ' . $QueueProcesses->buildServerString());
$io->out();
$io->hr();
$io->out();
$io->out('Jobs currently in the queue:');
/** @var array<string, string> $types */
$types = $QueuedJobs->getTypes()->toArray();
//TODO: refactor using $io->helper table?
foreach ($types as $type) {
$io->out(' - ' . str_pad($type, 20, ' ', STR_PAD_RIGHT) . ': ' . $QueuedJobs->getLength($type));
}
$io->out();
$scheduled = $QueuedJobs->getScheduledStats()->count();
$io->out('Jobs currently scheduled to run in the future: ' . $scheduled);
$io->out();
$io->hr();
$io->out();
$io->out('Finished job statistics:');
$data = $QueuedJobs->getStats();
//TODO: refactor using $io->helper table?
foreach ($data as $item) {
$io->out(' - ' . $item['job_task'] . ': ');
$io->out(' - Finished Jobs in Database: ' . $item['num']);
$io->out(' - Average Job existence : ' . str_pad(Number::precision($item['alltime'], 0), 8, ' ', STR_PAD_LEFT) . 's');
$io->out(' - Average Execution delay : ' . str_pad(Number::precision($item['fetchdelay'], 0), 8, ' ', STR_PAD_LEFT) . 's');
$io->out(' - Average Execution time : ' . str_pad(Number::precision($item['runtime'], 0), 8, ' ', STR_PAD_LEFT) . 's');
}
}
/**
* @return array<string>
*/
protected function getTasks(): array {
$taskFinder = new TaskFinder();
return $taskFinder->all();
}
/**
* @return array<string>
*/
protected function getAddableTasks(): array {
$taskFinder = new TaskFinder();
return $taskFinder->allAddable();
}
}