Fixing issue where worker-only middleware were run in all contexts

This commit is contained in:
Ryan Weaver 2019-10-22 09:22:16 -04:00
parent ec8e34f05e
commit 290a72917b
6 changed files with 82 additions and 8 deletions

View File

@ -14,6 +14,7 @@ namespace Symfony\Bridge\Doctrine\Messenger;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Middleware\StackInterface;
use Symfony\Component\Messenger\Stamp\ReceivedStamp;
/**
* Clears entity manager after calling all handlers.
@ -27,7 +28,9 @@ class DoctrineClearEntityManagerMiddleware extends AbstractDoctrineMiddleware
try {
return $stack->next()->handle($envelope, $stack);
} finally {
$entityManager->clear();
if (null !== $envelope->last(ReceivedStamp::class)) {
$entityManager->clear();
}
}
}
}

View File

@ -14,6 +14,7 @@ namespace Symfony\Bridge\Doctrine\Messenger;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Middleware\StackInterface;
use Symfony\Component\Messenger\Stamp\ReceivedStamp;
/**
* Closes connection and therefore saves number of connections.
@ -29,7 +30,9 @@ class DoctrineCloseConnectionMiddleware extends AbstractDoctrineMiddleware
return $stack->next()->handle($envelope, $stack);
} finally {
$connection->close();
if (null !== $envelope->last(ReceivedStamp::class)) {
$connection->close();
}
}
}
}

View File

@ -14,6 +14,7 @@ namespace Symfony\Bridge\Doctrine\Messenger;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Middleware\StackInterface;
use Symfony\Component\Messenger\Stamp\ReceivedStamp;
/**
* Checks whether the connection is still open or reconnects otherwise.
@ -23,6 +24,15 @@ use Symfony\Component\Messenger\Middleware\StackInterface;
class DoctrinePingConnectionMiddleware extends AbstractDoctrineMiddleware
{
protected function handleForManager(EntityManagerInterface $entityManager, Envelope $envelope, StackInterface $stack): Envelope
{
if (null !== $envelope->last(ReceivedStamp::class)) {
$this->pingConnection($entityManager);
}
return $stack->next()->handle($envelope, $stack);
}
private function pingConnection(EntityManagerInterface $entityManager)
{
$connection = $entityManager->getConnection();
@ -34,7 +44,5 @@ class DoctrinePingConnectionMiddleware extends AbstractDoctrineMiddleware
if (!$entityManager->isOpen()) {
$this->managerRegistry->resetManager($this->entityManagerName);
}
return $stack->next()->handle($envelope, $stack);
}
}

View File

@ -16,6 +16,7 @@ use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bridge\Doctrine\Messenger\DoctrineClearEntityManagerMiddleware;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException;
use Symfony\Component\Messenger\Stamp\ReceivedStamp;
use Symfony\Component\Messenger\Test\Middleware\MiddlewareTestCase;
class DoctrineClearEntityManagerMiddlewareTest extends MiddlewareTestCase
@ -34,7 +35,10 @@ class DoctrineClearEntityManagerMiddlewareTest extends MiddlewareTestCase
$middleware = new DoctrineClearEntityManagerMiddleware($managerRegistry, 'default');
$middleware->handle(new Envelope(new \stdClass()), $this->getStackMock());
$envelope = new Envelope(new \stdClass(), [
new ReceivedStamp('async'),
]);
$middleware->handle($envelope, $this->getStackMock());
}
public function testInvalidEntityManagerThrowsException()
@ -51,4 +55,22 @@ class DoctrineClearEntityManagerMiddlewareTest extends MiddlewareTestCase
$middleware->handle(new Envelope(new \stdClass()), $this->getStackMock(false));
}
public function testMiddlewareDoesNotClearInNonWorkerContext()
{
$entityManager = $this->createMock(EntityManagerInterface::class);
$entityManager->expects($this->never())
->method('clear');
$managerRegistry = $this->createMock(ManagerRegistry::class);
$managerRegistry
->method('getManager')
->with('default')
->willReturn($entityManager);
$middleware = new DoctrineClearEntityManagerMiddleware($managerRegistry, 'default');
$envelope = new Envelope(new \stdClass());
$middleware->handle($envelope, $this->getStackMock());
}
}

View File

@ -17,6 +17,7 @@ use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bridge\Doctrine\Messenger\DoctrineCloseConnectionMiddleware;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException;
use Symfony\Component\Messenger\Stamp\ReceivedStamp;
use Symfony\Component\Messenger\Test\Middleware\MiddlewareTestCase;
class DoctrineCloseConnectionMiddlewareTest extends MiddlewareTestCase
@ -49,7 +50,10 @@ class DoctrineCloseConnectionMiddlewareTest extends MiddlewareTestCase
->method('close')
;
$this->middleware->handle(new Envelope(new \stdClass()), $this->getStackMock());
$envelope = new Envelope(new \stdClass(), [
new ReceivedStamp('async'),
]);
$this->middleware->handle($envelope, $this->getStackMock());
}
public function testInvalidEntityManagerThrowsException()
@ -66,4 +70,14 @@ class DoctrineCloseConnectionMiddlewareTest extends MiddlewareTestCase
$middleware->handle(new Envelope(new \stdClass()), $this->getStackMock(false));
}
public function testMiddlewareNotCloseInNonWorkerContext()
{
$this->connection->expects($this->never())
->method('close')
;
$envelope = new Envelope(new \stdClass());
$this->middleware->handle($envelope, $this->getStackMock());
}
}

View File

@ -17,6 +17,7 @@ use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bridge\Doctrine\Messenger\DoctrinePingConnectionMiddleware;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException;
use Symfony\Component\Messenger\Stamp\ReceivedStamp;
use Symfony\Component\Messenger\Test\Middleware\MiddlewareTestCase;
class DoctrinePingConnectionMiddlewareTest extends MiddlewareTestCase
@ -56,7 +57,10 @@ class DoctrinePingConnectionMiddlewareTest extends MiddlewareTestCase
->method('connect')
;
$this->middleware->handle(new Envelope(new \stdClass()), $this->getStackMock());
$envelope = new Envelope(new \stdClass(), [
new ReceivedStamp('async'),
]);
$this->middleware->handle($envelope, $this->getStackMock());
}
public function testMiddlewarePingResetEntityManager()
@ -70,7 +74,10 @@ class DoctrinePingConnectionMiddlewareTest extends MiddlewareTestCase
->with($this->entityManagerName)
;
$this->middleware->handle(new Envelope(new \stdClass()), $this->getStackMock());
$envelope = new Envelope(new \stdClass(), [
new ReceivedStamp('async'),
]);
$this->middleware->handle($envelope, $this->getStackMock());
}
public function testInvalidEntityManagerThrowsException()
@ -87,4 +94,21 @@ class DoctrinePingConnectionMiddlewareTest extends MiddlewareTestCase
$middleware->handle(new Envelope(new \stdClass()), $this->getStackMock(false));
}
public function testMiddlewareNoPingInNonWorkerContext()
{
$this->connection->expects($this->never())
->method('ping')
->willReturn(false);
$this->connection->expects($this->never())
->method('close')
;
$this->connection->expects($this->never())
->method('connect')
;
$envelope = new Envelope(new \stdClass());
$this->middleware->handle($envelope, $this->getStackMock());
}
}