Skip to content

Commit

Permalink
Merge pull request #3 from krakphp/must-match-all-receivers
Browse files Browse the repository at this point in the history
Must Match All Receivers
  • Loading branch information
ragboyjr authored May 4, 2020
2 parents b69f3c0 + f3da5a6 commit bb4b1e6
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 2 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@ Each pool config must have a `receivers` property which is a simple Glob that wi

It's important to note, that a receiver can ONLY be apart of one pool. So if two pools have receiver patterns that match the same receiver, then the first defined pool would own that receiver.

### Disabling Must Match All Receivers

By default, the bundle will throw an exception if any receivers are not matched by the pool config. This is to help prevent any unexpected bugs where you the receiver name is for some reason not matched by a pool when you expected it to.

To disable this check, update the `must_match_all_receivers` config option to false:

```yaml
messenger_auto_scale:
must_match_all_receivers: false
```

## Configuring Heartbeats

By default, each worker pool will log a heartbeat event every 60 seconds. If you want to change the frequency of that, you use the pool `heartbeat_interval` to define the number of seconds between subsequent heartbeats.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ private function buildSupervisorPoolConfigs(array $rawPoolConfig, array $receive
[$matchedReceiverNames, $receiverNames] = $this->matchReceiverNameFromRawPool($rawPool, $receiverNames);
yield ['name' => $poolName, 'poolConfig' => $rawPool, 'receiverIds' => $matchedReceiverNames];
}

if (count($receiverNames) && $rawPoolConfig['must_match_all_receivers']) {
throw new \LogicException('Some receivers were not matched by the pool config: ' . implode(', ', $receiverNames));
}
}

/** return the matched receiver names and unmatched recevier names as a two tuple. */
Expand Down
8 changes: 6 additions & 2 deletions src/MessengerAutoScaleBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Krak\SymfonyMessengerAutoScale;

use Symfony\Component\Config\Definition\Builder\BooleanNodeDefinition;
use Symfony\Component\Config\Definition\Builder\ScalarNodeDefinition;
use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand All @@ -11,13 +12,13 @@
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\Config\FileLocator;
use function Krak\Schema\{
bool,
dict,
struct,
int,
listOf,
string,
ProcessSchema\SymfonyConfig\configTree
};
ProcessSchema\SymfonyConfig\configTree};

class MessengerAutoScaleBundle extends Bundle
{
Expand Down Expand Up @@ -53,6 +54,9 @@ public function getConfigTreeBuilder() {
'console_path' => string(['configure' => function(ScalarNodeDefinition $def) {
$def->defaultValue('%kernel.project_dir%/bin/console');
}]),
'must_match_all_receivers' => bool(['configure' => function(BooleanNodeDefinition $def) {
$def->defaultTrue();
}]),
'pools' => dict(struct([
'min_procs' => int(),
'max_procs' => int(),
Expand Down
37 changes: 37 additions & 0 deletions tests/Feature/Bundle/InitsKernel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Krak\SymfonyMessengerAutoScale\Tests\Feature\Bundle;

use Krak\SymfonyMessengerAutoScale\MessengerAutoScaleBundle;
use Krak\SymfonyMessengerAutoScale\Tests\Feature\Fixtures\TestFixtureBundle;
use Krak\SymfonyMessengerRedis\MessengerRedisBundle;
use Nyholm\BundleTest\CompilerPass\PublicServicePass;
use Symfony\Component\DependencyInjection\ContainerBuilder;

trait InitsKernel
{
protected function getBundleClass() {
return MessengerAutoScaleBundle::class;
}

private function registerPublicServiceCompilerPass() {
$this->addCompilerPass(new PublicServicePass('/(Krak.*|krak\..*|messenger.default_serializer|message_bus)/'));
}

private function given_the_kernel_is_booted_with_config_resources(array $configResources) {
$kernel = $this->createKernel();
$kernel->addBundle(TestFixtureBundle::class);
$kernel->addBundle(MessengerRedisBundle::class);
foreach ($configResources as $config) {
$kernel->addConfigFile($config);
}
$this->bootKernel();
}

private function given_the_kernel_is_booted_with_messenger_and_auto_scale_config() {
$this->given_the_kernel_is_booted_with_config_resources([
__DIR__ . '/../Fixtures/messenger-config.yaml',
__DIR__ . '/../Fixtures/auto-scale-config.yaml',
]);
}
}
66 changes: 66 additions & 0 deletions tests/Feature/Bundle/MustMatchAllReceiversTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace Krak\SymfonyMessengerAutoScale\Tests\Feature\Bundle;

use Nyholm\BundleTest\BaseBundleTestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;

final class MustMatchAllReceiversTest extends BaseBundleTestCase
{
use InitsKernel;

/** @var \Throwable|null $e */
private $exception;
private $configs;


protected function setUp() {
parent::setUp();
$this->registerPublicServiceCompilerPass();
}

/** @test */
public function throws_exception_if_not_all_receivers_are_matched() {
$this->given_the_config_where_not_all_receivers_are_set_is_available();
$this->when_the_kernel_is_booted();
$this->then_the_not_matched_logic_exception_is_thrown();
}

/** @test */
public function can_disable_must_match_all_receivers_flag() {
$this->given_the_config_where_not_all_receivers_are_set_flag_is_disabled();
$this->when_the_kernel_is_booted();
$this->then_no_exception_is_thrown();
}

private function given_the_config_where_not_all_receivers_are_set_is_available() {
$this->configs = [
__DIR__ . '/../Fixtures/messenger-config.yaml',
__DIR__ . '/../Fixtures/auto-scale-config-with-missing-receivers.yaml',
];
}

private function given_the_config_where_not_all_receivers_are_set_flag_is_disabled() {
$this->configs = [
__DIR__ . '/../Fixtures/messenger-config.yaml',
__DIR__ . '/../Fixtures/auto-scale-config-with-missing-receivers-disabled.yaml'
];
}

private function when_the_kernel_is_booted() {
try {
$this->given_the_kernel_is_booted_with_config_resources($this->configs);
} catch (\Throwable $e) {
$this->exception = $e;
}
}

private function then_the_not_matched_logic_exception_is_thrown() {
$this->assertInstanceOf(\LogicException::class, $this->exception);
$this->assertEquals('Some receivers were not matched by the pool config: catalog', $this->exception->getMessage());
}

private function then_no_exception_is_thrown() {
$this->assertNull($this->exception);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
messenger_auto_scale:
must_match_all_receivers: false
pools:
sales:
receivers: "sales*"
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
messenger_auto_scale:
pools:
sales:
receivers: "sales*"

0 comments on commit bb4b1e6

Please sign in to comment.