-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Publish stored events in Identity to RabbitMq #14
- Loading branch information
Showing
10 changed files
with
255 additions
and
9 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
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,11 @@ | ||
services: | ||
|
||
identity.publish-stored-events-to-rabbit-mq-command: | ||
class: Gambling\Identity\Port\Adapter\Console\PublishStoredEventsToRabbitMqCommand | ||
public: false | ||
arguments: | ||
- '@identity.event-store' | ||
- '@identity.predis' | ||
- '@identity.message-broker' | ||
tags: | ||
- { name: console.command } |
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
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
98 changes: 98 additions & 0 deletions
98
code/src/Identity/Port/Adapter/Console/PublishStoredEventsToRabbitMqCommand.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,98 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Gambling\Identity\Port\Adapter\Console; | ||
|
||
use Gambling\Common\EventStore\ConsistentOrderEventStore; | ||
use Gambling\Common\EventStore\EventStore; | ||
use Gambling\Common\EventStore\FollowEventStoreDispatcher; | ||
use Gambling\Common\EventStore\InMemoryCacheEventStorePointer; | ||
use Gambling\Common\EventStore\StoredEventPublisher; | ||
use Gambling\Common\EventStore\ThrottlingEventStore; | ||
use Gambling\Common\MessageBroker\MessageBroker; | ||
use Gambling\Common\Port\Adapter\EventStore\PredisEventStorePointer; | ||
use Gambling\Common\Port\Adapter\EventStore\Subscriber\SymfonyConsoleDebugSubscriber; | ||
use Gambling\Identity\Port\Adapter\Messaging\PublishStoredEventsToRabbitMqSubscriber; | ||
use Predis\Client; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
final class PublishStoredEventsToRabbitMqCommand extends Command | ||
{ | ||
/** | ||
* @var EventStore | ||
*/ | ||
private $eventStore; | ||
|
||
/** | ||
* @var Client | ||
*/ | ||
private $predis; | ||
|
||
/** | ||
* @var MessageBroker | ||
*/ | ||
private $messageBroker; | ||
|
||
/** | ||
* PublishStoredEventsToRabbitMqCommand constructor. | ||
* | ||
* @param EventStore $eventStore | ||
* @param Client $predis | ||
* @param MessageBroker $messageBroker | ||
*/ | ||
public function __construct(EventStore $eventStore, Client $predis, MessageBroker $messageBroker) | ||
{ | ||
parent::__construct(); | ||
|
||
$this->eventStore = $eventStore; | ||
$this->predis = $predis; | ||
$this->messageBroker = $messageBroker; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
protected function configure() | ||
{ | ||
$this | ||
->setName('identity:publish-stored-events-to-rabbit-mq'); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
// The creation of FollowEventStoreDispatcher could be done via container. | ||
$debugSubscriber = new SymfonyConsoleDebugSubscriber($output); | ||
$rabbitMqSubscriber = new PublishStoredEventsToRabbitMqSubscriber( | ||
$this->messageBroker | ||
); | ||
|
||
$eventStorePointer = new InMemoryCacheEventStorePointer( | ||
new PredisEventStorePointer( | ||
$this->predis, | ||
'rabbit-mq-pointer' | ||
) | ||
); | ||
|
||
$storedEventPublisher = new StoredEventPublisher(); | ||
$storedEventPublisher->subscribe($rabbitMqSubscriber); | ||
$storedEventPublisher->subscribe($debugSubscriber); | ||
|
||
$followEventStoreDispatcher = new FollowEventStoreDispatcher( | ||
$eventStorePointer, | ||
new ThrottlingEventStore( | ||
new ConsistentOrderEventStore($this->eventStore), | ||
100 | ||
), | ||
$storedEventPublisher | ||
); | ||
|
||
while (true) { | ||
$followEventStoreDispatcher->dispatch(1000); | ||
} | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
code/src/Identity/Port/Adapter/Messaging/PublishStoredEventsToRabbitMqSubscriber.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,52 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Gambling\Identity\Port\Adapter\Messaging; | ||
|
||
use Gambling\Common\EventStore\StoredEvent; | ||
use Gambling\Common\EventStore\StoredEventSubscriber; | ||
use Gambling\Common\MessageBroker\MessageBroker; | ||
|
||
final class PublishStoredEventsToRabbitMqSubscriber implements StoredEventSubscriber | ||
{ | ||
/** | ||
* @var MessageBroker | ||
*/ | ||
private $messageBroker; | ||
|
||
/** | ||
* PublishStoredEventsToRabbitMqSubscriber constructor. | ||
* | ||
* @param MessageBroker $messageBroker | ||
*/ | ||
public function __construct(MessageBroker $messageBroker) | ||
{ | ||
$this->messageBroker = $messageBroker; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function handle(StoredEvent $storedEvent): void | ||
{ | ||
// We should definitely filter the events we are going to publish, | ||
// since that belongs to our public interface for the other contexts. | ||
// However, it's not done for simplicity in this sample project. | ||
// We could | ||
// * publish specific messages by name. | ||
// * filter out specific properties in the payload. | ||
// * translate when the properties for an event in the payload changed. | ||
$this->messageBroker->publish( | ||
$storedEvent->payload(), | ||
'Identity.' . $storedEvent->name() | ||
); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function isSubscribedTo(StoredEvent $storedEvent): bool | ||
{ | ||
return true; | ||
} | ||
} |
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
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
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
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