Skip to content

Commit

Permalink
Merge pull request #57 from jeskew/feature/named-providers
Browse files Browse the repository at this point in the history
Feature/named providers
  • Loading branch information
k-k committed Dec 17, 2014
2 parents d6a0e38 + 34aff09 commit 9f70222
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 51 deletions.
5 changes: 3 additions & 2 deletions docs/aws-provider.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@ credentials in your configuration.
uecode_qpush:
providers:
aws:
my_provider:
driver: aws
key: <aws key>
secret: <aws secret>
region: us-east-1
queues:
my_queue_name:
provider: aws
provider: my_provider
options:
push_notifications: true
subscribers:
Expand Down
11 changes: 10 additions & 1 deletion docs/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,24 @@ A working configuration would look like the following
logging_enabled: true
providers:
aws:
driver: aws #optional for providers named 'aws' or 'ironmq'
key: YOUR_AWS_KEY_HERE
secret: YOUR_AWS_SECRET_HERE
region: YOUR_AWS_REGION_HERE
another_aws_provider:
driver: aws #required for named providers
key: YOUR_AWS_KEY_HERE
secret: YOUR_AWS_SECRET_HERE
region: YOUR_AWS_REGION_HERE
ironmq:
driver: aws #optional for providers named 'aws' or 'ironmq'
token: YOUR_IRONMQ_TOKEN_HERE
project_id: YOUR_IRONMQ_PROJECT_ID_HERE
in_band:
driver: sync
queues:
my_queue_key:
provider: ironmq #or aws or sync
provider: ironmq #or aws or in_band or another_aws_provider
options:
queue_name: my_actual_queue_name
push_notifications: true
Expand Down
7 changes: 4 additions & 3 deletions docs/iron-mq-provider.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,24 @@ an account and have a project id.
testing and using this service extremely easy.

Just include your OAuth `token` and `project_id` in the configuration and set your
queue to use the `ironmq` provider.
queue to use a provider using the `ironmq` driver.

.. code-block:: yaml
#app/config.yml
uecode_qpush:
providers:
ironmq:
my_provider:
driver: ironmq
token: YOUR_TOKEN_HERE
project_id: YOUR_PROJECT_ID_HERE
host: YOUR_OPTIONAL_HOST_HERE
port: YOUR_OPTIONAL_PORT_HERE
version_id: YOUR_OPTIONAL_VERSION_HERE
queues:
my_queue_name:
provider: ironmq
provider: my_provider
options:
push_notifications: true
subscribers:
Expand Down
7 changes: 5 additions & 2 deletions docs/sync-provider.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@ of queue-based code paths.
Configuration
^^^^^^^^^^^^^

To use the sync queue, set the ``provider`` of a given queue to ``sync``. No further
To designate a queue as synchronous, set the ``driver`` of its provider to ``sync``. No further
configuration is necessary.

.. code-block:: yaml
#app/config_dev.yml
uecode_qpush:
providers:
in_band:
driver: sync
queues:
my_queue_name:
provider: sync
provider: in_band
77 changes: 49 additions & 28 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,39 +56,60 @@ private function getProvidersNode()
{
$treeBuilder = new TreeBuilder();
$node = $treeBuilder->root('providers');
$requirements = [
'aws' => ['key', 'secret'],
'ironmq' => ['token', 'project_id'],
'sync' => [],
];

$node
->children()
->arrayNode('aws')
->children()
->scalarNode('key')->end()
->scalarNode('secret')->end()
->scalarNode('region')
->defaultValue('us-east-1')
->end()
->useAttributeAsKey('name')
->prototype('array')
->treatNullLike([])
->children()
->enumNode('driver')
->isRequired()
->values(array_keys($requirements))
->end()
->end()
->arrayNode('ironmq')
->children()
->scalarNode('token')->end()
->scalarNode('project_id')->end()
->enumNode('host')
->defaultValue('mq-aws-us-east-1')
->values([
'mq-aws-us-east-1',
'mq-aws-eu-west-1',
'mq-rackspace-ord',
'mq-rackspace-lon'
])
->end()
->scalarNode('port')
->defaultValue('443')
->end()
->scalarNode('api_version')
->defaultValue(1)
->end()
// IronMQ
->scalarNode('token')->end()
->scalarNode('project_id')->end()
->enumNode('host')
->defaultValue('mq-aws-us-east-1')
->values([
'mq-aws-us-east-1',
'mq-aws-eu-west-1',
'mq-rackspace-ord',
'mq-rackspace-lon',
])
->end()
->scalarNode('port')
->defaultValue('443')
->end()
->scalarNode('api_version')
->defaultValue(1)
->end()
// AWS
->scalarNode('key')->end()
->scalarNode('secret')->end()
->scalarNode('region')
->defaultValue('us-east-1')
->end()
->end()

->validate()
->always()
->then(function (array $provider) use ($node, $requirements) {
foreach ($requirements[$provider['driver']] as $requirement) {
if (empty($provider[$requirement])) {
throw new \InvalidArgumentException(
sprintf('%s queue providers must have a %s; none provided', $provider['driver'], $requirement)
);
}
}

return $provider;
})
->end()
;

Expand Down
34 changes: 21 additions & 13 deletions src/DependencyInjection/UecodeQPushExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,21 @@ public function load(array $configs, ContainerBuilder $container)
$class = null;
$client = null;

switch ($provider) {
switch ($config['providers'][$provider]['driver']) {
case 'aws':
$class = $container->getParameter('uecode_qpush.provider.aws');
$client = $this->createAwsClient(
$config['providers'][$provider],
$container
$container,
$provider
);
break;
case 'ironmq':
$class = $container->getParameter('uecode_qpush.provider.ironmq');
$client = $this->createIronMQClient(
$config['providers'][$provider],
$container
$container,
$provider
);
break;
case 'sync':
Expand Down Expand Up @@ -116,12 +118,15 @@ public function load(array $configs, ContainerBuilder $container)
*
* @param array $config A Configuration array for the client
* @param ContainerBuilder $container The container
* @param string $name The provider key
*
* return Reference
* @return Reference
*/
private function createAwsClient($config, ContainerBuilder $container)
private function createAwsClient($config, ContainerBuilder $container, $name)
{
if (!$container->hasDefinition('uecode_qpush.provider.aws')) {
$service = sprintf('uecode_qpush.provider.%s', $name);

if (!$container->hasDefinition($service)) {

if (!class_exists('Aws\Common\Aws')) {
throw new \RuntimeException(
Expand All @@ -140,24 +145,27 @@ private function createAwsClient($config, ContainerBuilder $container)
]
]);

$container->setDefinition('uecode_qpush.provider.aws', $aws)
$container->setDefinition($service, $aws)
->setPublic(false);
}

return new Reference('uecode_qpush.provider.aws');
return new Reference($service);
}

/**
* Creates a definition for the IronMQ provider
*
* @param array $config A Configuration array for the provider
* @param ContainerBuilder $container The container
* @param string $name The provider key
*
* return Reference
* @return Reference
*/
private function createIronMQClient($config, ContainerBuilder $container)
private function createIronMQClient($config, ContainerBuilder $container, $name)
{
if (!$container->hasDefinition('uecode_qpush.provider.ironmq')) {
$service = sprintf('uecode_qpush.provider.%s', $name);

if (!$container->hasDefinition($service)) {

if (!class_exists('IronMQ')) {
throw new \RuntimeException(
Expand All @@ -176,11 +184,11 @@ private function createIronMQClient($config, ContainerBuilder $container)
]
]);

$container->setDefinition('uecode_qpush.provider.ironmq', $ironmq)
$container->setDefinition($service, $ironmq)
->setPublic(false);
}

return new Reference('uecode_qpush.provider.ironmq');
return new Reference($service);
}

private function createSyncClient()
Expand Down
12 changes: 12 additions & 0 deletions tests/DependencyInjection/UecodeQPushExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,19 @@ public function testConfiguration()
$this->container->compile();

$this->assertTrue($this->container->has('uecode_qpush'));

$this->assertTrue($this->container->has('uecode_qpush.test_aws'));
$this->assertTrue($this->container->has('uecode_qpush.test_secondary_aws'));
$this->assertNotSame(
$this->container->get('uecode_qpush.test_aws'),
$this->container->get('uecode_qpush.test_secondary_aws')
);

$this->assertTrue($this->container->has('uecode_qpush.test_ironmq'));
$this->assertTrue($this->container->has('uecode_qpush.test_secondary_ironmq'));
$this->assertNotSame(
$this->container->get('uecode_qpush.test_ironmq'),
$this->container->get('uecode_qpush.test_secondary_ironmq')
);
}
}
41 changes: 39 additions & 2 deletions tests/Fixtures/config_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,23 @@ uecode_qpush:
logging_enabled: true
providers:
aws:
driver: aws
key: 123
secret: 123
region: us-east-1
secondary_aws:
driver: aws
key: 234
secret: 432
region: eu-west-1
ironmq:
driver: ironmq
token: 123
project_id: 123
secondary_ironmq:
driver: ironmq
token: 234
project_id: 234
queues:
test_aws:
provider: aws
Expand All @@ -22,6 +33,18 @@ uecode_qpush:
receive_wait_time: 3
subscribers:
- { endpoint: http://example.com/qpush, protocol: http }
test_secondary_aws:
provider: secondary_aws
options:
push_notifications: true
notification_retries: 3
message_delay: 0
message_timeout: 30
message_expiration: 604800
messages_to_receive: 1
receive_wait_time: 3
subscribers:
- { endpoint: http://example.com/qpush, protocol: http }
test_ironmq:
provider: ironmq
options:
Expand All @@ -34,9 +57,23 @@ uecode_qpush:
receive_wait_time: 3
subscribers:
- { endpoint: http://example.com/qpush, protocol: http }
test_secondary_ironmq:
provider: secondary_ironmq
options:
push_notifications: true
notification_retries: 3
message_delay: 0
message_timeout: 30
message_expiration: 604800
messages_to_receive: 1
receive_wait_time: 3
subscribers:
- { endpoint: http://example.com/qpush, protocol: http }

services:
event_dispatcher:
class: stdObject
class: stdClass
logger:
class: stdObject
class: Symfony\Bridge\Monolog\Logger
arguments:
- 'test'

0 comments on commit 9f70222

Please sign in to comment.