8000 [Messenger] reset connection on worker shutdown by SanderHagen · Pull Request #45910 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Messenger] reset connection on worker shutdown #45910

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
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
29 changes: 29 additions & 0 deletions src/Symfony/Component/Messenger/Tests/WorkerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use Symfony\Component\Messenger\Transport\Receiver\ReceiverInterface;
use Symfony\Component\Messenger\Worker;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\Service\ResetInterface;

/**
* @group time-sensitive
Expand Down Expand Up @@ -85,6 +86,19 @@ public function testHandlingErrorCausesReject()
$this->assertSame(0, $receiver->getAcknowledgeCount());
}

public function testWorkerResetsConnectionIfReceiverIsResettable()
{
$resettableReceiver = new ResettableDummyReceiver([]);

$bus = $this->createMock(MessageBusInterface::class);
$dispatcher = new EventDispatcher();

$worker = new Worker([$resettableReceiver], $bus, $dispatcher);
$worker->stop();
$worker->run();
$this->assertTrue($resettableReceiver->hasBeenReset());
}

public function testWorkerDoesNotSendNullMessagesToTheBus()
{
$receiver = new DummyReceiver([
Expand Down Expand Up @@ -283,3 +297,18 @@ public function getRejectCount(): int
return $this->rejectCount;
}
}

class ResettableDummyReceiver extends DummyReceiver implements ResetInterface
{
private $hasBeenReset = false;

public function reset()
{
$this->hasBeenReset = true;
}

public function hasBeenReset(): bool
{
return $this->hasBeenReset;
}
}
11 changes: 11 additions & 0 deletions src/Symfony/Component/Messenger/Worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Symfony\Component\Messenger\Stamp\ReceivedStamp;
use Symfony\Component\Messenger\Transport\Receiver\ReceiverInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\Service\ResetInterface;

/**
* @author Samuel Roze <samuel.roze@gmail.com>
Expand Down Expand Up @@ -102,6 +103,7 @@ public function run(array $options = []): void
}

$this->dispatchEvent(new WorkerStoppedEvent($this));
$this->resetReceiverConnections();
}

private function handleMessage(Envelope $envelope, ReceiverInterface $receiver, string $transportName): void
Expand Down Expand Up @@ -155,6 +157,15 @@ public function stop(): void
$this->shouldStop = true;
}

private function resetReceiverConnections(): void
{
foreach ($this->receivers as $transportName => $receiver) {
if ($receiver instanceof ResetInterface) {
$receiver->reset();
}
}
}

private function dispatchEvent($event)
{
if (null === $this->eventDispatcher) {
Expand Down
0