Skip to content

Adding DoctrineClearEntityManagerWorkerSubscriber to reset EM in worker #34156

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

Merged
merged 1 commit into from
Oct 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Symfony/Bridge/Doctrine/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ CHANGELOG
4.4.0
-----

* added `DoctrineClearEntityManagerMiddleware`
* added `DoctrineClearEntityManagerWorkerSubscriber`
* deprecated `RegistryInterface`, use `Doctrine\Common\Persistence\ManagerRegistry`
* added support for invokable event listeners
* added `getMetadataDriverClass` method to deprecate class parameters in service configuration files
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\Doctrine\Messenger;

use Doctrine\Common\Persistence\ManagerRegistry;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Messenger\Event\WorkerMessageFailedEvent;
use Symfony\Component\Messenger\Event\WorkerMessageHandledEvent;

/**
* Clears entity managers between messages being handled to avoid outdated data.
*
* @author Ryan Weaver <ryan@symfonycasts.com>
*/
class DoctrineClearEntityManagerWorkerSubscriber implements EventSubscriberInterface
{
private $managerRegistry;

public function __construct(ManagerRegistry $managerRegistry)
{
$this->managerRegistry = $managerRegistry;
}

public function onWorkerMessageHandled()
{
$this->clearEntityManagers();
}

public function onWorkerMessageFailed()
{
$this->clearEntityManagers();
}

public static function getSubscribedEvents()
{
yield WorkerMessageHandledEvent::class => 'onWorkerMessageHandled';
yield WorkerMessageFailedEvent::class => 'onWorkerMessageFailed';
}

private function clearEntityManagers()
{
foreach ($this->managerRegistry->getManagers() as $manager) {
$manager->clear();
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\Doctrine\Tests\Messenger;

use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bridge\Doctrine\Messenger\DoctrineClearEntityManagerWorkerSubscriber;
use Symfony\Component\Messenger\Test\Middleware\MiddlewareTestCase;

class DoctrineClearEntityManagerWorkerSubscriberTest extends MiddlewareTestCase
{
public function testMiddlewareClearEntityManager()
{
$entityManager1 = $this->createMock(EntityManagerInterface::class);
$entityManager1->expects($this->once())
->method('clear');

$entityManager2 = $this->createMock(EntityManagerInterface::class);
$entityManager2->expects($this->once())
->method('clear');

$managerRegistry = $this->createMock(ManagerRegistry::class);
$managerRegistry
->method('getManagers')
->with()
->willReturn([$entityManager1, $entityManager2]);

$subscriber = new DoctrineClearEntityManagerWorkerSubscriber($managerRegistry);
$subscriber->onWorkerMessageHandled();
}
}