Skip to content

Commit 853abfb

Browse files
committed
fixed limit bug and added commands usage to docs
Signed-off-by: bota <[email protected]>
1 parent 9d2f9fc commit 853abfb

File tree

4 files changed

+72
-18
lines changed

4 files changed

+72
-18
lines changed

config/autoload/cli.global.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
"swoole:stop" => StopCommand::class,
2020
"messenger:start" => ConsumeMessagesCommand::class,
2121
"messenger:debug" => DebugCommand::class,
22-
"messenger:processed" => GetProcessedMessagesCommand::class,
23-
"messenger:failed" => GetFailedMessagesCommand::class,
22+
"processed" => GetProcessedMessagesCommand::class,
23+
"failed" => GetFailedMessagesCommand::class,
2424
],
2525
],
2626
FileLockerInterface::class => [

docs/book/v1/commands.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
## Available commands and usage
2+
3+
The commands available are:
4+
5+
1. `GetFailedMessagesCommand.php` - returns logs with messages that failed to process (levelName:error)
6+
2. `GetProcessedMessagesCommand.php` - returns logs with messages that were successfully processed (levelName:info)
7+
8+
Both commands are used to extract data that can be filtered by period from the log file. The commands can be run in two different ways:
9+
10+
### CLI
11+
12+
To run the commands via CLI, use the following syntax:
13+
14+
`php bin/cli.php failed --start="yyy-mm-dd" --end="yyy-mm-dd" --limit=int`
15+
16+
`php bin/cli.php processed --start="yyy-mm-dd" --end="yyy-mm-dd" --limit=int`
17+
18+
### TCP message
19+
20+
To use commands using TCP messages the following messages can be used:
21+
22+
`echo "failed --start=yyy-mm-dd --end=yyy-mm-dd --limit=days" | socat - TCP:host:port`
23+
24+
`echo "processed --start=yyy-mm-dd --end=yyy-mm-dd --limit=days" | socat - TCP:host:port`
25+
26+
In both cases the flags are optional. Keep in mind if both `start` and `end` are set, `limit` will not be applied, it's only used when one of `start` or `end` is missing.

src/Swoole/Command/GetFailedMessagesCommand.php

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,36 @@ protected function execute(InputInterface $input, OutputInterface $output): int
5050
$end = $input->getOption('end');
5151
$limit = $input->getOption('limit');
5252

53-
if (! $end) {
54-
$end = date('Y-m-d H:i:s');
55-
} elseif (! preg_match('/\d{2}:\d{2}:\d{2}/', $end)) {
53+
if ($start && !preg_match('/\d{2}:\d{2}:\d{2}/', $start)) {
54+
$start .= ' 00:00:00';
55+
}
56+
57+
if ($end && !preg_match('/\d{2}:\d{2}:\d{2}/', $end)) {
5658
$end .= ' 23:59:59';
5759
}
5860

59-
if ($limit && is_numeric($limit) && ! $start) {
60-
$start = date('Y-m-d H:i:s', strtotime("-{$limit} days", strtotime($end)));
61-
} elseif ($start && ! preg_match('/\d{2}:\d{2}:\d{2}/', $start)) {
62-
$start .= ' 00:00:00';
61+
if ($limit && is_numeric($limit)) {
62+
if ($start && !$end) {
63+
$end = date('Y-m-d H:i:s', strtotime("+{$limit} days", strtotime($start)));
64+
} elseif (!$start && $end) {
65+
$start = date('Y-m-d H:i:s', strtotime("-{$limit} days", strtotime($end)));
66+
}
67+
}
68+
69+
if (!$end) {
70+
$end = date('Y-m-d H:i:s');
6371
}
6472

6573
$startTimestamp = $start ? strtotime($start) : null;
6674
$endTimestamp = $end ? strtotime($end) : null;
6775

68-
$logPath = 'log/queue-log.log';
76+
$logPath = dirname(__DIR__, 3) . '/log/queue-log.log';
77+
78+
if (! file_exists($logPath)) {
79+
$output->writeln("<error>Log file not found: $logPath</error>");
80+
return Command::FAILURE;
81+
}
82+
6983
$lines = file($logPath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
7084

7185
foreach ($lines as $line) {

src/Swoole/Command/GetProcessedMessagesCommand.php

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,36 @@ protected function execute(InputInterface $input, OutputInterface $output): int
5050
$end = $input->getOption('end');
5151
$limit = $input->getOption('limit');
5252

53-
if (! $end) {
54-
$end = date('Y-m-d H:i:s');
55-
} elseif (! preg_match('/\d{2}:\d{2}:\d{2}/', $end)) {
53+
if ($start && !preg_match('/\d{2}:\d{2}:\d{2}/', $start)) {
54+
$start .= ' 00:00:00';
55+
}
56+
57+
if ($end && !preg_match('/\d{2}:\d{2}:\d{2}/', $end)) {
5658
$end .= ' 23:59:59';
5759
}
5860

59-
if ($limit && is_numeric($limit) && ! $start) {
60-
$start = date('Y-m-d H:i:s', strtotime("-{$limit} days", strtotime($end)));
61-
} elseif ($start && ! preg_match('/\d{2}:\d{2}:\d{2}/', $start)) {
62-
$start .= ' 00:00:00';
61+
if ($limit && is_numeric($limit)) {
62+
if ($start && !$end) {
63+
$end = date('Y-m-d H:i:s', strtotime("+{$limit} days", strtotime($start)));
64+
} elseif (!$start && $end) {
65+
$start = date('Y-m-d H:i:s', strtotime("-{$limit} days", strtotime($end)));
66+
}
67+
}
68+
69+
if (!$end) {
70+
$end = date('Y-m-d H:i:s');
6371
}
6472

6573
$startTimestamp = $start ? strtotime($start) : null;
6674
$endTimestamp = $end ? strtotime($end) : null;
6775

68-
$logPath = 'log/queue-log.log';
76+
$logPath = dirname(__DIR__, 3) . '/log/queue-log.log';
77+
78+
if (! file_exists($logPath)) {
79+
$output->writeln("<error>Log file not found: $logPath</error>");
80+
return Command::FAILURE;
81+
}
82+
6983
$lines = file($logPath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
7084

7185
foreach ($lines as $line) {

0 commit comments

Comments
 (0)