Skip to content

Commit

Permalink
Configuration du mailer + messages asynchrones
Browse files Browse the repository at this point in the history
  • Loading branch information
mmarchois committed Jan 21, 2025
1 parent 699e0e3 commit 4c7b540
Show file tree
Hide file tree
Showing 10 changed files with 219 additions and 15 deletions.
3 changes: 3 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,6 @@ S3_ACCESS_KEY=""
S3_SECRET_KEY=""
S3_BUCKET=""
S3_ENDPOINT=""

MAILER_SENDER=[email protected]
MAILER_DSN=smtp://mailer:1025
1 change: 1 addition & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
web: bin/run
worker: php bin/console messenger:consume async
postdeploy: make scalingo-postdeploy
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"symfony/framework-bundle": "7.2.*",
"symfony/http-client": "7.2.*",
"symfony/lock": "7.2.*",
"symfony/mailer": "7.2.*",
"symfony/messenger": "7.2.*",
"symfony/mime": "7.2.*",
"symfony/monolog-bundle": "^3.8",
Expand Down
149 changes: 148 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions config/packages/mailer.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
framework:
mailer:
dsn: '%env(MAILER_DSN)%'
envelope:
sender: '%env(MAILER_SENDER)%'
headers:
From: 'DiaLog <%env(MAILER_SENDER)%>'
25 changes: 11 additions & 14 deletions config/packages/messenger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,16 @@ framework:
- validation

transports:
# https://symfony.com/doc/current/messenger.html#transport-configuration
# async: '%env(MESSENGER_TRANSPORT_DSN)%'
# failed: 'doctrine://default?queue_name=failed'
# sync: 'sync://'

async:
dsn: "%env(REDIS_URL)%/messages"
retry_strategy:
max_retries: 3
delay: 1000
routing:
# Route your messages to the transports
# 'App\Message\YourMessage': async
'App\Application\AsyncCommandInterface': async

# when@test:
# framework:
# messenger:
# transports:
# # replace with your transport name here (e.g., my_transport: 'in-memory://')
# # For more Messenger testing tools, see https://github.com/zenstruck/messenger-test
# async: 'in-memory://'
when@test:
framework:
messenger:
transports:
async: 'sync://'
17 changes: 17 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ services:
- database
- redis

php_worker:
build:
context: ./docker/php
command: 'bin/console messenger:consume async --time-limit=300 --memory-limit=256M --limit=50'
restart: always
volumes:
- ./:/var/www/dialog
depends_on:
- database
- redis

nginx:
image: nginx:stable-alpine
ports:
Expand Down Expand Up @@ -68,3 +79,9 @@ services:
# Allow accessing host ports from this container via host.docker.internal on Linux
# https://stackoverflow.com/a/43541732
- host.docker.internal:host-gateway

mailer:
image: schickling/mailcatcher
ports:
- 1025:1025
- 1080:1080
9 changes: 9 additions & 0 deletions src/Application/AsyncCommandInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace App\Application;

interface AsyncCommandInterface
{
}
10 changes: 10 additions & 0 deletions src/Infrastructure/Adapter/CommandBus.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Infrastructure\Adapter;

use App\Application\AsyncCommandInterface;
use App\Application\CommandBusInterface;
use App\Application\CommandInterface;
use Symfony\Component\Messenger\Exception\HandlerFailedException;
Expand Down Expand Up @@ -35,4 +36,13 @@ public function handle(CommandInterface $command): mixed
throw $previous;
}
}

/**
* Use whenever you want to execute a command async,
* but **please ensure the Messenger routing is correct**.
*/
public function dispatchAsync(AsyncCommandInterface $message): void
{
$this->messageBus->dispatch($message);
}
}
12 changes: 12 additions & 0 deletions symfony.lock
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,18 @@
"config/packages/lock.yaml"
]
},
"symfony/mailer": {
"version": "7.2",
"recipe": {
"repo": "github.com/symfony/recipes",
"branch": "main",
"version": "4.3",
"ref": "09051cfde49476e3c12cd3a0e44289ace1c75a4f"
},
"files": [
"config/packages/mailer.yaml"
]
},
"symfony/messenger": {
"version": "6.1",
"recipe": {
Expand Down

0 comments on commit 4c7b540

Please sign in to comment.