Skip to content

Commit

Permalink
Publish stored events in Identity to RabbitMq #14
Browse files Browse the repository at this point in the history
  • Loading branch information
marein committed Sep 21, 2018
1 parent 1e69387 commit 8c27749
Show file tree
Hide file tree
Showing 10 changed files with 255 additions and 9 deletions.
1 change: 1 addition & 0 deletions code/config/identity/config.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
imports:
- { resource: services/application_life_cycle.yml }
- { resource: services/console.yml }
- { resource: services/persistence.yml }
- { resource: services/subscriber.yml }
- { resource: services/user.yml }
Expand Down
11 changes: 11 additions & 0 deletions code/config/identity/services/console.yml
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 }
14 changes: 14 additions & 0 deletions code/config/identity/services/persistence.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
services:

identity.predis:
class: Predis\Client
public: false
arguments:
- '%env(GAMBLING_IDENTITY_PREDIS_CLIENT_URL)%'
-
prefix: 'identity.'

identity.doctrine-dbal:
alias: 'doctrine.dbal.identity_connection'

identity.doctrine-orm:
alias: 'doctrine.orm.identity_entity_manager'

identity.message-broker:
class: Gambling\Common\Port\Adapter\Messaging\GamblingMessageBroker
public: false
arguments:
- '%env(GAMBLING_IDENTITY_RABBIT_MQ_DSN)%'

identity.event-store:
class: Gambling\Common\Port\Adapter\EventStore\DoctrineEventStore
public: false
Expand Down
2 changes: 2 additions & 0 deletions code/environment.env.dist
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ GAMBLING_CONNECT_FOUR_RABBIT_MQ_DSN="amqp://guest:guest@rabbit-mq:5672?receive_m
# Identity Context #
############################
GAMBLING_IDENTITY_DOCTRINE_DBAL_URL="mysql://root:password@mysql:3306/identity"
GAMBLING_IDENTITY_PREDIS_CLIENT_URL="redis://redis:6379"
GAMBLING_IDENTITY_RABBIT_MQ_DSN="amqp://guest:guest@rabbit-mq:5672?receive_method=basic_consume&qos_prefetch_count=1000"

############################
# Web Interface Context #
Expand Down
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);
}
}
}
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;
}
}
11 changes: 11 additions & 0 deletions docker-compose.ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,17 @@ services:
- rabbit-mq
- nchan
restart: on-failure
php-identity-publish-stored-events-to-rabbit-mq:
image: marein/php-gambling-website:php-fpm
command: /project/bin/console identity:publish-stored-events-to-rabbit-mq --env=prod
environment:
WAIT_FOR: mysql:3306,redis:6379,rabbit-mq:5672,nchan:81
links:
- mysql
- redis
- rabbit-mq
- nchan
restart: on-failure
php-web-interface-publish-running-games-count-to-nchan:
image: marein/php-gambling-website:php-fpm
command: /project/bin/console web-interface:publish-running-games-count-to-nchan --env=prod
Expand Down
51 changes: 42 additions & 9 deletions docker-compose.production-advanced.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,10 @@ services:
GAMBLING_ENVIRONMENT: prod
GAMBLING_CHAT_DOCTRINE_DBAL_URL: mysql://root:password@mysql-chat:3306/chat
GAMBLING_CHAT_PREDIS_CLIENT_URL: redis://redis-chat:6379
GAMBLING_CONNECT_FOUR_DOCTRINE_DBAL_URL: mysql://root:password@mysql-connect-four:3306/chat
GAMBLING_CONNECT_FOUR_DOCTRINE_DBAL_URL: mysql://root:password@mysql-connect-four:3306/connect-four
GAMBLING_CONNECT_FOUR_PREDIS_CLIENT_URL: redis://redis-connect-four:6379
GAMBLING_IDENTITY_DOCTRINE_DBAL_URL: mysql://root:password@mysql-identity:3306/chat
GAMBLING_IDENTITY_DOCTRINE_DBAL_URL: mysql://root:password@mysql-identity:3306/identity
GAMBLING_IDENTITY_PREDIS_CLIENT_URL: redis://redis-identity:6379
GAMBLING_WEB_INTERFACE_PREDIS_CLIENT_URL: redis://redis-web-interface:6379
WAIT_FOR: mysql-chat:3306,mysql-connect-four:3306,mysql-identity:3306
links:
Expand All @@ -109,8 +110,10 @@ services:
environment:
GAMBLING_CHAT_DOCTRINE_DBAL_URL: mysql://root:password@mysql-chat:3306/chat
GAMBLING_CHAT_PREDIS_CLIENT_URL: redis://redis-chat:6379
GAMBLING_CONNECT_FOUR_DOCTRINE_DBAL_URL: mysql://root:password@mysql-connect-four:3306/chat
GAMBLING_CONNECT_FOUR_DOCTRINE_DBAL_URL: mysql://root:password@mysql-connect-four:3306/connect-four
GAMBLING_CONNECT_FOUR_PREDIS_CLIENT_URL: redis://redis-connect-four:6379
GAMBLING_IDENTITY_DOCTRINE_DBAL_URL: mysql://root:password@mysql-identity:3306/identity
GAMBLING_IDENTITY_PREDIS_CLIENT_URL: redis://redis-identity:6379
WAIT_FOR: mysql-chat:3306,mysql-connect-four:3306,mysql-identity:3306,redis-connect-four:6379
links:
- mysql-chat
Expand All @@ -125,8 +128,10 @@ services:
environment:
GAMBLING_CHAT_DOCTRINE_DBAL_URL: mysql://root:password@mysql-chat:3306/chat
GAMBLING_CHAT_PREDIS_CLIENT_URL: redis://redis-chat:6379
GAMBLING_CONNECT_FOUR_DOCTRINE_DBAL_URL: mysql://root:password@mysql-connect-four:3306/chat
GAMBLING_CONNECT_FOUR_DOCTRINE_DBAL_URL: mysql://root:password@mysql-connect-four:3306/connect-four
GAMBLING_CONNECT_FOUR_PREDIS_CLIENT_URL: redis://redis-connect-four:6379
GAMBLING_IDENTITY_DOCTRINE_DBAL_URL: mysql://root:password@mysql-identity:3306/identity
GAMBLING_IDENTITY_PREDIS_CLIENT_URL: redis://redis-identity:6379
WAIT_FOR: mysql-chat:3306,mysql-connect-four:3306,mysql-identity:3306,redis-connect-four:6379,rabbit-mq:5672
links:
- mysql-chat
Expand All @@ -141,8 +146,10 @@ services:
environment:
GAMBLING_CHAT_DOCTRINE_DBAL_URL: mysql://root:password@mysql-chat:3306/chat
GAMBLING_CHAT_PREDIS_CLIENT_URL: redis://redis-chat:6379
GAMBLING_CONNECT_FOUR_DOCTRINE_DBAL_URL: mysql://root:password@mysql-connect-four:3306/chat
GAMBLING_CONNECT_FOUR_DOCTRINE_DBAL_URL: mysql://root:password@mysql-connect-four:3306/connect-four
GAMBLING_CONNECT_FOUR_PREDIS_CLIENT_URL: redis://redis-connect-four:6379
GAMBLING_IDENTITY_DOCTRINE_DBAL_URL: mysql://root:password@mysql-identity:3306/identity
GAMBLING_IDENTITY_PREDIS_CLIENT_URL: redis://redis-identity:6379
WAIT_FOR: mysql-chat:3306,mysql-connect-four:3306,mysql-identity:3306,redis-chat:6379,rabbit-mq:5672
links:
- mysql-chat
Expand All @@ -151,14 +158,34 @@ services:
- redis-chat
- rabbit-mq
restart: on-failure
php-identity-publish-stored-events-to-rabbit-mq:
image: marein/php-gambling-website:php-fpm
command: /project/bin/console identity:publish-stored-events-to-rabbit-mq --env=prod
environment:
GAMBLING_CHAT_DOCTRINE_DBAL_URL: mysql://root:password@mysql-chat:3306/chat
GAMBLING_CHAT_PREDIS_CLIENT_URL: redis://redis-chat:6379
GAMBLING_CONNECT_FOUR_DOCTRINE_DBAL_URL: mysql://root:password@mysql-connect-four:3306/connect-four
GAMBLING_CONNECT_FOUR_PREDIS_CLIENT_URL: redis://redis-connect-four:6379
GAMBLING_IDENTITY_DOCTRINE_DBAL_URL: mysql://root:password@mysql-identity:3306/identity
GAMBLING_IDENTITY_PREDIS_CLIENT_URL: redis://redis-identity:6379
WAIT_FOR: mysql-chat:3306,mysql-connect-four:3306,mysql-identity:3306,redis-identity:6379,rabbit-mq:5672
links:
- mysql-chat
- mysql-connect-four
- mysql-identity
- redis-identity
- rabbit-mq
restart: on-failure
php-web-interface-publish-running-games-count-to-nchan:
image: marein/php-gambling-website:php-fpm
command: /project/bin/console web-interface:publish-running-games-count-to-nchan --env=prod
environment:
GAMBLING_CHAT_DOCTRINE_DBAL_URL: mysql://root:password@mysql-chat:3306/chat
GAMBLING_CHAT_PREDIS_CLIENT_URL: redis://redis-chat:6379
GAMBLING_CONNECT_FOUR_DOCTRINE_DBAL_URL: mysql://root:password@mysql-connect-four:3306/chat
GAMBLING_CONNECT_FOUR_DOCTRINE_DBAL_URL: mysql://root:password@mysql-connect-four:3306/connect-four
GAMBLING_CONNECT_FOUR_PREDIS_CLIENT_URL: redis://redis-connect-four:6379
GAMBLING_IDENTITY_DOCTRINE_DBAL_URL: mysql://root:password@mysql-identity:3306/identity
GAMBLING_IDENTITY_PREDIS_CLIENT_URL: redis://redis-identity:6379
WAIT_FOR: mysql-chat:3306,mysql-connect-four:3306,mysql-identity:3306,nchan:81,rabbit-mq:5672
links:
- mysql-chat
Expand All @@ -173,8 +200,10 @@ services:
environment:
GAMBLING_CHAT_DOCTRINE_DBAL_URL: mysql://root:password@mysql-chat:3306/chat
GAMBLING_CHAT_PREDIS_CLIENT_URL: redis://redis-chat:6379
GAMBLING_CONNECT_FOUR_DOCTRINE_DBAL_URL: mysql://root:password@mysql-connect-four:3306/chat
GAMBLING_CONNECT_FOUR_DOCTRINE_DBAL_URL: mysql://root:password@mysql-connect-four:3306/connect-four
GAMBLING_CONNECT_FOUR_PREDIS_CLIENT_URL: redis://redis-connect-four:6379
GAMBLING_IDENTITY_DOCTRINE_DBAL_URL: mysql://root:password@mysql-identity:3306/identity
GAMBLING_IDENTITY_PREDIS_CLIENT_URL: redis://redis-identity:6379
WAIT_FOR: mysql-chat:3306,mysql-connect-four:3306,mysql-identity:3306,nchan:81,rabbit-mq:5672
links:
- mysql-chat
Expand All @@ -189,8 +218,10 @@ services:
environment:
GAMBLING_CHAT_DOCTRINE_DBAL_URL: mysql://root:password@mysql-chat:3306/chat
GAMBLING_CHAT_PREDIS_CLIENT_URL: redis://redis-chat:6379
GAMBLING_CONNECT_FOUR_DOCTRINE_DBAL_URL: mysql://root:password@mysql-connect-four:3306/chat
GAMBLING_CONNECT_FOUR_DOCTRINE_DBAL_URL: mysql://root:password@mysql-connect-four:3306/connect-four
GAMBLING_CONNECT_FOUR_PREDIS_CLIENT_URL: redis://redis-connect-four:6379
GAMBLING_IDENTITY_DOCTRINE_DBAL_URL: mysql://root:password@mysql-identity:3306/identity
GAMBLING_IDENTITY_PREDIS_CLIENT_URL: redis://redis-identity:6379
WAIT_FOR: mysql-chat:3306,mysql-connect-four:3306,mysql-identity:3306,rabbit-mq:5672
links:
- mysql-chat
Expand All @@ -204,8 +235,10 @@ services:
environment:
GAMBLING_CHAT_DOCTRINE_DBAL_URL: mysql://root:password@mysql-chat:3306/chat
GAMBLING_CHAT_PREDIS_CLIENT_URL: redis://redis-chat:6379
GAMBLING_CONNECT_FOUR_DOCTRINE_DBAL_URL: mysql://root:password@mysql-connect-four:3306/chat
GAMBLING_CONNECT_FOUR_DOCTRINE_DBAL_URL: mysql://root:password@mysql-connect-four:3306/connect-four
GAMBLING_CONNECT_FOUR_PREDIS_CLIENT_URL: redis://redis-connect-four:6379
GAMBLING_IDENTITY_DOCTRINE_DBAL_URL: mysql://root:password@mysql-identity:3306/identity
GAMBLING_IDENTITY_PREDIS_CLIENT_URL: redis://redis-identity:6379
WAIT_FOR: mysql-chat:3306,mysql-connect-four:3306,mysql-identity:3306,rabbit-mq:5672
links:
- mysql-chat
Expand Down
11 changes: 11 additions & 0 deletions docker-compose.production.yml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,17 @@ services:
- rabbit-mq
- nchan
restart: on-failure
php-identity-publish-stored-events-to-rabbit-mq:
image: marein/php-gambling-website:php-fpm
command: /project/bin/console identity:publish-stored-events-to-rabbit-mq --env=prod
environment:
WAIT_FOR: mysql:3306,redis:6379,rabbit-mq:5672,nchan:81
links:
- mysql
- redis
- rabbit-mq
- nchan
restart: on-failure
php-web-interface-publish-running-games-count-to-nchan:
image: marein/php-gambling-website:php-fpm
command: /project/bin/console web-interface:publish-running-games-count-to-nchan --env=prod
Expand Down
13 changes: 13 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,19 @@ services:
- rabbit-mq
- nchan
restart: on-failure
php-identity-publish-stored-events-to-rabbit-mq:
build:
context: .
dockerfile: ./container/php-fpm/Dockerfile
command: /project/bin/console identity:publish-stored-events-to-rabbit-mq --env=prod
environment:
WAIT_FOR: mysql:3306,redis:6379,rabbit-mq:5672,nchan:81
links:
- mysql
- redis
- rabbit-mq
- nchan
restart: on-failure
php-web-interface-publish-running-games-count-to-nchan:
build:
context: .
Expand Down

0 comments on commit 8c27749

Please sign in to comment.