-
Notifications
You must be signed in to change notification settings - Fork 442
Amazon SNS transport. Publish-Subscribe support based on combination of SQS and SNQ #672
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
2af7a2d
Amazon SNS transport.
makasim a35a323
senses
ASKozienko 159de19
snsqs tests
ASKozienko 8c0e3c6
snsqs client driver
ASKozienko d9fa5f4
snsqs client driver
ASKozienko 57a8896
Merge remote-tracking branch 'origin/master' into sns
ASKozienko 7e0767e
snsqs client driver
ASKozienko b0e3da4
snsqs fix typo
ASKozienko 3f043ad
snsqs fix tests
ASKozienko 905c471
snsqs docs
ASKozienko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,6 +58,8 @@ remote rdkafka [email protected]:php-enqueue/rdkafka.git | |
remote dbal [email protected]:php-enqueue/dbal.git | ||
remote null [email protected]:php-enqueue/null.git | ||
remote sqs [email protected]:php-enqueue/sqs.git | ||
remote sns [email protected]:php-enqueue/sns.git | ||
remote snsqs [email protected]:php-enqueue/snsqs.git | ||
remote gps [email protected]:php-enqueue/gps.git | ||
remote enqueue-bundle [email protected]:php-enqueue/enqueue-bundle.git | ||
remote job-queue [email protected]:php-enqueue/job-queue.git | ||
|
@@ -84,6 +86,8 @@ split 'pkg/redis' redis | |
split 'pkg/dbal' dbal | ||
split 'pkg/null' null | ||
split 'pkg/sqs' sqs | ||
split 'pkg/sns' sns | ||
split 'pkg/snsqs' snsqs | ||
split 'pkg/gps' gps | ||
split 'pkg/enqueue-bundle' enqueue-bundle | ||
split 'pkg/job-queue' job-queue | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
<h2 align="center">Supporting Enqueue</h2> | ||
|
||
Enqueue is an MIT-licensed open source project with its ongoing development made possible entirely by the support of community and our customers. If you'd like to join them, please consider: | ||
|
||
- [Become a sponsor](https://www.patreon.com/makasim) | ||
- [Become our client](http://forma-pro.com/) | ||
|
||
--- | ||
|
||
# Amazon SNS-SQS transport | ||
|
||
Utilize two Amazon services [SNS-SQS](https://docs.aws.amazon.com/sns/latest/dg/sns-sqs-as-subscriber.html) to | ||
implement [Publish-Subscribe](https://www.enterpriseintegrationpatterns.com/patterns/messaging/PublishSubscribeChannel.html) | ||
enterprise integration pattern. As opposed to single SQS transport this adds ability to use [MessageBus](https://www.enterpriseintegrationpatterns.com/patterns/messaging/MessageBus.html) | ||
with enqueue. | ||
|
||
A transport for [Amazon SQS](https://aws.amazon.com/sqs/) broker. | ||
It uses internally official [aws sdk library](https://packagist.org/packages/aws/aws-sdk-php) | ||
|
||
* [Installation](#installation) | ||
* [Create context](#create-context) | ||
* [Declare topic, queue and bind them together](#declare-topic-queue-and-bind-them-together) | ||
* [Send message to topic](#send-message-to-topic) | ||
* [Send message to queue](#send-message-to-queue) | ||
* [Consume message](#consume-message) | ||
* [Purge queue messages](#purge-queue-messages) | ||
* [Queue from another AWS account](#queue-from-another-aws-account) | ||
|
||
## Installation | ||
|
||
```bash | ||
$ composer require enqueue/sqs | ||
``` | ||
|
||
## Create context | ||
|
||
```php | ||
<?php | ||
use Enqueue\SnsQs\SnsQsConnectionFactory; | ||
|
||
$factory = new SnsQsConnectionFactory([ | ||
'key' => 'aKey', | ||
'secret' => 'aSecret', | ||
'region' => 'aRegion', | ||
|
||
// or you can segregate options using prefixes "sns_", "sqs_" | ||
'key' => 'aKey', // common option for both SNS and SQS | ||
'sns_region' => 'aSnsRegion', // SNS transport option | ||
'sqs_region' => 'aSqsRegion', // SQS transport option | ||
]); | ||
|
||
// same as above but given as DSN string. You may need to url encode secret if it contains special char (like +) | ||
$factory = new SnsQsConnectionFactory('snsqs:?key=aKey&secret=aSecret®ion=aRegion'); | ||
|
||
$context = $factory->createContext(); | ||
|
||
// if you have enqueue/enqueue library installed you can use a factory to build context from DSN | ||
$context = (new \Enqueue\ConnectionFactoryFactory())->create('snsqs:')->createContext(); | ||
``` | ||
|
||
## Declare topic, queue and bind them together | ||
|
||
Declare topic, queue operation creates a topic, queue on a broker side. | ||
Bind creates connection between topic and queue. You publish message to | ||
the topic and topic sends message to each queue connected to the topic. | ||
|
||
|
||
```php | ||
<?php | ||
/** @var \Enqueue\SnsQs\SnsQsContext $context */ | ||
|
||
$inTopic = $context->createTopic('in'); | ||
$context->declareTopic($inTopic); | ||
|
||
$out1Queue = $context->createQueue('out1'); | ||
$context->declareQueue($out1Queue); | ||
|
||
$out2Queue = $context->createQueue('out2'); | ||
$context->declareQueue($out2Queue); | ||
|
||
$context->bind($inTopic, $out1Queue); | ||
$context->bind($inTopic, $out2Queue); | ||
|
||
// to remove topic/queue use deleteTopic/deleteQueue method | ||
//$context->deleteTopic($inTopic); | ||
//$context->deleteQueue($out1Queue); | ||
//$context->unbind(inTopic, $out1Queue); | ||
``` | ||
|
||
## Send message to topic | ||
|
||
```php | ||
<?php | ||
/** @var \Enqueue\SnsQs\SnsQsContext $context */ | ||
|
||
$inTopic = $context->createTopic('in'); | ||
$message = $context->createMessage('Hello world!'); | ||
|
||
$context->createProducer()->send($inTopic, $message); | ||
``` | ||
|
||
## Send message to queue | ||
|
||
You can bypass topic and publish message directly to the queue | ||
|
||
```php | ||
<?php | ||
/** @var \Enqueue\SnsQs\SnsQsContext $context */ | ||
|
||
$fooQueue = $context->createQueue('foo'); | ||
$message = $context->createMessage('Hello world!'); | ||
|
||
$context->createProducer()->send($fooQueue, $message); | ||
``` | ||
|
||
|
||
## Consume message: | ||
|
||
```php | ||
<?php | ||
/** @var \Enqueue\SnsQs\SnsQsContext $context */ | ||
|
||
$out1Queue = $context->createQueue('out1'); | ||
$consumer = $context->createConsumer($out1Queue); | ||
|
||
$message = $consumer->receive(); | ||
|
||
// process a message | ||
|
||
$consumer->acknowledge($message); | ||
// $consumer->reject($message); | ||
``` | ||
|
||
## Purge queue messages: | ||
|
||
```php | ||
<?php | ||
/** @var \Enqueue\SnsQs\SnsQsContext $context */ | ||
|
||
$fooQueue = $context->createQueue('foo'); | ||
|
||
$context->purgeQueue($fooQueue); | ||
``` | ||
|
||
## Queue from another AWS account | ||
|
||
SQS allows to use queues from another account. You could set it globally for all queues via option `queue_owner_aws_account_id` or | ||
per queue using `SnsQsQueue::setQueueOwnerAWSAccountId` method. | ||
|
||
```php | ||
<?php | ||
use Enqueue\SnsQs\SnsQsConnectionFactory; | ||
|
||
// globally for all queues | ||
$factory = new SnsQsConnectionFactory('snsqs:?sqs_queue_owner_aws_account_id=awsAccountId'); | ||
|
||
$context = (new SnsQsConnectionFactory('snsqs:'))->createContext(); | ||
|
||
// per queue. | ||
$queue = $context->createQueue('foo'); | ||
$queue->setQueueOwnerAWSAccountId('awsAccountId'); | ||
``` | ||
|
||
## Multi region examples | ||
|
||
Enqueue SNSQS provides a generic multi-region support. This enables users to specify which AWS Region to send a command to by setting region on SnsQsQueue. | ||
If not specified the default region is used. | ||
|
||
```php | ||
<?php | ||
use Enqueue\SnsQs\SnsQsConnectionFactory; | ||
|
||
$context = (new SnsQsConnectionFactory('snsqs:?region=eu-west-2'))->createContext(); | ||
|
||
$queue = $context->createQueue('foo'); | ||
$queue->setRegion('us-west-2'); | ||
|
||
// the request goes to US West (Oregon) Region | ||
$context->declareQueue($queue); | ||
``` | ||
|
||
[back to index](../index.md) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?php | ||
|
||
namespace Enqueue\Client\Driver; | ||
|
||
use Enqueue\SnsQs\SnsQsContext; | ||
use Enqueue\SnsQs\SnsQsTopic; | ||
use Interop\Queue\Destination; | ||
use Psr\Log\LoggerInterface; | ||
use Psr\Log\NullLogger; | ||
|
||
/** | ||
* @method SnsQsContext getContext() | ||
* @method SnsQsTopic createRouterTopic() | ||
*/ | ||
class SnsQsDriver extends GenericDriver | ||
{ | ||
public function __construct(SnsQsContext $context, ...$args) | ||
{ | ||
parent::__construct($context, ...$args); | ||
} | ||
|
||
public function setupBroker(LoggerInterface $logger = null): void | ||
{ | ||
$logger = $logger ?: new NullLogger(); | ||
$log = function ($text, ...$args) use ($logger) { | ||
$logger->debug(sprintf('[SqsQsDriver] '.$text, ...$args)); | ||
}; | ||
|
||
// setup router | ||
$routerTopic = $this->createRouterTopic(); | ||
$log('Declare router topic: %s', $routerTopic->getTopicName()); | ||
$this->getContext()->declareTopic($routerTopic); | ||
|
||
$routerQueue = $this->createQueue($this->getConfig()->getRouterQueue()); | ||
$log('Declare router queue: %s', $routerQueue->getQueueName()); | ||
$this->getContext()->declareQueue($routerQueue); | ||
|
||
$log('Bind router queue to topic: %s -> %s', $routerQueue->getQueueName(), $routerTopic->getTopicName()); | ||
$this->getContext()->bind($routerTopic, $routerQueue); | ||
|
||
// setup queues | ||
$declaredQueues = []; | ||
$declaredTopics = []; | ||
foreach ($this->getRouteCollection()->all() as $route) { | ||
$queue = $this->createRouteQueue($route); | ||
if (false === array_key_exists($queue->getQueueName(), $declaredQueues)) { | ||
$log('Declare processor queue: %s', $queue->getQueueName()); | ||
$this->getContext()->declareQueue($queue); | ||
|
||
$declaredQueues[$queue->getQueueName()] = true; | ||
} | ||
|
||
if ($route->isCommand()) { | ||
continue; | ||
} | ||
|
||
$topic = $this->doCreateTopic($this->createTransportQueueName($route->getSource(), true)); | ||
if (false === array_key_exists($topic->getTopicName(), $declaredTopics)) { | ||
$log('Declare processor topic: %s', $topic->getTopicName()); | ||
$this->getContext()->declareTopic($topic); | ||
|
||
$declaredTopics[$topic->getTopicName()] = true; | ||
} | ||
|
||
$log('Bind processor queue to topic: %s -> %s', $queue->getQueueName(), $topic->getTopicName()); | ||
$this->getContext()->bind($topic, $queue); | ||
} | ||
} | ||
|
||
protected function createRouterTopic(): Destination | ||
{ | ||
return $this->doCreateTopic( | ||
$this->createTransportRouterTopicName($this->getConfig()->getRouterTopic(), true) | ||
); | ||
} | ||
|
||
protected function createTransportRouterTopicName(string $name, bool $prefix): string | ||
{ | ||
$name = parent::createTransportRouterTopicName($name, $prefix); | ||
|
||
return str_replace('.', '_dot_', $name); | ||
} | ||
|
||
protected function createTransportQueueName(string $name, bool $prefix): string | ||
{ | ||
$name = parent::createTransportQueueName($name, $prefix); | ||
|
||
return str_replace('.', '_dot_', $name); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.