Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions config/autoload/cli.global.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
"swoole:stop" => StopCommand::class,
"messenger:start" => ConsumeMessagesCommand::class,
"messenger:debug" => DebugCommand::class,
"messenger:processed" => GetProcessedMessagesCommand::class,
"messenger:failed" => GetFailedMessagesCommand::class,
"processed" => GetProcessedMessagesCommand::class,
"failed" => GetFailedMessagesCommand::class,
],
],
FileLockerInterface::class => [
Expand Down
26 changes: 26 additions & 0 deletions docs/book/v1/commands.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
## Available commands and usage

The commands available are:

1. `GetFailedMessagesCommand.php` - returns logs with messages that failed to process (levelName:error)
2. `GetProcessedMessagesCommand.php` - returns logs with messages that were successfully processed (levelName:info)

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:

### CLI

To run the commands via CLI, use the following syntax:

`php bin/cli.php failed --start="yyy-mm-dd" --end="yyy-mm-dd" --limit=int`

`php bin/cli.php processed --start="yyy-mm-dd" --end="yyy-mm-dd" --limit=int`

### TCP message

To use commands using TCP messages the following messages can be used:

`echo "failed --start=yyy-mm-dd --end=yyy-mm-dd --limit=days" | socat - TCP:host:port`

`echo "processed --start=yyy-mm-dd --end=yyy-mm-dd --limit=days" | socat - TCP:host:port`

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.
30 changes: 22 additions & 8 deletions src/Swoole/Command/GetFailedMessagesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,36 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$end = $input->getOption('end');
$limit = $input->getOption('limit');

if (! $end) {
$end = date('Y-m-d H:i:s');
} elseif (! preg_match('/\d{2}:\d{2}:\d{2}/', $end)) {
if ($start && !preg_match('/\d{2}:\d{2}:\d{2}/', $start)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we want to use some objectual code here
datetime

https://www.php.net/manual/en/class.datetime.php

$start .= ' 00:00:00';
}

if ($end && !preg_match('/\d{2}:\d{2}:\d{2}/', $end)) {
$end .= ' 23:59:59';
}

if ($limit && is_numeric($limit) && ! $start) {
$start = date('Y-m-d H:i:s', strtotime("-{$limit} days", strtotime($end)));
} elseif ($start && ! preg_match('/\d{2}:\d{2}:\d{2}/', $start)) {
$start .= ' 00:00:00';
if ($limit && is_numeric($limit)) {
if ($start && !$end) {
$end = date('Y-m-d H:i:s', strtotime("+{$limit} days", strtotime($start)));
} elseif (!$start && $end) {
$start = date('Y-m-d H:i:s', strtotime("-{$limit} days", strtotime($end)));
}
}

if (!$end) {
$end = date('Y-m-d H:i:s');
}

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

$logPath = 'log/queue-log.log';
$logPath = dirname(__DIR__, 3) . '/log/queue-log.log';

if (! file_exists($logPath)) {
$output->writeln("<error>Log file not found: $logPath</error>");
return Command::FAILURE;
}

$lines = file($logPath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

foreach ($lines as $line) {
Expand Down
30 changes: 22 additions & 8 deletions src/Swoole/Command/GetProcessedMessagesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,36 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$end = $input->getOption('end');
$limit = $input->getOption('limit');

if (! $end) {
$end = date('Y-m-d H:i:s');
} elseif (! preg_match('/\d{2}:\d{2}:\d{2}/', $end)) {
if ($start && !preg_match('/\d{2}:\d{2}:\d{2}/', $start)) {
$start .= ' 00:00:00';
}

if ($end && !preg_match('/\d{2}:\d{2}:\d{2}/', $end)) {
$end .= ' 23:59:59';
}

if ($limit && is_numeric($limit) && ! $start) {
$start = date('Y-m-d H:i:s', strtotime("-{$limit} days", strtotime($end)));
} elseif ($start && ! preg_match('/\d{2}:\d{2}:\d{2}/', $start)) {
$start .= ' 00:00:00';
if ($limit && is_numeric($limit)) {
if ($start && !$end) {
$end = date('Y-m-d H:i:s', strtotime("+{$limit} days", strtotime($start)));
} elseif (!$start && $end) {
$start = date('Y-m-d H:i:s', strtotime("-{$limit} days", strtotime($end)));
}
}

if (!$end) {
$end = date('Y-m-d H:i:s');
}

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

$logPath = 'log/queue-log.log';
$logPath = dirname(__DIR__, 3) . '/log/queue-log.log';

if (! file_exists($logPath)) {
$output->writeln("<error>Log file not found: $logPath</error>");
return Command::FAILURE;
}

$lines = file($logPath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

foreach ($lines as $line) {
Expand Down
Loading