Skip to content

Commit 9eda8d7

Browse files
committed
Adding DoctrineClearEntityManagerWorkerSubscriber to reset entity manager in worker
1 parent 0472dbf commit 9eda8d7

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed
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->getManagerNames() as $managerName) {
52+
$this->managerRegistry->getManager($managerName)->clear();
53+
}
54+
}
55+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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('getManagerNames')
34+
->with()
35+
->willReturn(['first', 'second']);
36+
37+
$managerRegistry
38+
->expects($this->exactly(2))
39+
->method('getManager')
40+
->willReturnMap([
41+
['first', $entityManager1],
42+
['second', $entityManager2],
43+
]);
44+
45+
$subscriber = new DoctrineClearEntityManagerWorkerSubscriber($managerRegistry);
46+
$subscriber->onWorkerMessageHandled();
47+
}
48+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy