Skip to content

Commit

Permalink
additional "unhandled" event strategy + custom strategy support
Browse files Browse the repository at this point in the history
  • Loading branch information
prgTW committed Oct 15, 2016
1 parent 41cb103 commit 8c83bf4
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 18 deletions.
20 changes: 16 additions & 4 deletions doc/getting_started.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,24 @@ This will log consumed messages to the `asynchronous_command_bus` and `asynchron

## Choose event strategy

When handling events you have two strategies to choose from. Either you publish *all* events to the message queue or
you only publish the events that have a registered subscriber. If your application is the only one that consuming messages
you should consider using the **predefined** strategy. This will reduce the message overhead on the message queue.
When handling events you have three predefined strategies to choose from. Either you publish *all* events to the message
queue (*always* strategy) or you only publish the events that have a registered asynchronous subscriber
(*predefined* strategy) or you publish only events that weren't handled synchronously (*unhandled* strategy) regardless
of having an asynchronous event subscriber registered for these events. If your application is the only one that is
consuming messages you should consider using the **predefined** or **unhandled** strategy. This will reduce the message
overhead on the message queue.

```yaml
simple_bus_asynchronous:
events:
strategy: 'predefined' # default: 'always'
strategy: 'predefined' # 'predefined', 'unhandled' or 'always' (default: 'always')
```

You can select Your own strategy by defining customer **strategy_service_id**

```yaml
simple_bus_asynchronous:
events:
strategy:
strategy_service_id: your_strategy_service
```
24 changes: 21 additions & 3 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,28 @@ public function getConfigTreeBuilder()
->arrayNode('events')
->canBeEnabled()
->children()
->enumNode('strategy')
->arrayNode('strategy')
->addDefaultsIfNotSet()
->beforeNormalization()
->ifInArray(['always', 'predefined', 'unhandled'])
->then(function ($v) {
$map = [
'always' => 'simple_bus.asynchronous.always_publishes_messages_middleware',
'predefined' => 'simple_bus.asynchronous.publishes_predefined_messages_middleware',
'unhandled' => 'simple_bus.asynchronous.publishes_unhandled_messages_middleware',
];

return [
'strategy_service_id' => $map[$v]
];
})
->end()
->info('What strategy to use to publish messages')
->defaultValue('always')
->values(['always', 'predefined'])
->children()
->scalarNode('strategy_service_id')
->defaultValue('simple_bus.asynchronous.always_publishes_messages_middleware')
->end()
->end()
->end()
->scalarNode('publisher_service_id')
->info('Service id of an instance of Publisher')
Expand Down
7 changes: 1 addition & 6 deletions src/DependencyInjection/SimpleBusAsynchronousExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,7 @@ private function loadAsynchronousEventBus(array $config, ContainerBuilder $conta
$loader->load('asynchronous_events_logging.yml');
}


if ($config['strategy'] === 'always') {
$eventMiddleware = 'simple_bus.asynchronous.always_publishes_messages_middleware';
} else {
$eventMiddleware = 'simple_bus.asynchronous.publishes_predefined_messages_middleware';
}
$eventMiddleware = $config['strategy']['strategy_service_id'];

// insert before the middleware that actually notifies a message subscriber of the message
$container->getDefinition($eventMiddleware)->addTag('event_bus_middleware', ['priority' => 0]);
Expand Down
6 changes: 6 additions & 0 deletions src/Resources/config/asynchronous_events.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ services:
- '@simple_bus.event_bus.event_name_resolver'
- []

simple_bus.asynchronous.publishes_unhandled_messages_middleware:
class: SimpleBus\Asynchronous\MessageBus\PublishesUnhandledMessages
public: false
arguments:
- '@simple_bus.asynchronous.event_publisher'

simple_bus.asynchronous.event_bus.finishes_message_before_handling_next_middleware:
class: SimpleBus\Message\Bus\Middleware\FinishesHandlingMessageBeforeHandlingNext
public: false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionTestCase;
use SimpleBus\AsynchronousBundle\DependencyInjection\SimpleBusAsynchronousExtension;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class SimpleBusAsynchronousExtensionTest extends AbstractExtensionTestCase
{
Expand All @@ -25,7 +24,7 @@ protected function getMinimalConfiguration()
/**
* @test
*/
public function it_uses_strategy_allways_by_default()
public function it_uses_strategy_always_by_default()
{
$this->container->setParameter('kernel.bundles', ['SimpleBusCommandBusBundle'=>true, 'SimpleBusEventBusBundle'=>true]);
$this->load();
Expand All @@ -43,7 +42,29 @@ public function it_uses_strategy_predefined_when_configured()

$this->assertContainerBuilderHasServiceDefinitionWithTag('simple_bus.asynchronous.publishes_predefined_messages_middleware', 'event_bus_middleware', ['priority'=>0]);
}


/**
* @test
*/
public function it_uses_strategy_unhandled_when_configured()
{
$this->container->setParameter('kernel.bundles', ['SimpleBusCommandBusBundle'=>true, 'SimpleBusEventBusBundle'=>true]);
$this->load(['events'=>['strategy'=>'unhandled']]);

$this->assertContainerBuilderHasServiceDefinitionWithTag('simple_bus.asynchronous.publishes_unhandled_messages_middleware', 'event_bus_middleware', ['priority'=>0]);
}

/**
* @test
* @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException
* @expectedExceptionMessageRegExp ".*custom_strategy.*"
*/
public function it_uses_custom_strategy_when_configured()
{
$this->container->setParameter('kernel.bundles', ['SimpleBusCommandBusBundle'=>true, 'SimpleBusEventBusBundle'=>true]);
$this->load(['events'=>['strategy'=>['strategy_service_id'=>'custom_strategy']]]);
}

/**
* @test
* @expectedException \LogicException
Expand All @@ -54,7 +75,7 @@ public function it_throws_exception_if_command_bus_bundle_is_missing()
$this->container->setParameter('kernel.bundles', ['SimpleBusEventBusBundle'=>true]);
$this->load(['events'=>['strategy'=>'predefined']]);
}

/**
* @test
* @expectedException \LogicException
Expand All @@ -65,4 +86,4 @@ public function it_throws_exception_if_event_bus_bundle_is_missing()
$this->container->setParameter('kernel.bundles', ['SimpleBusCommandBusBundle'=>true]);
$this->load(['events'=>['strategy'=>'predefined']]);
}
}
}

0 comments on commit 8c83bf4

Please sign in to comment.