From bc4e949d59874c350dbe8c4408d86f3fd6098c03 Mon Sep 17 00:00:00 2001 From: elazar Date: Sun, 13 May 2018 10:31:14 -0500 Subject: [PATCH 1/8] Add support for using a pre-configured client with the SQS driver --- docs/bundle/config_reference.md | 1 + docs/transport/sqs.md | 6 +++++- .../Tests/Functional/UseCasesTest.php | 15 +++++++++++++-- pkg/sqs/SqsConnectionFactory.php | 5 ++++- pkg/sqs/Symfony/SqsTransportFactory.php | 1 + pkg/sqs/Tests/SqsConnectionFactoryTest.php | 16 ++++++++++++++++ 6 files changed, 40 insertions(+), 4 deletions(-) diff --git a/docs/bundle/config_reference.md b/docs/bundle/config_reference.md index 7b54dd23f..03290a602 100644 --- a/docs/bundle/config_reference.md +++ b/docs/bundle/config_reference.md @@ -228,6 +228,7 @@ enqueue: polling_interval: 1000 lazy: true sqs: + client: null key: null secret: null token: null diff --git a/docs/transport/sqs.md b/docs/transport/sqs.md index 03da0ebeb..a862f5382 100644 --- a/docs/transport/sqs.md +++ b/docs/transport/sqs.md @@ -34,6 +34,10 @@ $factory = new SqsConnectionFactory('sqs:?key=aKey&secret=aSecret®ion=aRegion $psrContext = $factory->createContext(); +// using a pre-configured client +$client = new Aws\Sqs\SqsClient([ /* ... */ ]); +$factory = new SqsConnectionFactory(['client' => $client]); + // if you have enqueue/enqueue library installed you can use a function from there to create the context $psrContext = \Enqueue\dsn_to_context('sqs:'); ``` @@ -109,4 +113,4 @@ $fooQueue = $psrContext->createQueue('foo'); $psrContext->purge($fooQueue); ``` -[back to index](../index.md) \ No newline at end of file +[back to index](../index.md) diff --git a/pkg/enqueue-bundle/Tests/Functional/UseCasesTest.php b/pkg/enqueue-bundle/Tests/Functional/UseCasesTest.php index 34adc2c68..d1286130d 100644 --- a/pkg/enqueue-bundle/Tests/Functional/UseCasesTest.php +++ b/pkg/enqueue-bundle/Tests/Functional/UseCasesTest.php @@ -51,7 +51,7 @@ public function provideEnqueueConfigs() $certDir = $baseDir.'/var/rabbitmq_certificates'; $this->assertDirectoryExists($certDir); - +/* yield 'amqp' => [[ 'transport' => [ 'default' => 'amqp', @@ -204,6 +204,17 @@ public function provideEnqueueConfigs() ], ], ]]; +*/ + + yield 'sqs_client' => [[ + 'transport' => [ + 'default' => 'sqs', + 'sqs' => [ + 'client' => '@Aws\Sqs\SqsClient', + ], + ], + ]]; +/* } yield 'mongodb_dsn' => [[ @@ -212,7 +223,7 @@ public function provideEnqueueConfigs() 'mongodb' => getenv('MONGO_DSN'), ], ]]; - + */ // yield 'gps' => [[ // 'transport' => [ // 'default' => 'gps', diff --git a/pkg/sqs/SqsConnectionFactory.php b/pkg/sqs/SqsConnectionFactory.php index 38006c097..67f14a720 100644 --- a/pkg/sqs/SqsConnectionFactory.php +++ b/pkg/sqs/SqsConnectionFactory.php @@ -19,6 +19,7 @@ class SqsConnectionFactory implements PsrConnectionFactory /** * $config = [ + * 'client' => null, - Pre-configured instance of Aws\Sqs\SqsClient. If provided, all other settings except for 'lazy' are ignored. * 'key' => null - AWS credentials. If no credentials are provided, the SDK will attempt to load them from the environment. * 'secret' => null, - AWS credentials. If no credentials are provided, the SDK will attempt to load them from the environment. * 'token' => null, - AWS credentials. If no credentials are provided, the SDK will attempt to load them from the environment. @@ -38,7 +39,9 @@ class SqsConnectionFactory implements PsrConnectionFactory */ public function __construct($config = 'sqs:') { - if (empty($config) || 'sqs:' === $config) { + if (is_array($config) && isset($config['client']) && $config['client'] instanceof SqsClient) { + $this->client = $config['client']; + } elseif (empty($config) || 'sqs:' === $config) { $config = []; } elseif (is_string($config)) { $config = $this->parseDsn($config); diff --git a/pkg/sqs/Symfony/SqsTransportFactory.php b/pkg/sqs/Symfony/SqsTransportFactory.php index 58c0c1a12..dfd9ea0b5 100644 --- a/pkg/sqs/Symfony/SqsTransportFactory.php +++ b/pkg/sqs/Symfony/SqsTransportFactory.php @@ -34,6 +34,7 @@ public function addConfiguration(ArrayNodeDefinition $builder) { $builder ->children() + ->scalarNode('client')->defaultNull()->end() ->scalarNode('key')->defaultNull()->end() ->scalarNode('secret')->defaultNull()->end() ->scalarNode('token')->defaultNull()->end() diff --git a/pkg/sqs/Tests/SqsConnectionFactoryTest.php b/pkg/sqs/Tests/SqsConnectionFactoryTest.php index d10447c04..6c28d4a99 100644 --- a/pkg/sqs/Tests/SqsConnectionFactoryTest.php +++ b/pkg/sqs/Tests/SqsConnectionFactoryTest.php @@ -2,6 +2,7 @@ namespace Enqueue\Sqs\Tests; +use Aws\Sqs\SqsClient; use Enqueue\Sqs\SqsConnectionFactory; use Enqueue\Sqs\SqsContext; use Enqueue\Test\ClassExtensionTrait; @@ -48,6 +49,21 @@ public function testCouldBeConstructedWithCustomConfiguration() ], 'config', $factory); } + public function testCouldBeConstructedWithClient() + { + $client = $this->createMock(SqsClient::class); + + $factory = new SqsConnectionFactory([ + 'client' => $client, + 'lazy' => false, + ]); + + $context = $factory->createContext(); + + $this->assertInstanceOf(SqsContext::class, $context); + $this->assertAttributeSame($client, 'client', $context); + } + public function testShouldCreateLazyContext() { $factory = new SqsConnectionFactory(['lazy' => true]); From b2e961316d332c82b07b4b59b9f2e8f4858e1750 Mon Sep 17 00:00:00 2001 From: elazar Date: Mon, 14 May 2018 07:29:14 -0500 Subject: [PATCH 2/8] Move client passed to SqsConnectionFactory out of config array --- docs/transport/sqs.md | 2 +- pkg/sqs/SqsConnectionFactory.php | 7 +++---- pkg/sqs/Tests/SqsConnectionFactoryTest.php | 5 +---- 3 files changed, 5 insertions(+), 9 deletions(-) diff --git a/docs/transport/sqs.md b/docs/transport/sqs.md index a862f5382..9c688f8ec 100644 --- a/docs/transport/sqs.md +++ b/docs/transport/sqs.md @@ -36,7 +36,7 @@ $psrContext = $factory->createContext(); // using a pre-configured client $client = new Aws\Sqs\SqsClient([ /* ... */ ]); -$factory = new SqsConnectionFactory(['client' => $client]); +$factory = new SqsConnectionFactory($client); // if you have enqueue/enqueue library installed you can use a function from there to create the context $psrContext = \Enqueue\dsn_to_context('sqs:'); diff --git a/pkg/sqs/SqsConnectionFactory.php b/pkg/sqs/SqsConnectionFactory.php index 67f14a720..b596dc6a2 100644 --- a/pkg/sqs/SqsConnectionFactory.php +++ b/pkg/sqs/SqsConnectionFactory.php @@ -19,7 +19,6 @@ class SqsConnectionFactory implements PsrConnectionFactory /** * $config = [ - * 'client' => null, - Pre-configured instance of Aws\Sqs\SqsClient. If provided, all other settings except for 'lazy' are ignored. * 'key' => null - AWS credentials. If no credentials are provided, the SDK will attempt to load them from the environment. * 'secret' => null, - AWS credentials. If no credentials are provided, the SDK will attempt to load them from the environment. * 'token' => null, - AWS credentials. If no credentials are provided, the SDK will attempt to load them from the environment. @@ -35,12 +34,12 @@ class SqsConnectionFactory implements PsrConnectionFactory * sqs: * sqs::?key=aKey&secret=aSecret&token=aToken * - * @param array|string|null $config + * @param array|string|SqsClient|null $config */ public function __construct($config = 'sqs:') { - if (is_array($config) && isset($config['client']) && $config['client'] instanceof SqsClient) { - $this->client = $config['client']; + if ($config instanceof SqsClient) { + return $this->client = $config; } elseif (empty($config) || 'sqs:' === $config) { $config = []; } elseif (is_string($config)) { diff --git a/pkg/sqs/Tests/SqsConnectionFactoryTest.php b/pkg/sqs/Tests/SqsConnectionFactoryTest.php index 6c28d4a99..aa3cf88de 100644 --- a/pkg/sqs/Tests/SqsConnectionFactoryTest.php +++ b/pkg/sqs/Tests/SqsConnectionFactoryTest.php @@ -53,10 +53,7 @@ public function testCouldBeConstructedWithClient() { $client = $this->createMock(SqsClient::class); - $factory = new SqsConnectionFactory([ - 'client' => $client, - 'lazy' => false, - ]); + $factory = new SqsConnectionFactory($client); $context = $factory->createContext(); From a137a61e25dd3db8d88bb406fc1427b435a5a757 Mon Sep 17 00:00:00 2001 From: Remon van de Kamp Date: Sun, 8 Jul 2018 15:46:33 +0200 Subject: [PATCH 3/8] Use client defined in configuration in SqsConnectionFactory --- .../Tests/Functional/App/config/custom-config.yml | 11 +++++++++++ .../Tests/Functional/UseCasesTest.php | 2 +- pkg/sqs/SqsConnectionFactory.php | 5 ++++- pkg/sqs/Symfony/SqsTransportFactory.php | 14 ++++++++++++-- 4 files changed, 28 insertions(+), 4 deletions(-) diff --git a/pkg/enqueue-bundle/Tests/Functional/App/config/custom-config.yml b/pkg/enqueue-bundle/Tests/Functional/App/config/custom-config.yml index 9bcc45a30..4615b5873 100644 --- a/pkg/enqueue-bundle/Tests/Functional/App/config/custom-config.yml +++ b/pkg/enqueue-bundle/Tests/Functional/App/config/custom-config.yml @@ -33,3 +33,14 @@ services: public: true tags: - { name: 'enqueue.client.processor' } + + test.sqs_client: + public: true + class: Aws\Sqs\SqsClient + arguments: + - + region: '%env(AWS_SQS_REGION)%' + version: '2012-11-05' + credentials: + key: '%env(AWS_SQS_KEY)%' + secret: '%env(AWS_SQS_SECRET)%' diff --git a/pkg/enqueue-bundle/Tests/Functional/UseCasesTest.php b/pkg/enqueue-bundle/Tests/Functional/UseCasesTest.php index d1286130d..cf81e0f8b 100644 --- a/pkg/enqueue-bundle/Tests/Functional/UseCasesTest.php +++ b/pkg/enqueue-bundle/Tests/Functional/UseCasesTest.php @@ -210,7 +210,7 @@ public function provideEnqueueConfigs() 'transport' => [ 'default' => 'sqs', 'sqs' => [ - 'client' => '@Aws\Sqs\SqsClient', + 'client' => 'test.sqs_client', ], ], ]]; diff --git a/pkg/sqs/SqsConnectionFactory.php b/pkg/sqs/SqsConnectionFactory.php index b596dc6a2..7c487969d 100644 --- a/pkg/sqs/SqsConnectionFactory.php +++ b/pkg/sqs/SqsConnectionFactory.php @@ -39,7 +39,10 @@ class SqsConnectionFactory implements PsrConnectionFactory public function __construct($config = 'sqs:') { if ($config instanceof SqsClient) { - return $this->client = $config; + $this->client = $config; + $this->config = $this->defaultConfig(); + + return; } elseif (empty($config) || 'sqs:' === $config) { $config = []; } elseif (is_string($config)) { diff --git a/pkg/sqs/Symfony/SqsTransportFactory.php b/pkg/sqs/Symfony/SqsTransportFactory.php index dfd9ea0b5..234008532 100644 --- a/pkg/sqs/Symfony/SqsTransportFactory.php +++ b/pkg/sqs/Symfony/SqsTransportFactory.php @@ -38,7 +38,7 @@ public function addConfiguration(ArrayNodeDefinition $builder) ->scalarNode('key')->defaultNull()->end() ->scalarNode('secret')->defaultNull()->end() ->scalarNode('token')->defaultNull()->end() - ->scalarNode('region')->isRequired()->end() + ->scalarNode('region')->end() ->integerNode('retries')->defaultValue(3)->end() ->scalarNode('version')->cannotBeEmpty()->defaultValue('2012-11-05')->end() ->booleanNode('lazy') @@ -54,8 +54,18 @@ public function addConfiguration(ArrayNodeDefinition $builder) */ public function createConnectionFactory(ContainerBuilder $container, array $config) { + if (empty($config['client']) && empty($config['region'])) { + throw new RuntimeException('Either "client" or "region" must be provided to SQS in Enqueue configuration'); + } + + $arguments = $config; + + if (!empty($config['client'])) { + $arguments = new Reference($config['client']); + } + $factory = new Definition(SqsConnectionFactory::class); - $factory->setArguments([$config]); + $factory->setArguments([$arguments]); $factoryId = sprintf('enqueue.transport.%s.connection_factory', $this->getName()); $container->setDefinition($factoryId, $factory); From 9b4ccd2ebd7db1f4b7f2fe1b580a402a9acd19ab Mon Sep 17 00:00:00 2001 From: elazar Date: Sun, 8 Jul 2018 09:31:24 -0500 Subject: [PATCH 4/8] Fix SqsTransportFactory --- pkg/sqs/Symfony/SqsTransportFactory.php | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/pkg/sqs/Symfony/SqsTransportFactory.php b/pkg/sqs/Symfony/SqsTransportFactory.php index 234008532..5f68c7684 100644 --- a/pkg/sqs/Symfony/SqsTransportFactory.php +++ b/pkg/sqs/Symfony/SqsTransportFactory.php @@ -54,15 +54,7 @@ public function addConfiguration(ArrayNodeDefinition $builder) */ public function createConnectionFactory(ContainerBuilder $container, array $config) { - if (empty($config['client']) && empty($config['region'])) { - throw new RuntimeException('Either "client" or "region" must be provided to SQS in Enqueue configuration'); - } - - $arguments = $config; - - if (!empty($config['client'])) { - $arguments = new Reference($config['client']); - } + $arguments = empty($config['client']) ? $config : new Reference($config['client']); $factory = new Definition(SqsConnectionFactory::class); $factory->setArguments([$arguments]); From 0aac0041382fda74477b7159575702d03bd1c6e3 Mon Sep 17 00:00:00 2001 From: elazar Date: Sun, 8 Jul 2018 10:02:41 -0500 Subject: [PATCH 5/8] Restore use cases in UseCasesTest --- pkg/enqueue-bundle/Tests/Functional/UseCasesTest.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/pkg/enqueue-bundle/Tests/Functional/UseCasesTest.php b/pkg/enqueue-bundle/Tests/Functional/UseCasesTest.php index cf81e0f8b..2b53b16c2 100644 --- a/pkg/enqueue-bundle/Tests/Functional/UseCasesTest.php +++ b/pkg/enqueue-bundle/Tests/Functional/UseCasesTest.php @@ -51,7 +51,7 @@ public function provideEnqueueConfigs() $certDir = $baseDir.'/var/rabbitmq_certificates'; $this->assertDirectoryExists($certDir); -/* + yield 'amqp' => [[ 'transport' => [ 'default' => 'amqp', @@ -204,7 +204,6 @@ public function provideEnqueueConfigs() ], ], ]]; -*/ yield 'sqs_client' => [[ 'transport' => [ @@ -214,7 +213,6 @@ public function provideEnqueueConfigs() ], ], ]]; -/* } yield 'mongodb_dsn' => [[ @@ -223,7 +221,7 @@ public function provideEnqueueConfigs() 'mongodb' => getenv('MONGO_DSN'), ], ]]; - */ + // yield 'gps' => [[ // 'transport' => [ // 'default' => 'gps', From ce0db4713adf4bc70eff9476155a0f8b2552f9de Mon Sep 17 00:00:00 2001 From: elazar Date: Sun, 8 Jul 2018 10:24:38 -0500 Subject: [PATCH 6/8] Fix test failures --- pkg/sqs/Tests/SqsConnectionFactoryTest.php | 2 +- pkg/sqs/Tests/Symfony/SqsTransportFactoryTest.php | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/sqs/Tests/SqsConnectionFactoryTest.php b/pkg/sqs/Tests/SqsConnectionFactoryTest.php index aa3cf88de..8b1a785ca 100644 --- a/pkg/sqs/Tests/SqsConnectionFactoryTest.php +++ b/pkg/sqs/Tests/SqsConnectionFactoryTest.php @@ -53,7 +53,7 @@ public function testCouldBeConstructedWithClient() { $client = $this->createMock(SqsClient::class); - $factory = new SqsConnectionFactory($client); + $factory = new SqsConnectionFactory($client, ['lazy' => false]); $context = $factory->createContext(); diff --git a/pkg/sqs/Tests/Symfony/SqsTransportFactoryTest.php b/pkg/sqs/Tests/Symfony/SqsTransportFactoryTest.php index a263856a3..19bdb3cb8 100644 --- a/pkg/sqs/Tests/Symfony/SqsTransportFactoryTest.php +++ b/pkg/sqs/Tests/Symfony/SqsTransportFactoryTest.php @@ -64,6 +64,7 @@ public function testShouldAllowAddConfiguration() 'version' => 'theVersion', 'lazy' => false, 'endpoint' => 'theEndpoint', + 'client' => null, ], $config); } From 610add14bee9fc62e36cef91db596de67f658765 Mon Sep 17 00:00:00 2001 From: elazar Date: Sun, 8 Jul 2018 10:30:23 -0500 Subject: [PATCH 7/8] Fix SqsConnectionFactory test --- pkg/sqs/SqsConnectionFactory.php | 2 +- pkg/sqs/Tests/SqsConnectionFactoryTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/sqs/SqsConnectionFactory.php b/pkg/sqs/SqsConnectionFactory.php index 7c487969d..275752b6c 100644 --- a/pkg/sqs/SqsConnectionFactory.php +++ b/pkg/sqs/SqsConnectionFactory.php @@ -40,7 +40,7 @@ public function __construct($config = 'sqs:') { if ($config instanceof SqsClient) { $this->client = $config; - $this->config = $this->defaultConfig(); + $this->config = ['lazy' => false] + $this->defaultConfig(); return; } elseif (empty($config) || 'sqs:' === $config) { diff --git a/pkg/sqs/Tests/SqsConnectionFactoryTest.php b/pkg/sqs/Tests/SqsConnectionFactoryTest.php index 8b1a785ca..aa3cf88de 100644 --- a/pkg/sqs/Tests/SqsConnectionFactoryTest.php +++ b/pkg/sqs/Tests/SqsConnectionFactoryTest.php @@ -53,7 +53,7 @@ public function testCouldBeConstructedWithClient() { $client = $this->createMock(SqsClient::class); - $factory = new SqsConnectionFactory($client, ['lazy' => false]); + $factory = new SqsConnectionFactory($client); $context = $factory->createContext(); From df3de8c9c9b0401fdf75540c05d7fa5ceba2b5b8 Mon Sep 17 00:00:00 2001 From: elazar Date: Sun, 8 Jul 2018 11:39:53 -0500 Subject: [PATCH 8/8] Add SQS defaults to custom config --- .../Tests/Functional/App/config/custom-config.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pkg/enqueue-bundle/Tests/Functional/App/config/custom-config.yml b/pkg/enqueue-bundle/Tests/Functional/App/config/custom-config.yml index 4615b5873..ae7f57943 100644 --- a/pkg/enqueue-bundle/Tests/Functional/App/config/custom-config.yml +++ b/pkg/enqueue-bundle/Tests/Functional/App/config/custom-config.yml @@ -1,7 +1,9 @@ parameters: locale: 'en' secret: 'ThisTokenIsNotSoSecretChangeIt' - + env(AWS_SQS_REGION): 'us-east-1' + env(AWS_SQS_KEY): 'key' + env(AWS_SQS_SECRET): 'secret' framework: #esi: ~