Skip to content

Commit

Permalink
remove logger from projectionist api and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidBadura committed Oct 10, 2022
1 parent 2d483c5 commit 0febbd0
Show file tree
Hide file tree
Showing 10 changed files with 703 additions and 79 deletions.
16 changes: 15 additions & 1 deletion baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
</DeprecatedClass>
</file>
<file src="src/Console/Command/ProjectionistRunCommand.php">
<ArgumentTypeCoercion occurrences="1">
<ArgumentTypeCoercion occurrences="2">
<code>$messageLimit</code>
<code>$sleep</code>
</ArgumentTypeCoercion>
</file>
Expand Down Expand Up @@ -343,6 +344,19 @@
<code>$this-&gt;prophesize(PipelineStore::class)</code>
</DeprecatedClass>
</file>
<file src="tests/Unit/Projection/DefaultProjectionistTest.php">
<DeprecatedInterface occurrences="9">
<code>class implements Projector {</code>
<code>class implements Projector {</code>
<code>class implements Projector {</code>
<code>class implements Projector {</code>
<code>class implements Projector {</code>
<code>class implements Projector {</code>
<code>class implements Projector {</code>
<code>class implements Projector {</code>
<code>class implements Projector {</code>
</DeprecatedInterface>
</file>
<file src="tests/Unit/Projection/DefaultProjectorRepositoryTest.php">
<DeprecatedInterface occurrences="2">
<code>class implements Projector {</code>
Expand Down
5 changes: 1 addition & 4 deletions src/Console/Command/ProjectionistBootCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Logger\ConsoleLogger;
use Symfony\Component\Console\Output\OutputInterface;

#[AsCommand(
Expand All @@ -17,10 +16,8 @@ final class ProjectionistBootCommand extends ProjectionistCommand
{
protected function execute(InputInterface $input, OutputInterface $output): int
{
$logger = new ConsoleLogger($output);

$criteria = $this->projectorCriteria();
$this->projectionist->boot($criteria, $logger);
$this->projectionist->boot($criteria);

return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Console/Command/ProjectionistCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
abstract class ProjectionistCommand extends Command
{
public function __construct(
protected readonly Projectionist $projectionist
protected readonly Projectionist $projectionist,
) {
parent::__construct();
}
Expand Down
5 changes: 1 addition & 4 deletions src/Console/Command/ProjectionistRemoveCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Logger\ConsoleLogger;
use Symfony\Component\Console\Output\OutputInterface;

#[AsCommand(
Expand All @@ -17,10 +16,8 @@ final class ProjectionistRemoveCommand extends ProjectionistCommand
{
protected function execute(InputInterface $input, OutputInterface $output): int
{
$logger = new ConsoleLogger($output);

$criteria = $this->projectorCriteria();
$this->projectionist->remove($criteria, $logger);
$this->projectionist->remove($criteria);

return 0;
}
Expand Down
14 changes: 10 additions & 4 deletions src/Console/Command/ProjectionistRunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ protected function configure(): void
protected function execute(InputInterface $input, OutputInterface $output): int
{
$runLimit = InputHelper::nullableInt($input->getOption('run-limit'));
$messageLimit = InputHelper::int($input->getOption('message-limit'));
$messageLimit = InputHelper::nullableInt($input->getOption('message-limit'));
$memoryLimit = InputHelper::nullableString($input->getOption('memory-limit'));
$timeLimit = InputHelper::nullableInt($input->getOption('time-limit'));
$sleep = InputHelper::int($input->getOption('sleep'));
Expand All @@ -85,7 +85,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

if ($memoryLimit) {
$eventDispatcher->addSubscriber(new StopWorkerOnMemoryLimitListener(Bytes::parseFromString($memoryLimit), $logger));
$eventDispatcher->addSubscriber(
new StopWorkerOnMemoryLimitListener(Bytes::parseFromString($memoryLimit), $logger)
);
}

if ($timeLimit) {
Expand All @@ -96,9 +98,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$eventDispatcher->addSubscriber(new StopWorkerOnTimeLimitListener($timeLimit, $logger));
}

if ($messageLimit !== null && $messageLimit <= 0) {
throw new InvalidArgumentGiven($messageLimit, 'null|positive-int');
}

$worker = new DefaultWorker(
function () use ($criteria, $messageLimit, $logger): void {
$this->projectionist->run($criteria, $messageLimit, $logger);
function () use ($criteria, $messageLimit): void {
$this->projectionist->run($criteria, $messageLimit);
},
$eventDispatcher,
$logger
Expand Down
5 changes: 1 addition & 4 deletions src/Console/Command/ProjectionistTeardownCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Logger\ConsoleLogger;
use Symfony\Component\Console\Output\OutputInterface;

#[AsCommand(
Expand All @@ -17,10 +16,8 @@ final class ProjectionistTeardownCommand extends ProjectionistCommand
{
protected function execute(InputInterface $input, OutputInterface $output): int
{
$logger = new ConsoleLogger($output);

$criteria = $this->projectorCriteria();
$this->projectionist->teardown($criteria, $logger);
$this->projectionist->teardown($criteria);

return 0;
}
Expand Down
68 changes: 28 additions & 40 deletions src/Projection/DefaultProjectionist.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@ public function __construct(
private readonly ProjectorStore $projectorStore,
private readonly ProjectorRepository $projectorRepository,
private readonly ProjectorResolver $resolver = new MetadataProjectorResolver(),
private readonly ?LoggerInterface $logger = null
) {
}

public function boot(
ProjectorCriteria $criteria = new ProjectorCriteria(),
?LoggerInterface $logger = null
): void {
public function boot(ProjectorCriteria $criteria = new ProjectorCriteria()): void
{
$projectorStates = $this->projectorStates()
->filterByProjectorStatus(ProjectorStatus::New)
->filterByCriteria($criteria);
Expand All @@ -50,10 +49,10 @@ public function boot(

try {
$createMethod();
$logger?->info(sprintf('%s created', $projectorState->id()->toString()));
$this->logger?->info(sprintf('%s created', $projectorState->id()->toString()));
} catch (Throwable $e) {
$logger?->error(sprintf('%s create error', $projectorState->id()->toString()));
$logger?->error($e->getMessage());
$this->logger?->error(sprintf('%s create error', $projectorState->id()->toString()));
$this->logger?->error($e->getMessage());
$projectorState->error();
$this->projectorStore->saveProjectorState($projectorState);
}
Expand All @@ -63,21 +62,18 @@ public function boot(

foreach ($stream as $message) {
foreach ($projectorStates->filterByProjectorStatus(ProjectorStatus::Booting) as $projectorState) {
$this->handleMessage($message, $projectorState, $logger);
$this->handleMessage($message, $projectorState);
}
}

foreach ($projectorStates as $projectorState) {
foreach ($projectorStates->filterByProjectorStatus(ProjectorStatus::Booting) as $projectorState) {
$projectorState->active();
$this->projectorStore->saveProjectorState($projectorState);
}
}

public function run(
ProjectorCriteria $criteria = new ProjectorCriteria(),
?int $limit = null,
?LoggerInterface $logger = null
): void {
public function run(ProjectorCriteria $criteria = new ProjectorCriteria(), ?int $limit = null): void
{
$projectorStates = $this->projectorStates()
->filterByProjectorStatus(ProjectorStatus::Active)
->filterByCriteria($criteria);
Expand Down Expand Up @@ -109,7 +105,7 @@ public function run(

$currentPosition++;

$logger?->info(sprintf('position: %s', $currentPosition));
$this->logger?->info(sprintf('position: %s', $currentPosition));

$messageCounter++;
if ($messageCounter >= $limit) {
Expand All @@ -118,17 +114,17 @@ public function run(
}
}

public function teardown(
ProjectorCriteria $criteria = new ProjectorCriteria(),
?LoggerInterface $logger = null
): void {
public function teardown(ProjectorCriteria $criteria = new ProjectorCriteria()): void
{
$projectorStates = $this->projectorStates()->filterByProjectorStatus(ProjectorStatus::Outdated);

foreach ($projectorStates as $projectorState) {
$projector = $this->projectorRepository->findByProjectorId($projectorState->id());

if (!$projector) {
$logger?->warning(sprintf('projector with the id "%s" not found', $projectorState->id()->toString()));
$this->logger?->warning(
sprintf('projector with the id "%s" not found', $projectorState->id()->toString())
);
continue;
}

Expand All @@ -137,13 +133,10 @@ public function teardown(
if ($dropMethod) {
try {
$dropMethod();
$logger?->info(sprintf('%s dropped', $projectorState->id()->toString()));
$this->logger?->info(sprintf('%s dropped', $projectorState->id()->toString()));
} catch (Throwable $e) {
$logger?->error(sprintf('%s drop error', $projectorState->id()->toString()));
$logger?->error($e->getMessage());
$projectorState->error();
$this->projectorStore->saveProjectorState($projectorState);

$this->logger?->error(sprintf('%s drop error', $projectorState->id()->toString()));
$this->logger?->error($e->getMessage());
continue;
}
}
Expand All @@ -152,10 +145,8 @@ public function teardown(
}
}

public function remove(
ProjectorCriteria $criteria = new ProjectorCriteria(),
?LoggerInterface $logger = null
): void {
public function remove(ProjectorCriteria $criteria = new ProjectorCriteria()): void
{
$projectorStates = $this->projectorStates();

foreach ($projectorStates as $projectorState) {
Expand All @@ -167,10 +158,10 @@ public function remove(
if ($dropMethod) {
try {
$dropMethod();
$logger?->info(sprintf('%s dropped', $projectorState->id()->toString()));
$this->logger?->info(sprintf('%s dropped', $projectorState->id()->toString()));
} catch (Throwable $e) {
$logger?->warning(sprintf('%s drop error, skipped', $projectorState->id()->toString()));
$logger?->error($e->getMessage());
$this->logger?->warning(sprintf('%s drop error, skipped', $projectorState->id()->toString()));
$this->logger?->error($e->getMessage());
}
}
}
Expand All @@ -179,11 +170,8 @@ public function remove(
}
}

private function handleMessage(
Message $message,
ProjectorState $projectorState,
?LoggerInterface $logger = null
): void {
private function handleMessage(Message $message, ProjectorState $projectorState): void
{
$projector = $this->projectorRepository->findByProjectorId($projectorState->id());

if (!$projector) {
Expand All @@ -196,8 +184,8 @@ private function handleMessage(
try {
$handleMethod($message);
} catch (Throwable $e) {
$logger?->error(sprintf('%s message error', $projectorState->id()->toString()));
$logger?->error($e->getMessage());
$this->logger?->error(sprintf('%s message error', $projectorState->id()->toString()));
$this->logger?->error($e->getMessage());
$projectorState->error();
$this->projectorStore->saveProjectorState($projectorState);

Expand Down
31 changes: 10 additions & 21 deletions src/Projection/Projectionist.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,19 @@
namespace Patchlevel\EventSourcing\Projection;

use Patchlevel\EventSourcing\Projection\ProjectorStore\ProjectorStateCollection;
use Psr\Log\LoggerInterface;

interface Projectionist
{
public function boot(
ProjectorCriteria $criteria = new ProjectorCriteria(),
?LoggerInterface $logger = null
): void;

public function run(
ProjectorCriteria $criteria = new ProjectorCriteria(),
?int $limit = null,
?LoggerInterface $logger = null
): void;

public function teardown(
ProjectorCriteria $criteria = new ProjectorCriteria(),
?LoggerInterface $logger = null
): void;

public function remove(
ProjectorCriteria $criteria = new ProjectorCriteria(),
?LoggerInterface $logger = null
): void;
public function boot(ProjectorCriteria $criteria = new ProjectorCriteria()): void;

/**
* @param positive-int $limit
*/
public function run(ProjectorCriteria $criteria = new ProjectorCriteria(), ?int $limit = null): void;

public function teardown(ProjectorCriteria $criteria = new ProjectorCriteria()): void;

public function remove(ProjectorCriteria $criteria = new ProjectorCriteria()): void;

public function projectorStates(): ProjectorStateCollection;
}
Loading

0 comments on commit 0febbd0

Please sign in to comment.