-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Messenger] Add clear Entity Manager middleware #31334
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
[Messenger] Add clear Entity Manager middleware #31334
Conversation
src/Symfony/Bridge/Doctrine/Messenger/DoctrineClearEntityManagerMiddleware.php
Show resolved
Hide resolved
src/Symfony/Bridge/Doctrine/Messenger/DoctrineClearEntityManagerMiddleware.php
Outdated
Show resolved
Hide resolved
4c30a13
to
8c03f5f
Compare
src/Symfony/Bridge/Doctrine/Messenger/DoctrineClearEntityManagerMiddleware.php
Outdated
Show resolved
Hide resolved
8c03f5f
to
42ac426
Compare
1a25291
to
5d2b022
Compare
I've switched branch to 4.4, tests are green. |
src/Symfony/Bridge/Doctrine/Tests/Messenger/DoctrineClearEntityManagerMiddlewareTest.php
Outdated
Show resolved
Hide resolved
src/Symfony/Bridge/Doctrine/Tests/Messenger/DoctrineClearEntityManagerMiddlewareTest.php
Outdated
Show resolved
Hide resolved
5d2b022
to
6e690a6
Compare
Status: Needs Review |
Thank you @Koc. |
This PR was merged into the 4.4 branch. Discussion ---------- [Messenger] Add clear Entity Manager middleware | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #29662 | License | MIT | Doc PR | TBD General purpose of this middleware: * avoid memory leaks during messages handling * prevent unexpected side effects when entities that already stored in identity map not refreshed between messages Commits ------- 6e690a6 Add clear Entity Manager middleware (closes #29662)
* | ||
* @author Konstantin Myakshin <molodchick@gmail.com> | ||
*/ | ||
class DoctrineClearEntityManagerMiddleware implements MiddlewareInterface |
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.
why not final?
I discovered this PR through Symfony blog post, and tried code of this PR on a Symfony 4.3 project. This middleware is correctly called by // get whatever user in $user variable
$message = new SendEmailCommand();
// Perform whatever business logic and add a stamp
$e = new Envelope($message, [new DelayHeader($delay)]);
$this->bus->dispatch($e);
// Applicative log in database that a message has been sent. I naively expect $user to still be available and known from the entity manager
$this->userLogger->userLog($user, 'send_email_command',"sent message with a delay of $delay seconds"); now produce the following error message
I can workaround it in my application, but I'm not sure this is a desirable feature to have this middleware called when a message is sent. Is there a way to use this middleware only when unning consumer and not when sending message ? Edit: after some more search, a similar discussion goes on #32436 |
See #34066 |
…ng always (weaverryan) This PR was merged into the 4.4 branch. Discussion ---------- [Messenger] Fix worker-only Doctrine middleware from running always | Q | A | ------------- | --- | Branch? | 4.4 (or 4.3?, this is a bug fix) | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #32436 Depends on #34069 | License | MIT | Doc PR | not needed Several Doctrine middleware are only meant to be run in a "worker" context: we want to "ping" the connection, "close" the connection and "clear" the entity manager ONLY when we are receiving messages. Before this PR, it was done always, which causes bad behavior for sync messages (imagine your Doctrine connection being closed in the middle of a controller or see #31334 (comment)). This fixes that in a pragmatic way: no new system for "worker-only" middleware or anything like that: just make the middleware smart enough to only do their work when a message is being received async. Commits ------- 290a729 Fixing issue where worker-only middleware were run in all contexts
General purpose of this middleware: