Skip to content

Commit 5c99c04

Browse files
committed
improved commands and logged processed message
Signed-off-by: bota <[email protected]>
1 parent d19bd96 commit 5c99c04

File tree

6 files changed

+100
-49
lines changed

6 files changed

+100
-49
lines changed

config/autoload/cli.global.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515
'version' => '1.0.0',
1616
'name' => 'DotKernel CLI',
1717
'commands' => [
18-
"swoole:start" => StartCommand::class,
19-
"swoole:stop" => StopCommand::class,
20-
"messenger:start" => ConsumeMessagesCommand::class,
21-
"messenger:debug" => DebugCommand::class,
22-
"processed" => GetProcessedMessagesCommand::class,
23-
"failed" => GetFailedMessagesCommand::class,
18+
"swoole:start" => StartCommand::class,
19+
"swoole:stop" => StopCommand::class,
20+
"messenger:start" => ConsumeMessagesCommand::class,
21+
"messenger:debug" => DebugCommand::class,
22+
"processed" => GetProcessedMessagesCommand::class,
23+
"failed" => GetFailedMessagesCommand::class,
2424
],
2525
],
2626
FileLockerInterface::class => [

docs/book/v1/commands.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,20 @@ Both commands are used to extract data that can be filtered by period from the l
1111

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

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

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

1818
### TCP message
1919

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

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

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

2626
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.
27+
28+
In order to be able to test the `processed` command, by default when processing the "control" message, it is logged as successfully processed with `"levelName":"info"` simulating that the message was processed successfully. To use it run the following message:
29+
30+
`echo "control" | socat - TCP:host:port`

src/App/Message/ExampleMessageHandler.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,18 @@ public function __construct(
2626

2727
public function __invoke(ExampleMessage $message): void
2828
{
29+
$payload = $message->getPayload();
30+
2931
try {
3032
// Throwing an exception to satisfy PHPStan (replace with own code)
31-
throw new \Exception("Failed to execute");
33+
// For proof of concept and testing purposes message "control" will automatically mark it as successfully
34+
// processed and logged as info
35+
if ($payload['foo'] === 'control') {
36+
$this->logger->info($payload['foo'] . ': was processed successfully');
37+
} else {
38+
throw new \Exception("Failed to execute");
39+
}
3240
} catch (\Throwable $exception) {
33-
$payload = $message->getPayload();
3441
$this->logger->error($payload['foo'] . ' failed with message: '
3542
. $exception->getMessage() . ' after ' . ($payload['retry'] ?? 0) . ' retries');
3643
$this->retry($payload);

src/Swoole/Command/GetFailedMessagesCommand.php

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
use Symfony\Component\Console\Input\InputOption;
1212
use Symfony\Component\Console\Output\OutputInterface;
1313

14-
use function date;
14+
use function dirname;
1515
use function file;
16+
use function file_exists;
1617
use function is_numeric;
1718
use function json_decode;
18-
use function preg_match;
1919
use function strtolower;
2020
use function strtotime;
2121

@@ -46,32 +46,42 @@ protected function configure(): void
4646

4747
protected function execute(InputInterface $input, OutputInterface $output): int
4848
{
49-
$start = $input->getOption('start');
50-
$end = $input->getOption('end');
51-
$limit = $input->getOption('limit');
49+
try {
50+
$startOption = $input->getOption('start');
51+
$endOption = $input->getOption('end');
52+
$limit = $input->getOption('limit');
53+
54+
$startDate = $startOption ? new \DateTimeImmutable($startOption) : null;
55+
$endDate = $endOption ? new \DateTimeImmutable($endOption) : null;
56+
} catch (\Exception $e) {
57+
$output->writeln('<error>Invalid date format provided.</error>');
58+
return Command::FAILURE;
59+
}
5260

53-
if ($start && !preg_match('/\d{2}:\d{2}:\d{2}/', $start)) {
54-
$start .= ' 00:00:00';
61+
if ($startDate && $startDate->format('H:i:s') === '00:00:00') {
62+
$startDate = $startDate->setTime(0, 0, 0);
5563
}
5664

57-
if ($end && !preg_match('/\d{2}:\d{2}:\d{2}/', $end)) {
58-
$end .= ' 23:59:59';
65+
if ($endDate && $endDate->format('H:i:s') === '00:00:00') {
66+
$endDate = $endDate->setTime(23, 59, 59);
5967
}
6068

6169
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)));
70+
if ($startDate && ! $endDate) {
71+
$endDate = $startDate->modify("+{$limit} days");
72+
} elseif (! $startDate && $endDate) {
73+
$startDate = $endDate->modify("-{$limit} days");
6674
}
6775
}
6876

69-
if (!$end) {
70-
$end = date('Y-m-d H:i:s');
77+
if (! $endDate) {
78+
$endDate = new \DateTime();
7179
}
7280

73-
$startTimestamp = $start ? strtotime($start) : null;
74-
$endTimestamp = $end ? strtotime($end) : null;
81+
if ($startDate > $endDate) {
82+
$output->writeln('<error>The start date cannot be after the end date.</error>');
83+
return Command::FAILURE;
84+
}
7585

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

@@ -80,7 +90,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8090
return Command::FAILURE;
8191
}
8292

83-
$lines = file($logPath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
93+
$lines = file($logPath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
94+
95+
$startTimestamp = $startDate?->getTimestamp();
96+
$endTimestamp = $endDate->getTimestamp();
97+
98+
$found = false;
8499

85100
foreach ($lines as $line) {
86101
$entry = json_decode($line, true);
@@ -102,6 +117,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
102117
}
103118

104119
$output->writeln($line);
120+
$found = true;
121+
}
122+
123+
if (! $found) {
124+
$output->writeln('<comment>No matching log entries found.</comment>');
105125
}
106126

107127
return Command::SUCCESS;

src/Swoole/Command/GetProcessedMessagesCommand.php

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
use Symfony\Component\Console\Input\InputOption;
1212
use Symfony\Component\Console\Output\OutputInterface;
1313

14-
use function date;
14+
use function dirname;
1515
use function file;
16+
use function file_exists;
1617
use function is_numeric;
1718
use function json_decode;
18-
use function preg_match;
1919
use function strtolower;
2020
use function strtotime;
2121

@@ -46,32 +46,42 @@ protected function configure(): void
4646

4747
protected function execute(InputInterface $input, OutputInterface $output): int
4848
{
49-
$start = $input->getOption('start');
50-
$end = $input->getOption('end');
51-
$limit = $input->getOption('limit');
49+
try {
50+
$startOption = $input->getOption('start');
51+
$endOption = $input->getOption('end');
52+
$limit = $input->getOption('limit');
53+
54+
$startDate = $startOption ? new \DateTimeImmutable($startOption) : null;
55+
$endDate = $endOption ? new \DateTimeImmutable($endOption) : null;
56+
} catch (\Exception $e) {
57+
$output->writeln('<error>Invalid date format provided.</error>');
58+
return Command::FAILURE;
59+
}
5260

53-
if ($start && !preg_match('/\d{2}:\d{2}:\d{2}/', $start)) {
54-
$start .= ' 00:00:00';
61+
if ($startDate && $startDate->format('H:i:s') === '00:00:00') {
62+
$startDate = $startDate->setTime(0, 0, 0);
5563
}
5664

57-
if ($end && !preg_match('/\d{2}:\d{2}:\d{2}/', $end)) {
58-
$end .= ' 23:59:59';
65+
if ($endDate && $endDate->format('H:i:s') === '00:00:00') {
66+
$endDate = $endDate->setTime(23, 59, 59);
5967
}
6068

6169
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)));
70+
if ($startDate && ! $endDate) {
71+
$endDate = $startDate->modify("+{$limit} days");
72+
} elseif (! $startDate && $endDate) {
73+
$startDate = $endDate->modify("-{$limit} days");
6674
}
6775
}
6876

69-
if (!$end) {
70-
$end = date('Y-m-d H:i:s');
77+
if (! $endDate) {
78+
$endDate = new \DateTime();
7179
}
7280

73-
$startTimestamp = $start ? strtotime($start) : null;
74-
$endTimestamp = $end ? strtotime($end) : null;
81+
if ($startDate > $endDate) {
82+
$output->writeln('<error>The start date cannot be after the end date.</error>');
83+
return Command::FAILURE;
84+
}
7585

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

@@ -80,7 +90,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8090
return Command::FAILURE;
8191
}
8292

83-
$lines = file($logPath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
93+
$lines = file($logPath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
94+
95+
$startTimestamp = $startDate?->getTimestamp();
96+
$endTimestamp = $endDate->getTimestamp();
97+
98+
$found = false;
8499

85100
foreach ($lines as $line) {
86101
$entry = json_decode($line, true);
@@ -102,6 +117,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
102117
}
103118

104119
$output->writeln($line);
120+
$found = true;
121+
}
122+
123+
if (! $found) {
124+
$output->writeln('<comment>No matching log entries found.</comment>');
105125
}
106126

107127
return Command::SUCCESS;

src/Swoole/Delegators/TCPServerDelegator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public function __invoke(ContainerInterface $container, string $serviceName, cal
7777
$logger->error("Error running command: " . $e->getMessage());
7878
}
7979
} else {
80-
$bus->dispatch(new ExampleMessage(["foo" => $data]));
80+
$bus->dispatch(new ExampleMessage(["foo" => $message]));
8181
$bus->dispatch(new ExampleMessage(["foo" => "with 5 seconds delay"]), [
8282
new DelayStamp(5000),
8383
]);

0 commit comments

Comments
 (0)