Skip to content
This repository has been archived by the owner on May 10, 2022. It is now read-only.

Commit

Permalink
Support native SQS URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
mnapoli committed Dec 5, 2019
1 parent 0862213 commit 85918ef
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 20 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,9 @@ Here, the `MyMessage` class will be dispatch to the `async` transport. We can no
To do that, let's configure the `MESSENGER_TRANSPORT_DSN` environment variable to contain the URL of the queue:

```dotenv
MESSENGER_TRANSPORT_DSN=sqs://sqs.us-east-1.amazonaws.com/123456789101/my-queue
MESSENGER_TRANSPORT_DSN=https://sqs.us-east-1.amazonaws.com/123456789101/my-queue
```

**Watch out:** the SQS URL _must start_ with `sqs://` instead of `https://`. This prefix is the way Symfony Messenger works. That means that you must replace the beginning of the SQS URL that AWS will give you.

### Sending messages

Now that Messenger is configured with SQS, we can send messages using the `MessageBusInterface`. For example, in a controller:
Expand Down
14 changes: 2 additions & 12 deletions src/Sqs/SqsTransportFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,11 @@ public function __construct(SqsClient $sqs, ?SerializerInterface $serializer = n

public function createTransport(string $dsn, array $options, SerializerInterface $serializer): TransportInterface
{
return new SqsTransport($this->sqs, $this->serializer, $this->dsnToQueueUrl($dsn));
return new SqsTransport($this->sqs, $this->serializer, $dsn);
}

public function supports(string $dsn, array $options): bool
{
return strpos($dsn, 'sqs://') === 0;
}

/**
* The Symfony Messenger DSN for the queue must start with `sqs://` so that the bundle can recognize it
* as a SQS queue URL.
* However we need to turn it into a real HTTPS URL to be able to use it (else it's not a valid URL).
*/
private function dsnToQueueUrl(string $dsn): string
{
return str_replace('sqs://', 'https://', $dsn);
return preg_match('#^https://sqs\.[\w\-]+\.amazonaws\.com/.+#', $dsn) === 1;
}
}
Empty file removed tests/.gitkeep
Empty file.
8 changes: 4 additions & 4 deletions tests/FunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ public function test factory(): void
$factory = $this->container->get(SqsTransportFactory::class);
$this->assertInstanceOf(SqsTransportFactory::class, $factory);

$this->assertTrue($factory->supports('sqs://sqs.us-east-1.amazonaws.com/123456789101/test', []));
$this->assertFalse($factory->supports('https://sqs.us-east-1.amazonaws.com/123456789101/test', []));
$this->assertTrue($factory->supports('https://sqs.us-east-1.amazonaws.com/123456789101/test', []));
$this->assertFalse($factory->supports('https://example.com', []));

$transport = $factory->createTransport('sqs://sqs.us-east-1.amazonaws.com/123456789101/test', [], new PhpSerializer);
$transport = $factory->createTransport('https://sqs.us-east-1.amazonaws.com/123456789101/test', [], new PhpSerializer);
$this->assertInstanceOf(SqsTransport::class, $transport);
}

Expand All @@ -56,7 +56,7 @@ public function test send message(): void
$bus = $this->container->get(MessageBusInterface::class);
$bus->dispatch(new TestMessage('hello'));

// Check that the URL has been transformed in a HTTPS URL
// Check that the URL is correct
$this->assertEquals('https://sqs.us-east-1.amazonaws.com/123456789101/bref-test', $queueUrl);
}
}
2 changes: 1 addition & 1 deletion tests/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ framework:

messenger:
transports:
foo: 'sqs://sqs.us-east-1.amazonaws.com/123456789101/bref-test'
foo: 'https://sqs.us-east-1.amazonaws.com/123456789101/bref-test'
routing:
'Bref\Messenger\Test\TestMessage\TestMessage': foo

Expand Down

0 comments on commit 85918ef

Please sign in to comment.