8000 feature #34156 Adding DoctrineClearEntityManagerWorkerSubscriber to r… · symfony/symfony@9eedb45 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9eedb45

Browse files
committed
feature #34156 Adding DoctrineClearEntityManagerWorkerSubscriber to reset EM in worker (weaverryan)
This PR was merged into the 4.4 branch. Discussion ---------- Adding DoctrineClearEntityManagerWorkerSubscriber to reset EM in worker | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes & no :) | New feature? | yes | Deprecations? | no | Tickets | Fix #34073 | License | MIT | Doc PR | symfony/symfony-docs#12575 | DoctrineBundle PR | doctrine/DoctrineBundle#1043 Hi! I've seen a few developers get "bit" by an issue recently: after running `messenger:consume`, the 2nd, 3rd, 4th, etc message that are handled are getting out-of-date data from Doctrine. The reason is simple: A) Consume the 1st message, it queries for `Foo id=1` to do something B) 10 minutes go by C) Consume the 2nd message. It also queries for `Foo id=1`, but because this is already in the identity map, Doctrine re-uses the data that is now *10* minutes old. Even though one worker process handles many messages, the system should (as much as possible) isolate each handler from each other. This is one very practical place we can help people. Also, checking the code, I don't think clearing the entity manager will cause any issues for an EM whose Connection has not been "connected" yet (i.e. it will not cause a connection to be established). We would wire this in DoctrineBundle, and could make it disable-able... in case that's something that's needed. Commits ------- e7b9888 Adding DoctrineClearEntityManagerWorkerSubscriber to reset entity manager in worker
2 parents cf10c02 + e7b9888 commit 9eedb45

File tree

5 files changed

+96
-113
lines changed

5 files changed

+96
-113
lines changed

src/Symfony/Bridge/Doctrine/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ CHANGELOG
44
4.4.0
55
-----
66

7-
* added `DoctrineClearEntityManagerMiddleware`
7+
* added `DoctrineClearEntityManagerWorkerSubscriber`
88
* deprecated `RegistryInterface`, use `Doctrine\Common\Persistence\ManagerRegistry`
99
* added support for invokable event listeners
1010
* added `getMetadataDriverClass` method to deprecate class parameters in service configuration files

src/Symfony/Bridge/Doctrine/Messenger/DoctrineClearEntityManagerMiddleware.php

Lines changed: 0 additions & 36 deletions
This file was deleted.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Doctrine\Messenger;
13+
14+
use Doctrine\Common\Persistence\ManagerRegistry;
15+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
16+
use Symfony\Component\Messenger\Event\WorkerMessageFailedEvent;
17+
use Symfony\Component\Messenger\Event\WorkerMessageHandledEvent;
18+
19+
/**
20+
* Clears entity managers between messages being handled to avoid outdated data.
21+
*
22+
* @author Ryan Weaver <ryan@symfonycasts.com>
23+
*/
24+
class DoctrineClearEntityManagerWorkerSubscriber implements EventSubscriberInterface
25+
{
26+
private $managerRegistry;
27+
28+
public function __construct(ManagerRegistry $managerRegistry)
29+
{
30+
$this->managerRegistry = $managerRegistry;
31+
}
32+
33+
public function onWorkerMessageHandled()
34+
{
35+
$this->clearEntityManagers();
36+
}
37+
38+
public function onWorkerMessageFailed()
39+
{
40+
$this->clearEntityManagers();
41+
}
42+
43+
public static function getSubscribedEvents()
44+
{
45+
yield WorkerMessageHandledEvent::class => 'onWorkerMessageHandled';
46+
yield WorkerMessageFailedEvent::class => 'onWorkerMessageFailed';
47+
}
48+
49+
private function clearEntityManagers()
50+
{
51+
foreach ($this->managerRegistry->getManagers() as $manager) {
52+
$manager->clear();
53+
}
54+
}
55+
}

src/Symfony/Bridge/Doctrine/Tests/Messenger/DoctrineClearEntityManagerMiddlewareTest.php

Lines changed: 0 additions & 76 deletions
This file was deleted.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Doctrine\Tests\Messenger;
13+
14+
use Doctrine\Common\Persistence\ManagerRegistry;
15+
use Doctrine\ORM\EntityManagerInterface;
16+
use Symfony\Bridge\Doctrine\Messenger\DoctrineClearEntityManagerWorkerSubscriber;
17+
use Symfony\Component\Messenger\Test\Middleware\MiddlewareTestCase;
18+
19+
class DoctrineClearEntityManagerWorkerSubscriberTest extends MiddlewareTestCase
20+
{
21+
public function testMiddlewareClearEntityManager()
22+
{
23+
$entityManager1 = $this->createMock(EntityManagerInterface::class);
24+
$entityManager1->expects($this->once())
25+
->method('clear');
26+
27+
$entityManager2 = $this->createMock(EntityManagerInterface::class);
28+
$entityManager2->expects($this->once())
29+
->method('clear');
30+
31+
$managerRegistry = $this->createMock(ManagerRegistry::class);
32+
$managerRegistry
33+
->method('getManagers')
34+
->with()
35+
->willReturn([$entityManager1, $entityManager2]);
36+
37+
$subscriber = new DoctrineClearEntityManagerWorkerSubscriber($managerRegistry);
38+
$subscriber->onWorkerMessageHandled();
39+
}
40+
}

0 commit comments

Comments
 (0)
0