Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Revert SyncTransport simplification and fix properly #34155

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 2 commits 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
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Middleware\StackInterface;
use Symfony\Component\Messenger\Stamp\ReceivedStamp;
use Symfony\Component\Messenger\Stamp\ConsumedByWorkerStamp;

/**
* Clears entity manager after calling all handlers.
Expand All @@ -28,7 +28,7 @@ protected function handleForManager(EntityManagerInterface $entityManager, Envel
try {
return $stack->next()->handle($envelope, $stack);
} finally {
if (null !== $envelope->last(ReceivedStamp::class)) {
if (null !== $envelope->last(ConsumedByWorkerStamp::class)) {
$entityManager->clear();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Middleware\StackInterface;
use Symfony\Component\Messenger\Stamp\ReceivedStamp;
use Symfony\Component\Messenger\Stamp\ConsumedByWorkerStamp;

/**
* Closes connection and therefore saves number of connections.
Expand All @@ -30,7 +30,7 @@ protected function handleForManager(EntityManagerInterface $entityManager, Envel

return $stack->next()->handle($envelope, $stack);
} finally {
if (null !== $envelope->last(ReceivedStamp::class)) {
if (null !== $envelope->last(ConsumedByWorkerStamp::class)) {
$connection->close();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Middleware\StackInterface;
use Symfony\Component\Messenger\Stamp\ReceivedStamp;
use Symfony\Component\Messenger\Stamp\ConsumedByWorkerStamp;

/**
* Checks whether the connection is still open or reconnects otherwise.
Expand All @@ -25,7 +25,7 @@ class DoctrinePingConnectionMiddleware extends AbstractDoctrineMiddleware
{
protected function handleForManager(EntityManagerInterface $entityManager, Envelope $envelope, StackInterface $stack): Envelope
{
if (null !== $envelope->last(ReceivedStamp::class)) {
if (null !== $envelope->last(ConsumedByWorkerStamp::class)) {
$this->pingConnection($entityManager);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +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\Stamp\ConsumedByWorkerStamp;
use Symfony\Component\Messenger\Test\Middleware\MiddlewareTestCase;

class DoctrineClearEntityManagerMiddlewareTest extends MiddlewareTestCase
Expand All @@ -36,7 +36,7 @@ public function testMiddlewareClearEntityManager()
$middleware = new DoctrineClearEntityManagerMiddleware($managerRegistry, 'default');

$envelope = new Envelope(new \stdClass(), [
new ReceivedStamp('async'),
new ConsumedByWorkerStamp(),
]);
$middleware->handle($envelope, $this->getStackMock());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +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\Stamp\ConsumedByWorkerStamp;
use Symfony\Component\Messenger\Test\Middleware\MiddlewareTestCase;

class DoctrineCloseConnectionMiddlewareTest extends MiddlewareTestCase
Expand Down Expand Up @@ -51,7 +51,7 @@ public function testMiddlewareCloseConnection()
;

$envelope = new Envelope(new \stdClass(), [
new ReceivedStamp('async'),
new ConsumedByWorkerStamp(),
]);
$this->middleware->handle($envelope, $this->getStackMock());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +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\Stamp\ConsumedByWorkerStamp;
use Symfony\Component\Messenger\Test\Middleware\MiddlewareTestCase;

class DoctrinePingConnectionMiddlewareTest extends MiddlewareTestCase
Expand Down Expand Up @@ -58,7 +58,7 @@ public function testMiddlewarePingOk()
;

$envelope = new Envelope(new \stdClass(), [
new ReceivedStamp('async'),
new ConsumedByWorkerStamp(),
]);
$this->middleware->handle($envelope, $this->getStackMock());
}
Expand All @@ -75,7 +75,7 @@ public function testMiddlewarePingResetEntityManager()
;

$envelope = new Envelope(new \stdClass(), [
new ReceivedStamp('async'),
new ConsumedByWorkerStamp(),
]);
$this->middleware->handle($envelope, $this->getStackMock());
}
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bridge/Doctrine/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"symfony/dependency-injection": "^3.4|^4.0|^5.0",
"symfony/form": "^4.4|^5.0",
"symfony/http-kernel": "^3.4|^4.0|^5.0",
"symfony/messenger": "^4.3|^5.0",
"symfony/messenger": "^4.4|^5.0",
"symfony/property-access": "^3.4|^4.0|^5.0",
"symfony/property-info": "^3.4|^4.0|^5.0",
"symfony/proxy-manager-bridge": "^3.4|^4.0|^5.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1775,27 +1775,18 @@ private function registerMessengerConfiguration(array $config, ContainerBuilder
$container->setAlias('messenger.default_serializer', $config['serializer']['default_serializer']);
}

$senderReferences = [];
$syncTransports = [];
$senderAliases = [];
$transportRetryReferences = [];
foreach ($config['transports'] as $name => $transport) {
$serializerId = $transport['serializer'] ?? 'messenger.default_serializer';

if (0 === strpos($transport['dsn'], 'sync://')) {
$syncTransports[] = $name;
} else {
$transportDefinition = (new Definition(TransportInterface::class))
->setFactory([new Reference('messenger.transport_factory'), 'createTransport'])
->setArguments([$transport['dsn'], $transport['options'] + ['transport_name' => $name], new Reference($serializerId)])
->addTag('messenger.receiver', ['alias' => $name])
;
$container->setDefinition($transportId = 'messenger.transport.'.$name, $transportDefinition);

// alias => service_id
$senderReferences[$name] = new Reference($transportId);
// service_id => service_id
$senderReferences[$transportId] = new Reference($transportId);
}
$transportDefinition = (new Definition(TransportInterface::class))
->setFactory([new Reference('messenger.transport_factory'), 'createTransport'])
->setArguments([$transport['dsn'], $transport['options'] + ['transport_name' => $name], new Reference($serializerId)])
->addTag('messenger.receiver', ['alias' => $name])
;
$container->setDefinition($transportId = 'messenger.transport.'.$name, $transportDefinition);
$senderAliases[$name] = $transportId;

if (null !== $transport['retry_strategy']['service']) {
$transportRetryReferences[$name] = new Reference($transport['retry_strategy']['service']);
Expand All @@ -1813,25 +1804,30 @@ private function registerMessengerConfiguration(array $config, ContainerBuilder
}
}

$senderReferences = [];
// alias => service_id
foreach ($senderAliases as $alias => $serviceId) {
$senderReferences[$alias] = new Reference($serviceId);
}
// service_id => service_id
foreach ($senderAliases as $serviceId) {
$senderReferences[$serviceId] = new Reference($serviceId);
}

$messageToSendersMapping = [];
foreach ($config['routing'] as $message => $messageConfiguration) {
if ('*' !== $message && !class_exists($message) && !interface_exists($message, false)) {
throw new LogicException(sprintf('Invalid Messenger routing configuration: class or interface "%s" not found.', $message));
}

// filter out "sync" senders
$realSenders = [];
// make sure senderAliases contains all senders
foreach ($messageConfiguration['senders'] as $sender) {
if (isset($senderReferences[$sender])) {
$realSenders[] = $sender;
} elseif (!\in_array($sender, $syncTransports, true)) {
if (!isset($senderReferences[$sender])) {
throw new LogicException(sprintf('Invalid Messenger routing configuration: the "%s" class is being routed to a sender called "%s". This is not a valid transport or service id.', $message, $sender));
}
}

if ($realSenders) {
$messageToSendersMapping[$message] = $realSenders;
}
$messageToSendersMapping[$message] = $messageConfiguration['senders'];
}

$container->getDefinition('messenger.senders_locator')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@
<tag name="messenger.transport_factory" />
</service>

<service id="messenger.transport.sync.factory" class="Symfony\Component\Messenger\Transport\Sync\SyncTransportFactory">
<tag name="messenger.transport_factory" />
<argument type="service" id="messenger.routable_message_bus" />
</service>

<service id="messenger.transport.in_memory.factory" class="Symfony\Component\Messenger\Transport\InMemoryTransportFactory">
<tag name="messenger.transport_factory" />
<tag name="kernel.reset" method="reset" />
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddAnnotationsCachedReaderPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\FrameworkExtension;
use Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Messenger\DummyMessage;
use Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Messenger\FooMessage;
use Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Messenger\SecondMessage;
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
use Symfony\Bundle\FullStack;
use Symfony\Component\Cache\Adapter\AdapterInterface;
Expand Down Expand Up @@ -784,17 +782,6 @@ public function testMessengerInvalidTransportRouting()
$this->createContainerFromFile('messenger_routing_invalid_transport');
}

public function testMessengerSyncTransport()
{
$container = $this->createContainerFromFile('messenger_sync_transport');
$senderLocatorDefinition = $container->getDefinition('messenger.senders_locator');

$sendersMapping = $senderLocatorDefinition->getArgument(0);
$this->assertEquals(['amqp'], $sendersMapping[DummyMessage::class]);
$this->assertArrayNotHasKey(SecondMessage::class, $sendersMapping);
$this->assertEquals(['amqp'], $sendersMapping[FooMessage::class]);
}

public function testTranslator()
{
$container = $this->createContainerFromFile('full');
Expand Down
1 change: 0 additions & 1 deletion src/Symfony/Component/Messenger/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ CHANGELOG
4.4.0
-----

* [BC BREAK] The `SyncTransport` and `SyncTransportFactory` classes were removed.
* Deprecated passing a `ContainerInterface` instance as first argument of the `ConsumeMessagesCommand` constructor,
pass a `RoutableMessageBus` instance instead.
* Added support for auto trimming of Redis streams.
Expand Down
19 changes: 19 additions & 0 deletions src/Symfony/Component/Messenger/Stamp/ConsumedByWorkerStamp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

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

namespace Symfony\Component\Messenger\Stamp;

/**
* A marker that this message was consumed by a worker process.
*/
class ConsumedByWorkerStamp implements NonSendableStampInterface
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ public function testItReceivesSignals()
Get envelope with message: Symfony\Component\Messenger\Tests\Fixtures\DummyMessage
with stamps: [
"Symfony\\Component\\Messenger\\Transport\\AmqpExt\\AmqpReceivedStamp",
"Symfony\\Component\\Messenger\\Stamp\\ReceivedStamp"
"Symfony\\Component\\Messenger\\Stamp\\ReceivedStamp",
"Symfony\\Component\\Messenger\\Stamp\\ConsumedByWorkerStamp"
]
Done.

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

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

namespace Symfony\Component\Messenger\Tests\Transport\AmqpExt;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
use Symfony\Component\Messenger\Transport\Sync\SyncTransport;
use Symfony\Component\Messenger\Transport\Sync\SyncTransportFactory;

class SyncTransportFactoryTest extends TestCase
{
public function testCreateTransport()
{
$serializer = $this->createMock(SerializerInterface::class);
$bus = $this->createMock(MessageBusInterface::class);
$factory = new SyncTransportFactory($bus);
$transport = $factory->createTransport('sync://', [], $serializer);
$this->assertInstanceOf(SyncTransport::class, $transport);
}
}
Loading