-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
[Messenger] Added documentation about DispatchAfterCurrentBusMiddleware #10015
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
Changes from 1 commit
8107340
66fc600
091040f
0a3ccf6
b5921bf
aba0edf
9b589ff
0e1e0dd
aa4096f
636de55
157d3f0
6b71ef8
45245df
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,15 +4,15 @@ | |
Record Events Produced by a Handler | ||
=================================== | ||
|
||
In a example application there is a command (a CQRS message) named ``CreateUser``. | ||
That command is handled by the ``CreateUserHandler``. The command handler creates | ||
In an example application there is a command (a CQRS message) named ``CreateUser``. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. - In an example application there is a command (a CQRS message) named ``CreateUser``.
+ Let's take the example of an application that has a command (a CQRS message) named ``CreateUser``. ? |
||
That command is handled by the ``CreateUserHandler`` which creates | ||
a ``User`` object, stores that object to a database and dispatches an ``UserCreatedEvent``. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "a UserCreatedEvent" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you sure? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Almost :) I'm not native. But you wrote "a User" just before. See https://english.stackexchange.com/questions/105116/is-it-a-user-or-an-user There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would say that "a User" was a typo. I read your source. I did not know that. Thank you. I've updated the PR. |
||
That event is also a normal message but is handled by an *event* bus. | ||
|
||
There are many subscribers to the ``UserCreatedEvent``, one subscriber may send | ||
a welcome email to the new user. Since we are using the ``DoctrineTransactionMiddleware`` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd suggest to split the two and explicit the "problem": - Since we are using the ``DoctrineTransactionMiddleware`` [...]
+ We are using the ``DoctrineTransactionMiddleware`` to wrap all database queries in one database transaction.
+
+ **Problem:** if an exception is thrown when sending the welcome email, then the user will not be created because the ``DoctrineTransactionMiddleware`` will rollback the Doctrine transaction, in which the user has been created. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice. Thank you |
||
we wrap all database queries in one database transaction and rollback that transaction | ||
if an exception is thrown. That would mean that if an exception is thrown when sending | ||
if an exception is thrown. That means that if an exception is thrown when sending | ||
the welcome email, then the user will not be created. | ||
|
||
The solution to this issue is to not dispatch the ``UserCreatedEvent`` in the | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same, I'd highlight the solution:
|
||
|
@@ -27,7 +27,7 @@ in the middleware chain. | |
|
||
.. code-block:: yaml | ||
|
||
# config/packages/workflow.yaml | ||
# config/packages/messenger.yaml | ||
framework: | ||
messenger: | ||
default_bus: messenger.bus.command | ||
|
@@ -45,6 +45,11 @@ in the middleware chain. | |
|
||
.. code-block:: php | ||
|
||
namespace App\Messenger\CommandHandler; | ||
|
||
use App\Entity\User; | ||
use App\Messenger\Command\CreateUser; | ||
use App\Messenger\Event\UserCreatedEvent; | ||
use Doctrine\ORM\EntityManagerInterface; | ||
Nyholm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
use Symfony\Component\Messenger\MessageRecorderInterface; | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be better to find a name that describes the problem than the technical solution. Something like "Event recorder: Tolerating failures while in Doctrine transactions"?