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

Commit 7894ad8

Browse filesBrowse files
committed
Fixing issue where worker-only middleware were run in all contexts
1 parent ec8e34f commit 7894ad8
Copy full SHA for 7894ad8

9 files changed

+111
-10
lines changed

‎src/Symfony/Bridge/Doctrine/Messenger/AbstractDoctrineMiddleware.php

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Doctrine/Messenger/AbstractDoctrineMiddleware.php
+11Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException;
1818
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
1919
use Symfony\Component\Messenger\Middleware\StackInterface;
20+
use Symfony\Component\Messenger\Stamp\ReceivedStamp;
2021

2122
/**
2223
* @author Konstantin Myakshin <molodchick@gmail.com>
@@ -46,4 +47,14 @@ final public function handle(Envelope $envelope, StackInterface $stack): Envelop
4647
}
4748

4849
abstract protected function handleForManager(EntityManagerInterface $entityManager, Envelope $envelope, StackInterface $stack): Envelope;
50+
51+
protected function isMessageReceivedFromWorker(Envelope $envelope): bool
52+
{
53+
/** @var ReceivedStamp|null $receivedStamp */
54+
if (null === $receivedStamp = $envelope->last(ReceivedStamp::class)) {
55+
return false;
56+
}
57+
58+
return $receivedStamp->isReceivedViaWorker();
59+
}
4960
}

‎src/Symfony/Bridge/Doctrine/Messenger/DoctrineClearEntityManagerMiddleware.php

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Doctrine/Messenger/DoctrineClearEntityManagerMiddleware.php
+3-1Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ protected function handleForManager(EntityManagerInterface $entityManager, Envel
2727
try {
2828
return $stack->next()->handle($envelope, $stack);
2929
} finally {
30-
$entityManager->clear();
30+
if ($this->isMessageReceivedFromWorker($envelope)) {
31+
$entityManager->clear();
32+
}
3133
}
3234
}
3335
}

‎src/Symfony/Bridge/Doctrine/Messenger/DoctrineCloseConnectionMiddleware.php

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Doctrine/Messenger/DoctrineCloseConnectionMiddleware.php
+3-1Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ protected function handleForManager(EntityManagerInterface $entityManager, Envel
2929

3030
return $stack->next()->handle($envelope, $stack);
3131
} finally {
32-
$connection->close();
32+
if ($this->isMessageReceivedFromWorker($envelope)) {
33+
$connection->close();
34+
}
3335
}
3436
}
3537
}

‎src/Symfony/Bridge/Doctrine/Messenger/DoctrinePingConnectionMiddleware.php

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Doctrine/Messenger/DoctrinePingConnectionMiddleware.php
+9-2Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@
2323
class DoctrinePingConnectionMiddleware extends AbstractDoctrineMiddleware
2424
{
2525
protected function handleForManager(EntityManagerInterface $entityManager, Envelope $envelope, StackInterface $stack): Envelope
26+
{
27+
if ($this->isMessageReceivedFromWorker($envelope)) {
28+
$this->pingConnection($entityManager);
29+
}
30+
31+
return $stack->next()->handle($envelope, $stack);
32+
}
33+
34+
private function pingConnection(EntityManagerInterface $entityManager)
2635
{
2736
$connection = $entityManager->getConnection();
2837

@@ -34,7 +43,5 @@ protected function handleForManager(EntityManagerInterface $entityManager, Envel
3443
if (!$entityManager->isOpen()) {
3544
$this->managerRegistry->resetManager($this->entityManagerName);
3645
}
37-
38-
return $stack->next()->handle($envelope, $stack);
3946
}
4047
}

‎src/Symfony/Bridge/Doctrine/Tests/Messenger/DoctrineClearEntityManagerMiddlewareTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Doctrine/Tests/Messenger/DoctrineClearEntityManagerMiddlewareTest.php
+27-1Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Bridge\Doctrine\Messenger\DoctrineClearEntityManagerMiddleware;
1717
use Symfony\Component\Messenger\Envelope;
1818
use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException;
19+
use Symfony\Component\Messenger\Stamp\ReceivedStamp;
1920
use Symfony\Component\Messenger\Test\Middleware\MiddlewareTestCase;
2021

2122
class DoctrineClearEntityManagerMiddlewareTest extends MiddlewareTestCase
@@ -34,7 +35,10 @@ public function testMiddlewareClearEntityManager()
3435

3536
$middleware = new DoctrineClearEntityManagerMiddleware($managerRegistry, 'default');
3637

37-
$middleware->handle(new Envelope(new \stdClass()), $this->getStackMock());
38+
$envelope = new Envelope(new \stdClass(), [
39+
new ReceivedStamp('async')
40+
]);
41+
$middleware->handle($envelope, $this->getStackMock());
3842
}
3943

4044
public function testInvalidEntityManagerThrowsException()
@@ -51,4 +55,26 @@ public function testInvalidEntityManagerThrowsException()
5155

5256
$middleware->handle(new Envelope(new \stdClass()), $this->getStackMock(false));
5357
}
58+
59+
public function testMiddlewareDoesNotClearInNonWorkerContext()
60+
{
61+
$entityManager = $this->createMock(EntityManagerInterface::class);
62+
$entityManager->expects($this->never())
63+
->method('clear');
64+
65+
$managerRegistry = $this->createMock(ManagerRegistry::class);
66+
$managerRegistry
67+
->method('getManager')
68+
->with('default')
69+
->willReturn($entityManager);
70+
71+
$middleware = new DoctrineClearEntityManagerMiddleware($managerRegistry, 'default');
72+
73+
$envelope1 = new Envelope(new \stdClass());
74+
$envelope2 = new Envelope(new \stdClass(), [
75+
new ReceivedStamp('sync_transport', false)
76+
]);
77+
$middleware->handle($envelope1, $this->getStackMock());
78+
$middleware->handle($envelope2, $this->getStackMock());
79+
}
5480
}

‎src/Symfony/Bridge/Doctrine/Tests/Messenger/DoctrineCloseConnectionMiddlewareTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Doctrine/Tests/Messenger/DoctrineCloseConnectionMiddlewareTest.php
+19-1Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Bridge\Doctrine\Messenger\DoctrineCloseConnectionMiddleware;
1818
use Symfony\Component\Messenger\Envelope;
1919
use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException;
20+
use Symfony\Component\Messenger\Stamp\ReceivedStamp;
2021
use Symfony\Component\Messenger\Test\Middleware\MiddlewareTestCase;
2122

2223
class DoctrineCloseConnectionMiddlewareTest extends MiddlewareTestCase
@@ -49,7 +50,10 @@ public function testMiddlewareCloseConnection()
4950
->method('close')
5051
;
5152

52-
$this->middleware->handle(new Envelope(new \stdClass()), $this->getStackMock());
53+
$envelope = new Envelope(new \stdClass(), [
54+
new ReceivedStamp('async')
55+
]);
56+
$this->middleware->handle($envelope, $this->getStackMock());
5357
}
5458

5559
public function testInvalidEntityManagerThrowsException()
@@ -66,4 +70,18 @@ public function testInvalidEntityManagerThrowsException()
6670

6771
$middleware->handle(new Envelope(new \stdClass()), $this->getStackMock(false));
6872
}
73+
74+
public function testMiddlewareNotCloseInNonWorkerContext()
75+
{
76+
$this->connection->expects($this->never())
77+
->method('close')
78+
;
79+
80+
$envelope1 = new Envelope(new \stdClass());
81+
$envelope2 = new Envelope(new \stdClass(), [
82+
new ReceivedStamp('sync_transport', false)
83+
]);
84+
$this->middleware->handle($envelope1, $this->getStackMock());
85+
$this->middleware->handle($envelope2, $this->getStackMock());
86+
}
6987
}

‎src/Symfony/Bridge/Doctrine/Tests/Messenger/DoctrinePingConnectionMiddlewareTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Doctrine/Tests/Messenger/DoctrinePingConnectionMiddlewareTest.php
+30-2Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Bridge\Doctrine\Messenger\DoctrinePingConnectionMiddleware;
1818
use Symfony\Component\Messenger\Envelope;
1919
use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException;
20+
use Symfony\Component\Messenger\Stamp\ReceivedStamp;
2021
use Symfony\Component\Messenger\Test\Middleware\MiddlewareTestCase;
2122

2223
class DoctrinePingConnectionMiddlewareTest extends MiddlewareTestCase
@@ -56,7 +57,10 @@ public function testMiddlewarePingOk()
5657
->method('connect')
5758
;
5859

59-
$this->middleware->handle(new Envelope(new \stdClass()), $this->getStackMock());
60+
$envelope = new Envelope(new \stdClass(), [
61+
new ReceivedStamp('async')
62+
]);
63+
$this->middleware->handle($envelope, $this->getStackMock());
6064
}
6165

6266
public function testMiddlewarePingResetEntityManager()
@@ -70,7 +74,10 @@ public function testMiddlewarePingResetEntityManager()
7074
->with($this->entityManagerName)
7175
;
7276

73-
$this->middleware->handle(new Envelope(new \stdClass()), $this->getStackMock());
77+
$envelope = new Envelope(new \stdClass(), [
78+
new ReceivedStamp('async')
79+
]);
80+
$this->middleware->handle($envelope, $this->getStackMock());
7481
}
7582

7683
public function testInvalidEntityManagerThrowsException()
@@ -87,4 +94,25 @@ public function testInvalidEntityManagerThrowsException()
8794

8895
$middleware->handle(new Envelope(new \stdClass()), $this->getStackMock(false));
8996
}
97+
98+
public function testMiddlewareNoPingInNonWorkerContext()
99+
{
100+
$this->connection->expects($this->never())
101+
->method('ping')
102+
->willReturn(false);
103+
104+
$this->connection->expects($this->never())
105+
->method('close')
106+
;
107+
$this->connection->expects($this->never())
108+
->method('connect')
109+
;
110+
111+
$envelope1 = new Envelope(new \stdClass());
112+
$envelope2 = new Envelope(new \stdClass(), [
113+
new ReceivedStamp('sync_transport', false)
114+
]);
115+
$this->middleware->handle($envelope1, $this->getStackMock());
116+
$this->middleware->handle($envelope2, $this->getStackMock());
117+
}
90118
}

‎src/Symfony/Component/Messenger/Stamp/ReceivedStamp.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Messenger/Stamp/ReceivedStamp.php
+8-1Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,21 @@
2626
final class ReceivedStamp implements NonSendableStampInterface
2727
{
2828
private $transportName;
29+
private $receivedViaWorker;
2930

30-
public function __construct(string $transportName)
31+
public function __construct(string $transportName, bool $receivedViaWorker = true)
3132
{
3233
$this->transportName = $transportName;
34+
$this->receivedViaWorker = $receivedViaWorker;
3335
}
3436

3537
public function getTransportName(): string
3638
{
3739
return $this->transportName;
3840
}
41+
42+
public function isReceivedViaWorker(): bool
43+
{
44+
return $this->receivedViaWorker;
45+
}
3946
}

‎src/Symfony/Component/Messenger/Transport/Sync/SyncTransport.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Messenger/Transport/Sync/SyncTransport.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function send(Envelope $envelope): Envelope
5858
$sentStamp = $envelope->last(SentStamp::class);
5959
$alias = null === $sentStamp ? 'sync' : ($sentStamp->getSenderAlias() ?: $sentStamp->getSenderClass());
6060

61-
$envelope = $envelope->with(new ReceivedStamp($alias));
61+
$envelope = $envelope->with(new ReceivedStamp($alias, false));
6262

6363
return $this->messageBus->dispatch($envelope);
6464
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.