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

Skip to content

Commit dac1bd8

Browse files
committed
Fixes #43866 configurable stop signals for messenger
1 parent 8e8207b commit dac1bd8

File tree

7 files changed

+111
-39
lines changed

7 files changed

+111
-39
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php

+13-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use Psr\Log\LogLevel;
1717
use Symfony\Bundle\FullStack;
1818
use Symfony\Component\Asset\Package;
19-
use Symfony\Component\Cache\Adapter\DoctrineAdapter;
2019
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
2120
use Symfony\Component\Config\Definition\Builder\NodeBuilder;
2221
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
@@ -1050,7 +1049,7 @@ private function addCacheSection(ArrayNodeDefinition $rootNode, callable $willBe
10501049
->scalarNode('default_redis_provider')->defaultValue('redis://localhost')->end()
10511050
->scalarNode('default_memcached_provider')->defaultValue('memcached://localhost')->end()
10521051
->scalarNode('default_doctrine_dbal_provider')->defaultValue('database_connection')->end()
1053-
->scalarNode('default_pdo_provider')->defaultValue($willBeAvailable('doctrine/dbal', Connection::class) && class_exists(DoctrineAdapter::class) ? 'database_connection' : null)->end()
1052+
->scalarNode('default_pdo_provider')->defaultValue($willBeAvailable('doctrine/dbal', Connection::class) ? 'database_connection' : null)->end()
10541053
->arrayNode('pools')
10551054
->useAttributeAsKey('name')
10561055
->prototype('array')
@@ -1308,6 +1307,18 @@ private function addMessengerSection(ArrayNodeDefinition $rootNode, callable $en
13081307
->then(static function (array $v): void { throw new InvalidConfigurationException(sprintf('The specified default bus "%s" is not configured. Available buses are "%s".', $v['default_bus'], implode('", "', array_keys($v['buses'])))); })
13091308
->end()
13101309
->children()
1310+
->arrayNode('stop_signals')
1311+
->beforeNormalization()
1312+
->ifString()->then(static fn (string $value): int => constant($value))
1313+
->castToArray()
1314+
->end()
1315+
->validate()
1316+
->ifFalse(static fn ($v): bool => function_exists('pcntl_signal'))
1317+
->then(static function (): void { throw new InvalidConfigurationException('You must install pcntl extension in order to use stop_signals.'); })
1318+
->ifNull()->then(static function (): void { throw new InvalidConfigurationException('Invalid signal passed to stop_signals'); })
1319+
->end()
1320+
->scalarPrototype()->end()
1321+
13111322
->arrayNode('routing')
13121323
->normalizeKeys(false)
13131324
->useAttributeAsKey('message_class')

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

+4-8
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
namespace Symfony\Bundle\FrameworkBundle\DependencyInjection;
1313

14-
use Composer\InstalledVersions;
1514
use Doctrine\Common\Annotations\AnnotationRegistry;
1615
use Doctrine\Common\Annotations\Reader;
1716
use Http\Client\HttpClient;
@@ -247,10 +246,6 @@ public function load(array $configs, ContainerBuilder $container)
247246
{
248247
$loader = new PhpFileLoader($container, new FileLocator(\dirname(__DIR__).'/Resources/config'));
249248

250-
if (class_exists(InstalledVersions::class) && InstalledVersions::isInstalled('symfony/symfony') && 'symfony/symfony' !== (InstalledVersions::getRootPackage()['name'] ?? '')) {
251-
trigger_deprecation('symfony/symfony', '6.1', 'Requiring the "symfony/symfony" package is deprecated; replace it with standalone components instead.');
252-
}
253-
254249
$loader->load('web.php');
255250

256251
if (!class_exists(BackedEnumValueResolver::class)) {
@@ -1615,10 +1610,9 @@ private function registerAnnotationsConfiguration(array $config, ContainerBuilde
16151610

16161611
$container
16171612
->getDefinition('annotations.cached_reader')
1618-
->setPublic(true) // set to false in AddAnnotationsCachedReaderPass
16191613
->replaceArgument(2, $config['debug'])
1620-
// reference the cache provider without using it until AddAnnotationsCachedReaderPass runs
1621-
->addArgument(new ServiceClosureArgument(new Reference($cacheService)))
1614+
// temporary property to lazy-reference the cache provider without using it until AddAnnotationsCachedReaderPass runs
1615+
->setProperty('cacheProviderBackup', new ServiceClosureArgument(new Reference($cacheService)))
16221616
->addTag('annotations.cached_reader')
16231617
;
16241618

@@ -1914,6 +1908,8 @@ private function registerMessengerConfiguration(array $config, ContainerBuilder
19141908
$config['default_bus'] = key($config['buses']);
19151909
}
19161910

1911+
$container->getDefinition('stop_worker_on_sigterm_signal_listener')->setArgument('$signals', $config['stop_signals'] ?? \SIGTERM);
1912+
19171913
$defaultMiddleware = [
19181914
'before' => [
19191915
['id' => 'add_bus_name_stamp_middleware'],

src/Symfony/Bundle/FrameworkBundle/Resources/config/messenger.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Symfony\Component\Messenger\EventListener\SendFailedMessageToFailureTransportListener;
2424
use Symfony\Component\Messenger\EventListener\StopWorkerOnCustomStopExceptionListener;
2525
use Symfony\Component\Messenger\EventListener\StopWorkerOnRestartSignalListener;
26+
use Symfony\Component\Messenger\EventListener\StopWorkerOnSignalListener;
2627
use Symfony\Component\Messenger\EventListener\StopWorkerOnSigtermSignalListener;
2728
use Symfony\Component\Messenger\Middleware\AddBusNameStampMiddleware;
2829
use Symfony\Component\Messenger\Middleware\DispatchAfterCurrentBusMiddleware;
@@ -192,8 +193,9 @@
192193
->tag('kernel.event_subscriber')
193194
->tag('monolog.logger', ['channel' => 'messenger'])
194195

195-
->set('messenger.listener.stop_worker_on_sigterm_signal_listener', StopWorkerOnSigtermSignalListener::class)
196+
->set('messenger.listener.stop_worker_on_signal_listener', StopWorkerOnSignalListener::class)
196197
->args([
198+
abstract_arg('signals'),
197199
service('logger')->ignoreOnInvalid(),
198200
])
199201
->tag('kernel.event_subscriber')

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php

+86-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use PHPUnit\Framework\TestCase;
1616
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Configuration;
1717
use Symfony\Bundle\FullStack;
18-
use Symfony\Component\Cache\Adapter\DoctrineAdapter;
1918
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
2019
use Symfony\Component\Config\Definition\Processor;
2120
use Symfony\Component\DependencyInjection\ContainerBuilder;
@@ -265,6 +264,91 @@ public function testLockMergeConfigs()
265264
);
266265
}
267266

267+
public function testMessengerWithoutPcntl()
268+
{
269+
$this->expectException(InvalidConfigurationException::class);
270+
$this->expectExceptionMessage('You must install pcntl extension in order to use stop_signals.');
271+
$processor = new Processor();
272+
$configuration = new Configuration(true);
273+
274+
$processor->processConfiguration($configuration, [
275+
'framework' => [
276+
'messenger' => [
277+
'stop_signals' => \SIGTERM
278+
],
279+
],
280+
]);
281+
}
282+
283+
public function testMessengerWithoutPcntlWithoutStopSignals()
284+
{
285+
$processor = new Processor();
286+
$configuration = new Configuration(true);
287+
288+
$processor->processConfiguration($configuration, [
289+
'framework' => [
290+
'messenger' => [
291+
],
292+
],
293+
]);
294+
295+
$this->expectNotToPerformAssertions();
296+
}
297+
298+
public function testMessengerWithInvalidSignal()
299+
{
300+
$this->expectException(InvalidConfigurationException::class);
301+
$this->expectExceptionMessage('Invalid signal passed to stop_signals');
302+
$processor = new Processor();
303+
$configuration = new Configuration(true);
304+
305+
$processor->processConfiguration($configuration, [
306+
'framework' => [
307+
'messenger' => [
308+
'stop_signals' => \SIG_IGN
309+
],
310+
],
311+
]);
312+
}
313+
314+
public function testMessengerWithMultipleStopSignals()
315+
{
316+
$processor = new Processor();
317+
$configuration = new Configuration(true);
318+
319+
$config = $processor->processConfiguration($configuration, [
320+
'framework' => [
321+
'messenger' => [
322+
'stop_signals' => [\SIGTERM, \SIGQUIT]
323+
],
324+
],
325+
]);
326+
327+
static::assertEquals(
328+
[\SIGTERM, \SIGQUIT],
329+
$config['messenger']['stop_signals']
330+
);
331+
}
332+
333+
public function testMessengerWithStringStopSignal()
334+
{
335+
$processor = new Processor();
336+
$configuration = new Configuration(true);
337+
338+
$config = $processor->processConfiguration($configuration, [
339+
'framework' => [
340+
'messenger' => [
341+
'stop_signals' => ['SIGTERM', 'SIGQUIT']
342+
],
343+
],
344+
]);
345+
346+
static::assertEquals(
347+
[\SIGTERM, \SIGQUIT],
348+
$config['messenger']['stop_signals']
349+
);
350+
}
351+
268352
public function testItShowANiceMessageIfTwoMessengerBusesAreConfiguredButNoDefaultBus()
269353
{
270354
$expectedMessage = 'You must specify the "default_bus" if you define more than one bus.';
@@ -502,7 +586,7 @@ protected static function getBundleDefaultConfig()
502586
'default_redis_provider' => 'redis://localhost',
503587
'default_memcached_provider' => 'memcached://localhost',
504588
'default_doctrine_dbal_provider' => 'database_connection',
505-
'default_pdo_provider' => ContainerBuilder::willBeAvailable('doctrine/dbal', Connection::class, ['symfony/framework-bundle']) && class_exists(DoctrineAdapter::class) ? 'database_connection' : null,
589+
'default_pdo_provider' => ContainerBuilder::willBeAvailable('doctrine/dbal', Connection::class, ['symfony/framework-bundle']) ? 'database_connection' : null,
506590
'prefix_seed' => '_%kernel.project_dir%.%kernel.container_class%',
507591
],
508592
'workflows' => [

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/messenger.php

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Messenger\FooMessage;
55

66
$container->loadFromExtension('framework', [
7+
'stop_signals'=> \SIGTERM,
78
'messenger' => [
89
'routing' => [
910
FooMessage::class => ['sender.bar', 'sender.biz'],

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/messenger.yml

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
framework:
22
messenger:
3+
stop_signals: SIGTERM
34
routing:
45
'Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Messenger\FooMessage': ['sender.bar', 'sender.biz']
56
'Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Messenger\BarMessage': 'sender.foo'

src/Symfony/Component/Messenger/EventListener/StopWorkerOnSigtermSignalListener.php

+3-26
Original file line numberDiff line numberDiff line change
@@ -12,38 +12,15 @@
1212
namespace Symfony\Component\Messenger\EventListener;
1313

1414
use Psr\Log\LoggerInterface;
15-
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
16-
use Symfony\Component\Messenger\Event\WorkerStartedEvent;
1715

1816
/**
1917
* @author Tobias Schultze <http://tobion.de>
18+
* @deprecated Use StopWorkerOnSignalListener instead.
2019
*/
21-
class StopWorkerOnSigtermSignalListener implements EventSubscriberInterface
20+
class StopWorkerOnSigtermSignalListener extends StopWorkerOnSignalListener
2221
{
23-
private ?LoggerInterface $logger;
24-
2522
public function __construct(LoggerInterface $logger = null)
2623
{
27-
$this->logger = $logger;
28-
}
29-
30-
public function onWorkerStarted(WorkerStartedEvent $event): void
31-
{
32-
pcntl_signal(\SIGTERM, function () use ($event) {
33-
$this->logger?->info('Received SIGTERM signal.', ['transport_names' => $event->getWorker()->getMetadata()->getTransportNames()]);
34-
35-
$event->getWorker()->stop();
36-
});
37-
}
38-
39-
public static function getSubscribedEvents(): array
40-
{
41-
if (!\function_exists('pcntl_signal')) {
42-
return [];
43-
}
44-
45-
return [
46-
WorkerStartedEvent::class => ['onWorkerStarted', 100],
47-
];
24+
parent::__construct(\SIGTERM, $logger);
4825
}
4926
}

0 commit comments

Comments
 (0)