Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
31 changes: 16 additions & 15 deletions src/Command/RunSubscriptionCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Streak\Domain\Event\Subscription\Repository;
use Streak\Domain\EventStore;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Helper;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
Expand Down Expand Up @@ -53,22 +54,9 @@ protected function configure()

protected function execute(InputInterface $input, OutputInterface $output)
{
ProgressBar::setFormatDefinition('custom', 'Subscription <fg=blue>%subscription_type%</>(<fg=cyan>%subscription_id%</>) processed <fg=yellow>%current%</> events in <fg=magenta>%elapsed%</>.');

// progress bar by default is writing to stderr, lets try to mitigate that
if ($output instanceof StreamOutput) {
$output = new StreamOutput($output->getStream(), $output->getVerbosity(), null, $output->getFormatter()); // @codeCoverageIgnore
}

// by instantiating progress bar here we start measuring time before we load subscription,
// so elapsed time will include subscription initialization period
$progress = new ProgressBar($output);
$progress->setFormat('custom');
$progress->setOverwrite(true);
$progress->setMessage($input->getArgument('subscription-type'), 'subscription_type');
$progress->setMessage($input->getArgument('subscription-id'), 'subscription_id');

$start = time();
$subscription = $this->subscriptions->find($this->id($input));
$initializedIn = time() - $start;

if (null === $subscription) {
$output->write(sprintf('Subscription <fg=blue>%s</>(<fg=cyan>%s</>) not found.', $input->getArgument('subscription-type'), $input->getArgument('subscription-id')));
Expand All @@ -82,6 +70,19 @@ protected function execute(InputInterface $input, OutputInterface $output)
$limit = (int) $limit;
}

ProgressBar::setFormatDefinition('custom', 'Subscription <fg=blue>%subscription_type%</>(<fg=cyan>%subscription_id%</>) initialized in <fg=yellow>%initialized_in%</> and processed <fg=yellow>%current%</> events in <fg=magenta>%elapsed%</>.');

// progress bar by default is writing to stderr, lets try to mitigate that
if ($output instanceof StreamOutput) {
$output = new StreamOutput($output->getStream(), $output->getVerbosity(), null, $output->getFormatter()); // @codeCoverageIgnore
}

$progress = new ProgressBar($output);
$progress->setFormat('custom');
$progress->setOverwrite(true);
$progress->setMessage($input->getArgument('subscription-type'), 'subscription_type');
$progress->setMessage($input->getArgument('subscription-id'), 'subscription_id');
$progress->setMessage(Helper::formatTime($initializedIn), 'initialized_in');
$progress->display();

try {
Expand Down
4 changes: 2 additions & 2 deletions src/Command/RunSubscriptionsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,8 @@ private function executable(array $command, InputInterface $input) : array

private function decorate(string $string) : string
{
$pattern = '/Subscription (.+)\((.+)\) processed (.+) events in (.+)./i';
$replacement = 'Subscription <fg=blue>$1</>(<fg=cyan>$2</>) processed <fg=yellow>$3</> events in <fg=magenta>$4</>.';
$pattern = '/Subscription (.+)\((.+)\) initialized in (.+) and processed (.+) events in (.+)./i';
$replacement = 'Subscription <fg=blue>$1</>(<fg=cyan>$2</>) initialized in <fg=yellow>$3</> and processed <fg=yellow>$4</> events in <fg=magenta>$5</>.';
$decorated = preg_replace($pattern, $replacement, $string);

if (null === $decorated) {
Expand Down
12 changes: 6 additions & 6 deletions src/Tests/Command/RunSubscriptionCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@ public function testCommand()
$command->run(new ArrayInput(['subscription-type' => 'Streak\\StreakBundle\\Tests\\Command\\RunSubscriptionCommandTest\\SubscriptionId1', 'subscription-id' => 'EC2BE294-C07A-4198-A159-4551686F14F9']), $this->output);

$expected =
"Subscription Streak\StreakBundle\Tests\Command\RunSubscriptionCommandTest\SubscriptionId1(EC2BE294-C07A-4198-A159-4551686F14F9) processed 0 events in < 1 sec.".
"Subscription Streak\StreakBundle\Tests\Command\RunSubscriptionCommandTest\SubscriptionId1(EC2BE294-C07A-4198-A159-4551686F14F9) initialized in < 1 sec and processed 0 events in < 1 sec.".
self::TERMINAL_CLEAR_LINE.
"Subscription Streak\StreakBundle\Tests\Command\RunSubscriptionCommandTest\SubscriptionId1(EC2BE294-C07A-4198-A159-4551686F14F9) processed 2 events in < 1 sec."
"Subscription Streak\StreakBundle\Tests\Command\RunSubscriptionCommandTest\SubscriptionId1(EC2BE294-C07A-4198-A159-4551686F14F9) initialized in < 1 sec and processed 2 events in < 1 sec."
;
$this->assertEquals($expected, $this->output->output);
}
Expand All @@ -141,9 +141,9 @@ public function testCommandWithLimit()
$command->run(new ArrayInput(['subscription-type' => 'Streak\\StreakBundle\\Tests\\Command\\RunSubscriptionCommandTest\\SubscriptionId1', 'subscription-id' => 'EC2BE294-C07A-4198-A159-4551686F14F9', '--listening-limit' => 763723]), $this->output);

$expected =
"Subscription Streak\StreakBundle\Tests\Command\RunSubscriptionCommandTest\SubscriptionId1(EC2BE294-C07A-4198-A159-4551686F14F9) processed 0 events in < 1 sec.".
"Subscription Streak\StreakBundle\Tests\Command\RunSubscriptionCommandTest\SubscriptionId1(EC2BE294-C07A-4198-A159-4551686F14F9) initialized in < 1 sec and processed 0 events in < 1 sec.".
self::TERMINAL_CLEAR_LINE.
"Subscription Streak\StreakBundle\Tests\Command\RunSubscriptionCommandTest\SubscriptionId1(EC2BE294-C07A-4198-A159-4551686F14F9) processed 2 events in < 1 sec."
"Subscription Streak\StreakBundle\Tests\Command\RunSubscriptionCommandTest\SubscriptionId1(EC2BE294-C07A-4198-A159-4551686F14F9) initialized in < 1 sec and processed 2 events in < 1 sec."
;
$this->assertEquals($expected, $this->output->output);
}
Expand Down Expand Up @@ -217,9 +217,9 @@ public function testError()
$command->run(new ArrayInput(['subscription-type' => 'Streak\\StreakBundle\\Tests\\Command\\RunSubscriptionCommandTest\\SubscriptionId1', 'subscription-id' => 'EC2BE294-C07A-4198-A159-4551686F14F9']), $this->output);
} finally {
$expected =
"Subscription Streak\StreakBundle\Tests\Command\RunSubscriptionCommandTest\SubscriptionId1(EC2BE294-C07A-4198-A159-4551686F14F9) processed 0 events in < 1 sec.".
"Subscription Streak\StreakBundle\Tests\Command\RunSubscriptionCommandTest\SubscriptionId1(EC2BE294-C07A-4198-A159-4551686F14F9) initialized in < 1 sec and processed 0 events in < 1 sec.".
self::TERMINAL_CLEAR_LINE.
"Subscription Streak\StreakBundle\Tests\Command\RunSubscriptionCommandTest\SubscriptionId1(EC2BE294-C07A-4198-A159-4551686F14F9) processed 2 events in < 1 sec."
"Subscription Streak\StreakBundle\Tests\Command\RunSubscriptionCommandTest\SubscriptionId1(EC2BE294-C07A-4198-A159-4551686F14F9) initialized in < 1 sec and processed 2 events in < 1 sec."
;
$this->assertEquals($expected, $this->output->output);
}
Expand Down