Skip to content

Commit 2deab7e

Browse files
committed
queue consumer options
1 parent 8364a98 commit 2deab7e

12 files changed

Lines changed: 260 additions & 0 deletions

File tree

‎pkg/enqueue-bundle/DependencyInjection/Configuration.php‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,18 @@ public function getConfigTreeBuilder()
5050
->scalarNode('default_processor_queue')->defaultValue(Config::DEFAULT_PROCESSOR_QUEUE_NAME)->cannotBeEmpty()->end()
5151
->integerNode('redelivered_delay_time')->min(0)->defaultValue(0)->end()
5252
->end()->end()
53+
->arrayNode('consumption')->addDefaultsIfNotSet()->children()
54+
->integerNode('idle_timeout')
55+
->min(0)
56+
->defaultValue(0)
57+
->info('the time in milliseconds queue consumer waits if no message received')
58+
->end()
59+
->integerNode('receive_timeout')
60+
->min(0)
61+
->defaultValue(100)
62+
->info('the time in milliseconds queue consumer waits for a message (100 ms by default)')
63+
->end()
64+
->end()->end()
5365
->booleanNode('job')->defaultFalse()->end()
5466
->arrayNode('async_events')
5567
->addDefaultsIfNotSet()

‎pkg/enqueue-bundle/DependencyInjection/EnqueueExtension.php‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,19 @@ public function load(array $configs, ContainerBuilder $container)
110110
}
111111
}
112112

113+
// configure queue consumer
114+
$container->getDefinition('enqueue.consumption.queue_consumer')
115+
->setArgument(2, $config['consumption']['idle_timeout'])
116+
->setArgument(3, $config['consumption']['receive_timeout'])
117+
;
118+
119+
if ($container->hasDefinition('enqueue.client.queue_consumer')) {
120+
$container->getDefinition('enqueue.client.queue_consumer')
121+
->setArgument(2, $config['consumption']['idle_timeout'])
122+
->setArgument(3, $config['consumption']['receive_timeout'])
123+
;
124+
}
125+
113126
if ($config['job']) {
114127
if (false == class_exists(Job::class)) {
115128
throw new \LogicException('Seems "enqueue/job-queue" is not installed. Please fix this issue.');

‎pkg/enqueue-bundle/Tests/Unit/DependencyInjection/ConfigurationTest.php‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,4 +423,25 @@ public function testShouldAllowEnableAsyncEvents()
423423
],
424424
], $config);
425425
}
426+
427+
public function testShouldAllowConfigureConsumption()
428+
{
429+
$configuration = new Configuration([]);
430+
431+
$processor = new Processor();
432+
$config = $processor->processConfiguration($configuration, [[
433+
'transport' => [],
434+
'consumption' => [
435+
'idle_timeout' => 123,
436+
'receive_timeout' => 456,
437+
],
438+
]]);
439+
440+
$this->assertArraySubset([
441+
'consumption' => [
442+
'idle_timeout' => 123,
443+
'receive_timeout' => 456,
444+
],
445+
], $config);
446+
}
426447
}

‎pkg/enqueue-bundle/Tests/Unit/DependencyInjection/EnqueueExtensionTest.php‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,4 +535,28 @@ public function testShouldNotAddJobQueueEntityMappingIfDoctrineBundleIsNotRegist
535535

536536
$this->assertSame([], $container->getExtensionConfig('doctrine'));
537537
}
538+
539+
public function testShouldConfigureQueueConsumer()
540+
{
541+
$container = new ContainerBuilder();
542+
543+
$extension = new EnqueueExtension();
544+
$extension->load([[
545+
'client' => [],
546+
'transport' => [
547+
],
548+
'consumption' => [
549+
'idle-timeout' => 123,
550+
'receive-timeout' => 456,
551+
],
552+
]], $container);
553+
554+
$def = $container->getDefinition('enqueue.consumption.queue_consumer');
555+
$this->assertSame(123, $def->getArgument(2));
556+
$this->assertSame(456, $def->getArgument(3));
557+
558+
$def = $container->getDefinition('enqueue.client.queue_consumer');
559+
$this->assertSame(123, $def->getArgument(2));
560+
$this->assertSame(456, $def->getArgument(3));
561+
}
538562
}

‎pkg/enqueue/Consumption/QueueConsumer.php‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,38 @@ public function __construct(
6363
$this->boundProcessors = [];
6464
}
6565

66+
/**
67+
* @param int $timeout
68+
*/
69+
public function setIdleTimeout($timeout)
70+
{
71+
$this->idleTimeout = (int) $timeout;
72+
}
73+
74+
/**
75+
* @return int
76+
*/
77+
public function getIdleTimeout()
78+
{
79+
return $this->idleTimeout;
80+
}
81+
82+
/**
83+
* @param int $timeout
84+
*/
85+
public function setReceiveTimeout($timeout)
86+
{
87+
$this->receiveTimeout = (int) $timeout;
88+
}
89+
90+
/**
91+
* @return int
92+
*/
93+
public function getReceiveTimeout()
94+
{
95+
return $this->receiveTimeout;
96+
}
97+
6698
/**
6799
* @return PsrContext
68100
*/

‎pkg/enqueue/Symfony/Client/ConsumeMessagesCommand.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Enqueue\Consumption\Extension\LoggerExtension;
1010
use Enqueue\Consumption\QueueConsumer;
1111
use Enqueue\Symfony\Consumption\LimitsExtensionsCommandTrait;
12+
use Enqueue\Symfony\Consumption\QueueConsumerOptionsCommandTrait;
1213
use Symfony\Component\Console\Command\Command;
1314
use Symfony\Component\Console\Input\InputArgument;
1415
use Symfony\Component\Console\Input\InputInterface;
@@ -19,6 +20,7 @@ class ConsumeMessagesCommand extends Command
1920
{
2021
use LimitsExtensionsCommandTrait;
2122
use SetupBrokerExtensionCommandTrait;
23+
use QueueConsumerOptionsCommandTrait;
2224

2325
/**
2426
* @var QueueConsumer
@@ -67,6 +69,7 @@ protected function configure()
6769
{
6870
$this->configureLimitsExtensions();
6971
$this->configureSetupBrokerExtension();
72+
$this->configureQueueConsumerOptions();
7073

7174
$this
7275
->setName('enqueue:consume')
@@ -83,6 +86,8 @@ protected function configure()
8386
*/
8487
protected function execute(InputInterface $input, OutputInterface $output)
8588
{
89+
$this->setQueueConsumerOptions($this->consumer, $input);
90+
8691
$queueMetas = [];
8792
if ($clientQueueNames = $input->getArgument('client-queue-names')) {
8893
foreach ($clientQueueNames as $clientQueueName) {

‎pkg/enqueue/Symfony/Consumption/ConsumeMessagesCommand.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class ConsumeMessagesCommand extends Command implements ContainerAwareInterface
1616
{
1717
use ContainerAwareTrait;
1818
use LimitsExtensionsCommandTrait;
19+
use QueueConsumerOptionsCommandTrait;
1920

2021
/**
2122
* @var QueueConsumer
@@ -38,6 +39,7 @@ public function __construct(QueueConsumer $consumer)
3839
protected function configure()
3940
{
4041
$this->configureLimitsExtensions();
42+
$this->configureQueueConsumerOptions();
4143

4244
$this
4345
->setName('enqueue:transport:consume')
@@ -51,6 +53,8 @@ protected function configure()
5153
*/
5254
protected function execute(InputInterface $input, OutputInterface $output)
5355
{
56+
$this->setQueueConsumerOptions($this->consumer, $input);
57+
5458
$extensions = $this->getLimitsExtensions($input, $output);
5559
array_unshift($extensions, new LoggerExtension(new ConsoleLogger($output)));
5660

‎pkg/enqueue/Symfony/Consumption/ContainerAwareConsumeMessagesCommand.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class ContainerAwareConsumeMessagesCommand extends Command implements ContainerA
2020
{
2121
use ContainerAwareTrait;
2222
use LimitsExtensionsCommandTrait;
23+
use QueueConsumerOptionsCommandTrait;
2324

2425
/**
2526
* @var QueueConsumer
@@ -44,6 +45,7 @@ public function __construct(QueueConsumer $consumer)
4445
protected function configure()
4546
{
4647
$this->configureLimitsExtensions();
48+
$this->configureQueueConsumerOptions();
4749

4850
$this
4951
->setName('enqueue:transport:consume')
@@ -60,6 +62,8 @@ protected function configure()
6062
*/
6163
protected function execute(InputInterface $input, OutputInterface $output)
6264
{
65+
$this->setQueueConsumerOptions($this->consumer, $input);
66+
6367
/** @var PsrProcessor $processor */
6468
$processor = $this->container->get($input->getArgument('processor-service'));
6569
if (false == $processor instanceof PsrProcessor) {
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Enqueue\Symfony\Consumption;
4+
5+
use Enqueue\Consumption\QueueConsumer;
6+
use Symfony\Component\Console\Input\InputInterface;
7+
use Symfony\Component\Console\Input\InputOption;
8+
9+
trait QueueConsumerOptionsCommandTrait
10+
{
11+
/**
12+
* {@inheritdoc}
13+
*/
14+
protected function configureQueueConsumerOptions()
15+
{
16+
$this
17+
->addOption('idle-timeout', null, InputOption::VALUE_REQUIRED, 'The time in milliseconds queue consumer waits if no message received.')
18+
->addOption('receive-timeout', null, InputOption::VALUE_REQUIRED, 'The time in milliseconds queue consumer waits for a message.')
19+
;
20+
}
21+
22+
/**
23+
* @param QueueConsumer $consumer
24+
* @param InputInterface $input
25+
*/
26+
protected function setQueueConsumerOptions(QueueConsumer $consumer, InputInterface $input)
27+
{
28+
if (null !== $idleTimeout = $input->getOption('idle-timeout')) {
29+
$consumer->setIdleTimeout((int) $idleTimeout);
30+
}
31+
32+
if (null !== $receiveTimeout = $input->getOption('receive-timeout')) {
33+
$consumer->setReceiveTimeout((int) $receiveTimeout);
34+
}
35+
}
36+
}

‎pkg/enqueue/Tests/Consumption/QueueConsumerTest.php‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,24 @@ public function testThrowIfProcessorNeitherInstanceOfProcessorNorCallable()
108108
$consumer->bind(new NullQueue(''), new \stdClass());
109109
}
110110

111+
public function testCouldSetGetIdleTimeout()
112+
{
113+
$consumer = new QueueConsumer($this->createPsrContextStub(), null, 0);
114+
115+
$consumer->setIdleTimeout(123456);
116+
117+
$this->assertSame(123456, $consumer->getIdleTimeout());
118+
}
119+
120+
public function testCouldSetGetReceiveTimeout()
121+
{
122+
$consumer = new QueueConsumer($this->createPsrContextStub(), null, 0);
123+
124+
$consumer->setReceiveTimeout(123456);
125+
126+
$this->assertSame(123456, $consumer->getReceiveTimeout());
127+
}
128+
111129
public function testShouldAllowBindCallbackToQueueName()
112130
{
113131
$callback = function () {

0 commit comments

Comments
 (0)