Skip to content

Commit

Permalink
Merge pull request #35 from driftphp/feature/improved-console-messages
Browse files Browse the repository at this point in the history
Added improved messages
  • Loading branch information
mmoreram authored Apr 3, 2023
2 parents fe03f80 + 7e67378 commit 85e94dc
Show file tree
Hide file tree
Showing 13 changed files with 110 additions and 90 deletions.
16 changes: 8 additions & 8 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
version: 2
jobs:
test-php74:
test-php80:
docker:
- image: circleci/php:7.4-cli
- image: circleci/php:8.0-cli
- image: redis
- image: rabbitmq
- image: postgres:alpine
Expand All @@ -16,14 +16,14 @@ jobs:
- checkout

- run:
name: Run tests / Symfony 4^4
name: Run tests / Symfony 5^0
command: |
composer update -n --prefer-dist --prefer-lowest --no-suggest
sh .circleci/start.sh
composer update -n --prefer-dist --no-suggest
sh .circleci/start_base_testsuite.sh
test-php80:
test-php81:
docker:
- image: circleci/php:8.0-cli
- image: cimg/php:8.1
- image: redis
- image: rabbitmq
- image: postgres:alpine
Expand All @@ -46,5 +46,5 @@ workflows:
version: 2
test:
jobs:
- test-php74
- test-php80
- test-php81
2 changes: 1 addition & 1 deletion Async/AMQPAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ function () use (&$forced) {
});

EventLoopUtils::runLoop($this->loop, 2, function ($iterationsMissing) use ($outputPrinter) {
(new CommandBusHeaderMessage('', 'EventLoop stopped. This consumer will run it '.$iterationsMissing.' more times.'))->print($outputPrinter);
(new CommandBusHeaderMessage('EventLoop stopped. This consumer will run it '.$iterationsMissing.' more times.'))->print($outputPrinter);
}, $forced);
}
}
22 changes: 1 addition & 21 deletions Async/PostgreSQLAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ function () use (&$forced) {
});

EventLoopUtils::runLoop($this->loop, 2, function ($iterationsMissing) use ($outputPrinter) {
(new CommandBusHeaderMessage('', 'EventLoop stopped. This consumer will run it '.$iterationsMissing.' more times.'))->print($outputPrinter);
(new CommandBusHeaderMessage('EventLoop stopped. This consumer will run it '.$iterationsMissing.' more times.'))->print($outputPrinter);
}, $forced);
}

Expand Down Expand Up @@ -339,23 +339,3 @@ private function getAndDeleteKeyFromQueue(): PromiseInterface
->toPromise();
}
}

/*
*
*
DROP TABLE IF EXISTS commands;
CREATE TABLE commands (id VARCHAR, added_at TIMESTAMP, payload TEXT);
SELECT * FROM commands;
CREATE OR REPLACE FUNCTION notify_commands() RETURNS TRIGGER AS $$
BEGIN
PERFORM pg_notify('commands', '1');
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
DROP TRIGGER IF EXISTS notify_trigger ON commands;
CREATE TRIGGER notify_trigger
AFTER INSERT
ON commands
FOR EACH ROW EXECUTE PROCEDURE notify_commands();
*/
19 changes: 5 additions & 14 deletions Console/CommandBusHeaderMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,17 @@
/**
* Class ConsumerHeaderMessage.
*/
final class CommandBusHeaderMessage
final class CommandBusHeaderMessage extends CommandMessage
{
private $elapsedTime;
private $message;
private string $message;

/**
* ConsumerMessage constructor.
*
* @param string $elapsedTime
* @param string $message
*/
public function __construct(
string $elapsedTime,
string $message
) {
$this->elapsedTime = $elapsedTime;
public function __construct(string $message)
{
$this->message = $message;
}

Expand All @@ -46,11 +41,7 @@ public function __construct(
*/
public function print(OutputPrinter $outputPrinter)
{
$color = '32';

$outputPrinter->print("\033[01;{$color}mBUS\033[0m ");
$outputPrinter->print("(\e[00;37m".$this->elapsedTime.' | '.((int) (memory_get_usage() / 1000000))." MB\e[0m)");
$outputPrinter->print(" {$this->message}");
$outputPrinter->print("\033[01;32mBUS\033[0m {$this->message}");
$outputPrinter->printLine();
}
}
19 changes: 9 additions & 10 deletions Console/CommandConsumedLineMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,26 @@
/**
* Class ConsumerLineMessage.
*/
final class CommandConsumedLineMessage
final class CommandConsumedLineMessage extends CommandMessage
{
/**
* @var string
*/
const CONSUMED = 'Consumed';
const CONSUMED = '[CON]';

/**
* @var string
*/
const IGNORED = 'Ignored ';
const IGNORED = '[IGN]';

/**
* @var string
*/
const REJECTED = 'Rejected';
const REJECTED = '[REJ]';

private $class;
private $elapsedTime;
private $status;
private string $class;
private string $elapsedTime;
private string $status;

/**
* ConsumerMessage constructor.
Expand Down Expand Up @@ -72,9 +72,8 @@ public function print(OutputPrinter $outputPrinter)
$color = '31';
}

$outputPrinter->print("\033[01;{$color}m{$this->status}\033[0m");
$outputPrinter->print(" {$this->class} ");
$outputPrinter->print("(\e[00;37m".$this->elapsedTime.' | '.((int) (memory_get_usage() / 1000000))." MB\e[0m)");
$performance = $this->styledPerformance($this->elapsedTime);
$outputPrinter->print("\033[01;32mBUS\033[0m $performance \033[01;{$color}m{$this->status}\033[0m {$this->class}");
$outputPrinter->printLine();
}

Expand Down
11 changes: 7 additions & 4 deletions Console/CommandConsumerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Drift\EventBus\Bus\EventBus;
use Drift\EventBus\Subscriber\EventBusSubscriber;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
Expand Down Expand Up @@ -97,19 +98,21 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$outputFormatter = $output->getFormatter();
$outputFormatter->setStyle('performance', new OutputFormatterStyle('gray'));
$outputPrinter = new OutputPrinter($output, false, false);
$adapterName = $this->asyncAdapter->getName();
(new CommandBusHeaderMessage('', 'Consumer built'))->print($outputPrinter);
(new CommandBusHeaderMessage('', 'Using adapter '.$adapterName))->print($outputPrinter);
(new CommandBusHeaderMessage('', 'Started listening...'))->print($outputPrinter);
(new CommandBusHeaderMessage('Consumer built'))->print($outputPrinter);
(new CommandBusHeaderMessage('Using adapter '.$adapterName))->print($outputPrinter);
(new CommandBusHeaderMessage('Started listening...'))->print($outputPrinter);

$exchanges = self::buildQueueArray($input);
if (
class_exists(EventBusSubscriber::class) &&
!empty($exchanges) &&
!is_null($this->eventBusSubscriber)
) {
(new CommandBusHeaderMessage('', 'Kernel connected to exchanges.'))->print($outputPrinter);
(new CommandBusHeaderMessage('Kernel connected to exchanges.'))->print($outputPrinter);
$this
->eventBusSubscriber
->subscribeToExchanges(
Expand Down
33 changes: 33 additions & 0 deletions Console/CommandMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Drift\CommandBus\Console;

abstract class CommandMessage
{
/**
* @param string $elapsedType
*
* @return string
*/
protected function styledPerformance(string $elapsedType = null): string
{
$info = array_filter([
$elapsedType ? $this->toLength($elapsedType, str_contains($elapsedType, 'μ') ? 8 : 7) : null,
$this->toLength((string) (int) (memory_get_usage() / 1048576), 4),
$this->toLength((string) (int) (memory_get_usage(true) / 1048576), 4),
]);

return '<performance>['.implode('|', $info).']</performance>';
}

/**
* @param string $string
* @param int $length
*
* @return string
*/
protected function toLength(string $string, int $length): string
{
return str_pad($string, $length, ' ', STR_PAD_LEFT);
}
}
3 changes: 3 additions & 0 deletions Console/DebugCommandBusCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Drift\CommandBus\Middleware\DiscriminableMiddleware;
use Drift\CommandBus\Middleware\HandlerMiddleware;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

Expand Down Expand Up @@ -85,6 +86,8 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$outputFormatter = $output->getFormatter();
$outputFormatter->setStyle('performance', new OutputFormatterStyle('gray'));
$output->writeln('');
$this->printBus('Query', $this->queryBus, $output);
$this->printBus('Command', $this->commandBus, $output);
Expand Down
11 changes: 8 additions & 3 deletions Console/InfrastructureCheckCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@
use Clue\React\Block;
use Drift\CommandBus\Async\AsyncAdapter;
use Drift\Console\OutputPrinter;
use Drift\Server\Console\Style\Muted;
use Drift\Server\Console\Style\Purple;
use React\EventLoop\LoopInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

Expand Down Expand Up @@ -74,17 +77,19 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$outputFormatter = $output->getFormatter();
$outputFormatter->setStyle('performance', new OutputFormatterStyle('gray'));
$outputPrinter = new OutputPrinter($output, false, false);
$adapterName = $this->asyncAdapter->getName();
(new CommandBusHeaderMessage('', 'Started checking infrastructure...'))->print($outputPrinter);
(new CommandBusHeaderMessage('', 'Using adapter '.$adapterName))->print($outputPrinter);
(new CommandBusHeaderMessage('Started checking infrastructure...'))->print($outputPrinter);
(new CommandBusHeaderMessage('Using adapter '.$adapterName))->print($outputPrinter);

$promise = $this
->asyncAdapter
->checkInfrastructure($outputPrinter);

Block\await($promise, $this->loop);
(new CommandBusHeaderMessage('', 'Infrastructure checked'))->print($outputPrinter);
(new CommandBusHeaderMessage('Infrastructure checked'))->print($outputPrinter);

return 0;
}
Expand Down
11 changes: 7 additions & 4 deletions Console/InfrastructureCreateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Drift\Console\OutputPrinter;
use React\EventLoop\LoopInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
Expand Down Expand Up @@ -81,23 +82,25 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$outputFormatter = $output->getFormatter();
$outputFormatter->setStyle('performance', new OutputFormatterStyle('gray'));
$outputPrinter = new OutputPrinter($output, false, false);
if (!$input->getOption('force')) {
(new CommandBusHeaderMessage('', 'Please, use the flag --force'))->print($outputPrinter);
(new CommandBusHeaderMessage('Please, use the flag --force'))->print($outputPrinter);

return 1;
}

$adapterName = $this->asyncAdapter->getName();
(new CommandBusHeaderMessage('', 'Started building infrastructure...'))->print($outputPrinter);
(new CommandBusHeaderMessage('', 'Using adapter '.$adapterName))->print($outputPrinter);
(new CommandBusHeaderMessage('Started building infrastructure...'))->print($outputPrinter);
(new CommandBusHeaderMessage('Using adapter '.$adapterName))->print($outputPrinter);

$promise = $this
->asyncAdapter
->createInfrastructure($outputPrinter);

Block\await($promise, $this->loop);
(new CommandBusHeaderMessage('', 'Infrastructure built'))->print($outputPrinter);
(new CommandBusHeaderMessage('Infrastructure built'))->print($outputPrinter);

return 0;
}
Expand Down
11 changes: 7 additions & 4 deletions Console/InfrastructureDropCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Drift\Console\OutputPrinter;
use React\EventLoop\LoopInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
Expand Down Expand Up @@ -82,22 +83,24 @@ protected function configure()
protected function execute(InputInterface $input, OutputInterface $output)
{
$outputPrinter = new OutputPrinter($output, false, false);
$outputFormatter = $output->getFormatter();
$outputFormatter->setStyle('performance', new OutputFormatterStyle('gray'));
if (!$input->getOption('force')) {
(new CommandBusHeaderMessage('', 'Please, use the flag --force'))->print($outputPrinter);
(new CommandBusHeaderMessage('Please, use the flag --force'))->print($outputPrinter);

return 1;
}

$adapterName = $this->asyncAdapter->getName();
(new CommandBusHeaderMessage('', 'Started dropping infrastructure...'))->print($outputPrinter);
(new CommandBusHeaderMessage('', 'Using adapter '.$adapterName))->print($outputPrinter);
(new CommandBusHeaderMessage('Started dropping infrastructure...'))->print($outputPrinter);
(new CommandBusHeaderMessage('Using adapter '.$adapterName))->print($outputPrinter);

$promise = $this
->asyncAdapter
->dropInfrastructure($outputPrinter);

Block\await($promise, $this->loop);
(new CommandBusHeaderMessage('', 'Infrastructure dropped'))->print($outputPrinter);
(new CommandBusHeaderMessage('Infrastructure dropped'))->print($outputPrinter);

return 0;
}
Expand Down
Loading

0 comments on commit 85e94dc

Please sign in to comment.