Skip to content

Commit

Permalink
fix(coroutines): added entity manager resetter, which is decoupled fr…
Browse files Browse the repository at this point in the history
…om the stability checker, so it can be overridden easily
  • Loading branch information
Rastusik committed Feb 6, 2023
1 parent 52f9133 commit e8f5c7b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
11 changes: 10 additions & 1 deletion src/Bridge/Doctrine/DoctrineProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace K911\Swoole\Bridge\Doctrine;

use K911\Swoole\Bridge\Doctrine\DBAL\ConnectionKeepAliveResetter;
use K911\Swoole\Bridge\Doctrine\ORM\EntityManagerResetter;
use K911\Swoole\Bridge\Symfony\Bundle\DependencyInjection\CompilerPass\StatefulServices\CompileProcessor;
use K911\Swoole\Bridge\Symfony\Bundle\DependencyInjection\CompilerPass\StatefulServices\Proxifier;
use K911\Swoole\Bridge\Symfony\Bundle\DependencyInjection\ContainerConstants;
Expand Down Expand Up @@ -53,11 +54,12 @@ public function process(ContainerBuilder $container, Proxifier $proxifier): void
throw new \UnexpectedValueException('Cannot obtain array of doctrine connections.');
}

$this->createEntityManagerResetterDefinition($container);
$this->prepareConnectionsForProxification($container, $connectionSvcIds);

foreach ($entityManagers as $emName => $emSvcId) {
$emDef = $container->findDefinition($emSvcId);
$tagParams = [];
$tagParams = ['resetter' => EntityManagerResetter::class];
$limit = $this->getLimitFromEntityManagerConnection($container, $emDef);

if (null !== $limit) {
Expand All @@ -72,6 +74,13 @@ public function process(ContainerBuilder $container, Proxifier $proxifier): void
$this->fixDebugDataHolderResetter($container, $proxifier);
}

private function createEntityManagerResetterDefinition(ContainerBuilder $container): void
{
$resetterDef = new Definition(EntityManagerResetter::class);
$resetterDef->setClass(EntityManagerResetter::class);
$container->setDefinition(EntityManagerResetter::class, $resetterDef);
}

private function overrideEmConfigurator(ContainerBuilder $container, Definition $emDef): void
{
$configuratorCallback = $emDef->getConfigurator();
Expand Down
20 changes: 20 additions & 0 deletions src/Bridge/Doctrine/ORM/EntityManagerResetter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace K911\Swoole\Bridge\Doctrine\ORM;

use Doctrine\Persistence\ObjectManager;
use K911\Swoole\Bridge\Symfony\Container\Resetter;

final class EntityManagerResetter implements Resetter
{
public function reset(object $service): void
{
if (!$service instanceof ObjectManager) {
throw new \UnexpectedValueException(\sprintf('Invalid service - expected %s, got %s', ObjectManager::class, \get_class($service)));
}

$service->clear();
}
}
9 changes: 3 additions & 6 deletions src/Bridge/Doctrine/ORM/EntityManagerStabilityChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,15 @@
namespace K911\Swoole\Bridge\Doctrine\ORM;

use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use K911\Swoole\Bridge\Symfony\Container\StabilityChecker;

final class EntityManagerStabilityChecker implements StabilityChecker
{
public function isStable(object $service): bool
{
if (!$service instanceof EntityManager) {
throw new \UnexpectedValueException(\sprintf('Invalid service - expected %s, got %s', EntityManager::class, \get_class($service)));
}

if ($service->isOpen()) {
$service->clear(); // clear the em when not used any more in context, so RAM can be freed early
if (!$service instanceof EntityManagerInterface) {
throw new \UnexpectedValueException(\sprintf('Invalid service - expected %s, got %s', EntityManagerInterface::class, \get_class($service)));
}

return $service->isOpen();
Expand Down

0 comments on commit e8f5c7b

Please sign in to comment.