Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Messenger] Fix worker-only Doctrine middleware from running always #34066

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 22, 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
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
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.
Expand All @@ -27,7 +28,9 @@ protected function handleForManager(EntityManagerInterface $entityManager, Envel
try {
return $stack->next()->handle($envelope, $stack);
} finally {
$entityManager->clear();
if (null !== $envelope->last(ReceivedStamp::class)) {
$entityManager->clear();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
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.
Expand All @@ -29,7 +30,9 @@ protected function handleForManager(EntityManagerInterface $entityManager, Envel

return $stack->next()->handle($envelope, $stack);
} finally {
$connection->close();
if (null !== $envelope->last(ReceivedStamp::class)) {
$connection->close();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
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.
Expand All @@ -23,6 +24,15 @@
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();

Expand All @@ -34,7 +44,5 @@ protected function handleForManager(EntityManagerInterface $entityManager, Envel
if (!$entityManager->isOpen()) {
$this->managerRegistry->resetManager($this->entityManagerName);
}

return $stack->next()->handle($envelope, $stack);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
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
Expand All @@ -34,7 +35,10 @@ public function testMiddlewareClearEntityManager()

$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()
Expand All @@ -51,4 +55,22 @@ public function testInvalidEntityManagerThrowsException()

$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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
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
Expand Down Expand Up @@ -49,7 +50,10 @@ public function testMiddlewareCloseConnection()
->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()
Expand All @@ -66,4 +70,14 @@ public function testInvalidEntityManagerThrowsException()

$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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
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
Expand Down Expand Up @@ -56,7 +57,10 @@ public function testMiddlewarePingOk()
->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()
Expand All @@ -70,7 +74,10 @@ public function testMiddlewarePingResetEntityManager()
->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()
Expand All @@ -87,4 +94,21 @@ public function testInvalidEntityManagerThrowsException()

$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());
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.