-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #402 from patchlevel/projectionist-dev-runner
add dev runner for projectionist
- Loading branch information
Showing
6 changed files
with
199 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Patchlevel\EventSourcing\Console\Command; | ||
|
||
use Patchlevel\EventSourcing\Console\InputHelper; | ||
use Patchlevel\EventSourcing\Console\InvalidArgumentGiven; | ||
use Patchlevel\EventSourcing\Projection\Projectionist\Listener\ThrowErrorListener; | ||
use Patchlevel\EventSourcing\Projection\Projectionist\Projectionist; | ||
use Patchlevel\Worker\DefaultWorker; | ||
use Symfony\Component\Console\Attribute\AsCommand; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Logger\ConsoleLogger; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\EventDispatcher\EventDispatcherInterface; | ||
|
||
#[AsCommand( | ||
'event-sourcing:projectionist:dev-run', | ||
'Run the active projectors', | ||
)] | ||
final class ProjectionistDevRunCommand extends ProjectionistCommand | ||
{ | ||
public function __construct( | ||
Projectionist $projectionist, | ||
private readonly EventDispatcherInterface $eventDispatcher, | ||
) { | ||
parent::__construct($projectionist); | ||
} | ||
|
||
protected function configure(): void | ||
{ | ||
parent::configure(); | ||
|
||
$this | ||
->addOption( | ||
'run-limit', | ||
null, | ||
InputOption::VALUE_REQUIRED, | ||
'The maximum number of runs this command should execute', | ||
) | ||
->addOption( | ||
'message-limit', | ||
null, | ||
InputOption::VALUE_REQUIRED, | ||
'How many messages should be consumed in one run', | ||
100, | ||
) | ||
->addOption( | ||
'memory-limit', | ||
null, | ||
InputOption::VALUE_REQUIRED, | ||
'How much memory consumption should the worker be terminated', | ||
) | ||
->addOption( | ||
'time-limit', | ||
null, | ||
InputOption::VALUE_REQUIRED, | ||
'What is the maximum time the worker can run in seconds', | ||
) | ||
->addOption( | ||
'sleep', | ||
null, | ||
InputOption::VALUE_REQUIRED, | ||
'How much time should elapse before the next job is executed in microseconds', | ||
1000, | ||
); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$runLimit = InputHelper::nullablePositivInt($input->getOption('run-limit')); | ||
$messageLimit = InputHelper::nullablePositivInt($input->getOption('message-limit')); | ||
$memoryLimit = InputHelper::nullableString($input->getOption('memory-limit')); | ||
$timeLimit = InputHelper::nullablePositivInt($input->getOption('time-limit')); | ||
$sleep = InputHelper::int($input->getOption('sleep')); | ||
$criteria = $this->projectionCriteria($input); | ||
|
||
$logger = new ConsoleLogger($output); | ||
|
||
$this->eventDispatcher->addSubscriber(new ThrowErrorListener()); | ||
|
||
$worker = DefaultWorker::create( | ||
function () use ($criteria, $messageLimit): void { | ||
$this->projectionist->run($criteria, $messageLimit); | ||
}, | ||
[ | ||
'runLimit' => $runLimit, | ||
'memoryLimit' => $memoryLimit, | ||
'timeLimit' => $timeLimit, | ||
], | ||
$logger, | ||
); | ||
|
||
if ($sleep < 0) { | ||
throw new InvalidArgumentGiven($sleep, '0|positive-int'); | ||
} | ||
|
||
$this->projectionist->remove($criteria); | ||
$this->projectionist->boot($criteria); | ||
$worker->run($sleep); | ||
|
||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/Projection/Projectionist/Event/ProjectorErrorEvent.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Patchlevel\EventSourcing\Projection\Projectionist\Event; | ||
|
||
use Patchlevel\EventSourcing\Projection\Projection\ProjectionId; | ||
use Patchlevel\EventSourcing\Projection\Projector\Projector; | ||
use Throwable; | ||
|
||
final class ProjectorErrorEvent | ||
{ | ||
public function __construct( | ||
/** @var class-string<Projector> */ | ||
public readonly string $projector, | ||
public readonly ProjectionId $projection, | ||
public readonly Throwable $error, | ||
) { | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/Projection/Projectionist/Listener/ThrowErrorListener.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Patchlevel\EventSourcing\Projection\Projectionist\Listener; | ||
|
||
use Patchlevel\EventSourcing\Projection\Projectionist\Event\ProjectorErrorEvent; | ||
use Patchlevel\EventSourcing\Projection\Projectionist\ProjectionistError; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
|
||
final class ThrowErrorListener implements EventSubscriberInterface | ||
{ | ||
public function onProjectorError(ProjectorErrorEvent $event): void | ||
{ | ||
throw new ProjectionistError( | ||
$event->projector, | ||
$event->projection, | ||
$event->error, | ||
); | ||
} | ||
|
||
/** @return array<class-string, string> */ | ||
public static function getSubscribedEvents(): array | ||
{ | ||
return [ProjectorErrorEvent::class => 'onProjectorError']; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Patchlevel\EventSourcing\Projection\Projectionist; | ||
|
||
use Patchlevel\EventSourcing\Projection\Projection\ProjectionId; | ||
use RuntimeException; | ||
use Throwable; | ||
|
||
use function sprintf; | ||
|
||
final class ProjectionistError extends RuntimeException | ||
{ | ||
public function __construct( | ||
public readonly string $projector, | ||
public readonly ProjectionId $projectionId, | ||
Throwable $error, | ||
) { | ||
parent::__construct( | ||
sprintf( | ||
'error in projector "%s" for "%s": %s', | ||
$projector, | ||
$projectionId->toString(), | ||
$error->getMessage(), | ||
), | ||
0, | ||
$error, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters