Skip to content

Commit ff5b947

Browse files
Merge pull request #271 from haristku/fix/269-fix-monitor-windows-error
disable monitor interactive paging on windows #269
2 parents 45214e2 + 06b1286 commit ff5b947

1 file changed

Lines changed: 87 additions & 50 deletions

File tree

Commands/Monitor.php

Lines changed: 87 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,14 @@ class Monitor extends ConsoleCommand
1919
protected function configure()
2020
{
2121
$this->setName('queuedtracking:monitor');
22-
$this->setDescription("Shows and updates the current state of the queue every 2 seconds.\n Key ,=first page, .=last page, 0-9=move to page section, arrow LEFT=prev page, RIGHT=next page, UP=next 10 pages, DOWN=prev 10 pages, q=quit");
22+
if ($this->interactiveCapability()) {
23+
$this->setDescription("Shows and updates the current state of the queue every 2 seconds.\n Key ,=first page, .=last page, 0-9=move to page section, arrow LEFT=prev page, RIGHT=next page, UP=next 10 pages, DOWN=prev 10 pages, q=quit");
24+
} else {
25+
$this->setDescription("Shows and updates the current state of the queue every 2 seconds.");
26+
}
2327
$this->addRequiredValueOption('iterations', null, 'If set, will limit the number of monitoring iterations done.');
24-
$this->addRequiredValueOption('perpage', 'p', 'Number of queue worker displayed per page.', 16);
28+
$this->addRequiredValueOption('rowperpage', 'r', 'Number of queue worker displayed per page.', 16);
29+
$this->addRequiredValueOption('jumptopage', 'p', 'Jump to page (p).', 1);
2530
}
2631

2732
/**
@@ -66,14 +71,16 @@ protected function doExecute(): int
6671
));
6772
$iterationCount = 0;
6873

69-
$qCurrentPage = 1;
74+
$qCurrentPage = $this->getJumpToPageFromArg();
7075
$qCount = count($queues);
7176
$qPerPAge = min(max($this->getPerPageFromArg(), 1), $qCount);
7277
$qPageCount = ceil($qCount / $qPerPAge);
7378

74-
readline_callback_handler_install('', function () {
75-
});
76-
stream_set_blocking(STDIN, false);
79+
if ($this->interactiveCapability()) {
80+
readline_callback_handler_install('', function () {
81+
});
82+
stream_set_blocking(STDIN, false);
83+
}
7784

7885
$output->writeln(str_repeat("-", 30));
7986
$output->writeln("<fg=black;bg=white;options=bold>" . str_pad(" Q INDEX", 10) . str_pad(" | REQUEST SETS", 20) . "</>");
@@ -88,7 +95,7 @@ protected function doExecute(): int
8895

8996
while (1) {
9097
if (microtime(true) - $lastStatsTimer >= 2 || $keyPressed != "") {
91-
$output->write("\e[" . ($qPerPAge + 5) . "A");
98+
$output->write("\e[" . ($qPerPAge + 5) . "A\e[0G");
9299

93100
$qCurrentPage = min(max($qCurrentPage, 1), $qPageCount);
94101
$memory = $backend->getMemoryStats(); // I know this will only work with redis currently as it is not defined in backend interface etc. needs to be refactored once we add another backend
@@ -120,7 +127,7 @@ protected function doExecute(): int
120127
$output->writeln("<fg=black;bg=white;options=bold>" . str_pad(" " . ($qCount) . " Q", 10) . " | " . str_pad(number_format($sumInQueue) . " R", 16) . "</>");
121128
$output->writeln(str_repeat("-", 30));
122129
$output->writeln(sprintf(
123-
"Q [%s-%s] | <info>page %s/%s</> | <comment>press (0-9.,q) or arrow(L,R,U,D)</> | diff/sec %s \n" .
130+
"Q [%s-%s] | <info>page %s/%s</>" . ($this->interactiveCapability() ? " | <comment>press (0-9.,q) or arrow(L,R,U,D)</>" : " | <error>use -p arg to jump to specific page</>") . " | diff/sec %s \n" .
124131
"%s used memory (%s peak). <info>%d</> workers active." . str_repeat(" ", 15),
125132
($idx - $qPerPAge + 1),
126133
$idx,
@@ -143,44 +150,46 @@ protected function doExecute(): int
143150
$lastStatsTimer = microtime(true);
144151
}
145152

146-
$keyStroke = stream_get_contents(STDIN, 3);
147-
$keyPressed = strlen($keyStroke) == 3 ? $keyStroke[2] : (strlen($keyStroke) > 0 ? $keyStroke[0] : "");
148-
if ($keyPressed != "" and in_array($keyPressed, array(".", ",", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "q"))) {
149-
switch ($keyPressed) {
150-
case "0":
151-
case "1":
152-
case "2":
153-
case "3":
154-
case "4":
155-
case "5":
156-
case "6":
157-
case "7":
158-
case "8":
159-
case "9":
160-
$keyPressed = $keyPressed != "0" ? $keyPressed : "10";
161-
$qCurrentPage = floor(($qCurrentPage - 0.1) / 10) * 10 + (int)$keyPressed;
162-
break;
163-
case "C":
164-
$qCurrentPage++;
165-
break;
166-
case "D":
167-
$qCurrentPage--;
168-
break;
169-
case "A":
170-
$qCurrentPage += 10;
171-
break;
172-
case "B":
173-
$qCurrentPage -= 10;
174-
break;
175-
case ",":
176-
$qCurrentPage = 1;
177-
break;
178-
case ".":
179-
$qCurrentPage = $qPageCount;
180-
break;
181-
case "q":
182-
$output->writeln('');
183-
die;
153+
if ($this->interactiveCapability()) {
154+
$keyStroke = stream_get_contents(STDIN, 3);
155+
$keyPressed = strlen($keyStroke) == 3 ? $keyStroke[2] : (strlen($keyStroke) > 0 ? $keyStroke[0] : "");
156+
if ($keyPressed != "" and in_array($keyPressed, array(".", ",", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "q"))) {
157+
switch ($keyPressed) {
158+
case "0":
159+
case "1":
160+
case "2":
161+
case "3":
162+
case "4":
163+
case "5":
164+
case "6":
165+
case "7":
166+
case "8":
167+
case "9":
168+
$keyPressed = $keyPressed != "0" ? $keyPressed : "10";
169+
$qCurrentPage = floor(($qCurrentPage - 0.1) / 10) * 10 + (int)$keyPressed;
170+
break;
171+
case "C":
172+
$qCurrentPage++;
173+
break;
174+
case "D":
175+
$qCurrentPage--;
176+
break;
177+
case "A":
178+
$qCurrentPage += 10;
179+
break;
180+
case "B":
181+
$qCurrentPage -= 10;
182+
break;
183+
case ",":
184+
$qCurrentPage = 1;
185+
break;
186+
case ".":
187+
$qCurrentPage = $qPageCount;
188+
break;
189+
case "q":
190+
$output->writeln('');
191+
die;
192+
}
184193
}
185194
}
186195

@@ -212,21 +221,49 @@ private function getIterationsFromArg()
212221
}
213222

214223
/**
215-
* Loads the `perpage` argument from the commands arguments.
224+
* Loads the `rowperpage` argument from the commands arguments.
216225
*
217226
* @return int|null
218227
*/
219228
private function getPerPageFromArg()
220229
{
221-
$perPage = $this->getInput()->getOption('perpage');
230+
$perPage = $this->getInput()->getOption('rowperpage');
222231
if (!is_numeric($perPage)) {
223-
throw new \Exception('perpage needs to be numeric');
232+
throw new \Exception('rowperpage needs to be numeric');
224233
} else {
225234
$perPage = (int)$perPage;
226235
if ($perPage <= 0) {
227-
throw new \Exception('perpage needs to be a non-zero positive number');
236+
throw new \Exception('rowperpage needs to be a non-zero positive number');
228237
}
229238
}
230239
return $perPage;
231240
}
241+
242+
/**
243+
* Loads the `jumptopage` argument from the commands arguments.
244+
*
245+
* @return int|null
246+
*/
247+
private function getJumpToPageFromArg()
248+
{
249+
$perPage = $this->getInput()->getOption('jumptopage');
250+
if (!is_numeric($perPage)) {
251+
throw new \Exception('jumptopage needs to be numeric');
252+
} else {
253+
$perPage = (int)$perPage;
254+
if ($perPage <= 0) {
255+
throw new \Exception('jumptopage needs to be a non-zero positive number');
256+
}
257+
}
258+
return $perPage;
259+
}
260+
261+
/**
262+
*
263+
* @return bool
264+
*/
265+
private function interactiveCapability()
266+
{
267+
return function_exists('readline_callback_handler_install');
268+
}
232269
}

0 commit comments

Comments
 (0)