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

Skip to content

Commit e5ddf86

Browse files
committed
[Messenger] Fix exiting FailedMessagesRetryCommand
1 parent 8ef8225 commit e5ddf86

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

src/Symfony/Component/Messenger/Command/FailedMessagesRetryCommand.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919
use Symfony\Component\Console\Input\InputOption;
2020
use Symfony\Component\Console\Output\ConsoleOutputInterface;
2121
use Symfony\Component\Console\Output\OutputInterface;
22+
use Symfony\Component\Console\SignalRegistry\SignalRegistry;
2223
use Symfony\Component\Console\Style\SymfonyStyle;
2324
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
2425
use Symfony\Component\Messenger\Event\WorkerMessageReceivedEvent;
26+
use Symfony\Component\Messenger\Event\WorkerStartedEvent;
2527
use Symfony\Component\Messenger\EventListener\StopWorkerOnMessageLimitListener;
2628
use Symfony\Component\Messenger\MessageBusInterface;
2729
use Symfony\Component\Messenger\Stamp\MessageDecodingFailedStamp;
@@ -165,6 +167,16 @@ private function runInteractive(string $failureTransportName, SymfonyStyle $io,
165167

166168
private function runWorker(string $failureTransportName, ReceiverInterface $receiver, SymfonyStyle $io, bool $shouldForce): int
167169
{
170+
$this->eventDispatcher->addListener(WorkerStartedEvent::class, $signalsListener = function () {
171+
if (!\defined('SIGINT') || !SignalRegistry::isSupported()) {
172+
return;
173+
}
174+
175+
foreach ([\SIGINT, \SIGTERM] as $signal) {
176+
pcntl_signal($signal, $this->getApplication()->getSignalRegistry()->handle(...));
177+
}
178+
});
179+
168180
$count = 0;
169181
$listener = function (WorkerMessageReceivedEvent $messageReceivedEvent) use ($io, $receiver, $shouldForce, &$count) {
170182
++$count;
@@ -197,6 +209,7 @@ private function runWorker(string $failureTransportName, ReceiverInterface $rece
197209
try {
198210
$worker->run();
199211
} finally {
212+
$this->eventDispatcher->removeListener(WorkerStartedEvent::class, $signalsListener);
200213
$this->eventDispatcher->removeListener(WorkerMessageReceivedEvent::class, $listener);
201214
}
202215

src/Symfony/Component/Messenger/Tests/Command/FailedMessagesRetryCommandTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Messenger\Tests\Command;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Console\Application;
1516
use Symfony\Component\Console\Tester\CommandCompletionTester;
1617
use Symfony\Component\Console\Tester\CommandTester;
1718
use Symfony\Component\DependencyInjection\ServiceLocator;
@@ -60,6 +61,7 @@ public function testBasicRunWithServiceLocator()
6061
$bus,
6162
$dispatcher
6263
);
64+
(new Application())->add($command);
6365

6466
$tester = new CommandTester($command);
6567
$tester->execute(['id' => [10, 12], '--force' => true]);
@@ -138,6 +140,7 @@ public function testBasicRunWithServiceLocatorWithSpecificFailureTransport()
138140
$bus,
139141
$dispatcher
140142
);
143+
(new Application())->add($command);
141144

142145
$tester = new CommandTester($command);
143146
$tester->execute(['id' => [10, 12], '--transport' => $failureTransportName, '--force' => true]);

0 commit comments

Comments
 (0)