Skip to content

Commit 7a731e5

Browse files
committed
Symfony 4.4 Upgrade
- Deleted the auto scaling code, this will be its own package - Removed unneeded abstractions that have equivalents in the core messenger package - Refactored layout of code to keep with SF conventions - Removed old tests - Added full test suite for new transport - Added support for delayed messages - Added support for unique messages Signed-off-by: RJ Garcia <[email protected]>
1 parent 7974105 commit 7a731e5

38 files changed

+652
-922
lines changed

README.md

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
# Symfony Messenger Redis Adapter
22

3-
This provides custom Redis Integration with the Symfony Messenger 4.1 system. There already exists an implementation for 4.0, but that is not compatabile with the 4.1 integration (due to Messenger's BC policy).
3+
This provides custom Redis List Integration with the Symfony Messenger ^4.4 system.
44

5-
It also includes some Enhancers for AutoScaling which provide receivers the ability to properly auto scale requests depending on metrics provided from the Queue.
5+
The standard redis implementation requires redis 5.0 and utilizes the streams feature, this adapter uses redis lists to power the queue functionality.
66

77
## Installation
88

9-
Currently, this is best installed by cloning the repo as a submodule then using [path type composer repository](https://getcomposer.org/doc/05-repositories.md#path). This is under the `krak/symfony-messenger-redis`.
10-
11-
Until this is in a composer repository, you'll probably need to manually include the bundle in your `config/bundles.php` file like so:
9+
Install with composer at `krak/symfony-messenger-redis`.
1210

11+
If symfony's composer install doesn't automatically register the bundle, you can do so manually:
1312

1413
```php
1514
<?php
@@ -37,14 +36,33 @@ Where `MESSENGER_TRANSPORT_DSN` env is like: `redis://localhost:6379`
3736

3837
This will register a transport named `acme_redis` which will properly use the configured Redis transport from this library.
3938

39+
### Unique Messages
40+
41+
If you ever need to ensure that you a specific job needs to be unique while waiting in the queue, you can use the UniqueStamp.
42+
43+
```php
44+
use Symfony\Component\Messenger\Envelope;
45+
use Krak\SymfonyMessengerRedis\Stamp\UniqueStamp;
46+
Envelope::wrap($message)->with(new UniqueStamp('optional-unique-id'));
47+
```
48+
49+
If you don't pass an identifier to enforce uniqueness, the transport will perform an md5 hash of the message body to create an identifier.
50+
51+
Example usage of this stamp:
52+
53+
- You dispatch a ProductUpdatedMessage every time a product is saved in one system.
54+
- You have message handlers that then do some expensive operation based off of that product information
55+
- In the event a single product gets saved rapidly without effectively changing, it makes no sense to enqueue the same job for that product 100 times
56+
- Using the unique stamp ensures that if a message is already in the queue for that specific product, don't add in another message since the original message hasn't been processed yet.
57+
58+
### Delayed Messages
59+
60+
This library supports the DelayStamp provided by the core SF messenger.
61+
4062
### Available Options
4163

4264
Here are the available options that can be provided to the transport options array or as query parameters:
4365

44-
- **blocking_timeout:**
45-
- *required:* no
46-
- *default:* 30
47-
- *description:* The number of seconds for the redis bRpopLpush command to block. **DO NOT** set this value over or around60 seconds or you will experience `redis failed to read` type errors due to a bug in the redis extension.
4866
- **queue:**
4967
- *required:* yes
5068
- *default:* N/A
@@ -60,15 +78,21 @@ However, if for some reason a worker is killed via `SIGKILL` (aka `kill -9`), th
6078

6179
It won't hurt anything other than storage to have those `_processing` lists take up space. It also *shouldn't* hurt anything to periodically just wipe out those lists using the `DEL` command. But that would need to be verified that it wouldn't hurt any running workers (which I don't think it will).
6280

63-
### ReQueing Failed Messages
81+
### Using Symfony's Redis Transport at the same time
6482

65-
This library makes absolutely no attempt to requeue a message once it's failed. I've found in practice that if a message fails once, it will almost always fail again if re-tried shortly after.
83+
Both symfony's redis and the krak redis transport register the dsn prefix: `redis://`. In the scenario that you want to support both transports, you'll need to use the `use_krak_redis` option to disable this libraries redis transport.
6684

67-
Furthermore, The consequences for having code that can retry after failure mean that all operations must be idempotent, but typically, queued jobs by nature are not that way because they might be integrating with external vendors or payment processing systems that might cause duplicate charges or orders being made (due to an error and then retrying the same message).
68-
69-
I imagine in the future we will support better logging of failed messages and the possibility to store failed messages in a db table, but that would be handled via a Receiver decorator and not from the actual redis queing system.
70-
71-
It's important to note that currently, this library does not handle any cleaning of the processed queue.
85+
```yaml
86+
framework:
87+
messenger:
88+
transports:
89+
krak_redis:
90+
dsn: '%env(MESSENGER_TRANSPORT_DSN)%'
91+
options: { queue: queue_acme }
92+
sf_redis:
93+
dsn: '%env(MESSENGER_TRANSPORT_DSN)%'
94+
options: { use_krak_redis: false } # this allows symfony's redis transport factory to take precedence
95+
```
7296

7397
## Testing
7498

composer.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"require": {
1313
"ext-json": "*",
1414
"ext-redis": "*",
15-
"symfony/messenger": "4.1.*"
15+
"symfony/messenger": "^4.4"
1616
},
1717
"autoload": {
1818
"psr-4": {
@@ -25,12 +25,15 @@
2525
}
2626
},
2727
"require-dev": {
28-
"monolog/monolog": "^1.23",
28+
"nyholm/symfony-bundle-test": "^1.6",
2929
"phpunit/phpunit": "^7.3",
3030
"symfony/dependency-injection": "^4.1",
3131
"symfony/http-kernel": "^4.1",
32-
"symfony/process": "^4.1",
3332
"symfony/property-access": "^4.1",
3433
"symfony/serializer": "^4.1"
34+
},
35+
"scripts": {
36+
"test": "phpunit --testdox --colors=always",
37+
"flush-redis": "docker-compose exec -T redis redis-cli flushall"
3538
}
3639
}

phpunit.xml.dist

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<!-- https://phpunit.de/manual/current/en/appendixes.configuration.html -->
44
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5-
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.5/phpunit.xsd"
5+
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
66
backupGlobals="false"
77
colors="true"
88
bootstrap="vendor/autoload.php">
@@ -12,10 +12,10 @@
1212
<env name="REDIS_DSN" value="redis://localhost:6379?queue=messenger"/>
1313
</php>
1414
<testsuites>
15-
<testsuite name="Symfony MessengerRedis Integration Test Suite">
16-
<directory>tests/Integration</directory>
15+
<testsuite name="feature">
16+
<directory>tests/Feature</directory>
1717
</testsuite>
18-
<testsuite name="Symfony MessengerRedis Unit Test Suite">
18+
<testsuite name="unit">
1919
<directory>tests/Unit</directory>
2020
</testsuite>
2121
</testsuites>

src/DependencyInjection/MessengerReceiversPass.php

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/DependencyInjection/MessengerRedisExtension.php

Lines changed: 0 additions & 22 deletions
This file was deleted.

src/Enhancers/AutoScale/CreateProcessArgs.php

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/Enhancers/AutoScale/PcntlForkProcessManager.php

Lines changed: 0 additions & 27 deletions
This file was deleted.

src/Enhancers/AutoScale/ProcessManager.php

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/Enhancers/AutoScale/SymfonyProcessProcessManager.php

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/Enhancers/AutoScalingReceiver.php

Lines changed: 0 additions & 101 deletions
This file was deleted.

0 commit comments

Comments
 (0)