Skip to content

keep_alive option for batch consumers #496 #518

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 1 commit into from
Jan 18, 2018
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
1 change: 1 addition & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ protected function addBatchConsumers(ArrayNodeDefinition $node)
->scalarNode('idle_timeout')->end()
->scalarNode('timeout_wait')->defaultValue(3)->end()
->scalarNode('idle_timeout_exit_code')->end()
->scalarNode('keep_alive')->defaultFalse()->end()
->scalarNode('auto_setup_fabric')->defaultTrue()->end()
->arrayNode('qos_options')
->children()
Expand Down
4 changes: 4 additions & 0 deletions DependencyInjection/OldSoundRabbitMqExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,10 @@ protected function loadBatchConsumers()
$definition->addMethodCall('disableAutoSetupFabric');
}

if ($consumer['keep_alive']) {
$definition->addMethodCall('keepAlive');
}

$this->injectConnection($definition, $consumer['connection']);
if ($this->collectorEnabled) {
$this->injectLoggedChannel($definition, $key, $consumer['connection']);
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -810,8 +810,11 @@ batch_consumers:
timeout_wait: 5
auto_setup_fabric: false
idle_timeout_exit_code: -2
keep_alive: false
```

*Note*: If the `keep_alive` option is set to `true`, `idle_timeout_exit_code` will be ignored and the consumer process continues.

You can implement a batch consumer that will acknowledge all messages in one return or you can have control on what message to acknoledge.

```php
Expand Down
19 changes: 19 additions & 0 deletions RabbitMq/BatchConsumer.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ final class BatchConsumer extends BaseAmqp implements DequeuerInterface
*/
private $idleTimeout = 0;

/**
* @var bool
*/
private $keepAlive = false;

/**
* @var int
*/
Expand Down Expand Up @@ -84,6 +89,8 @@ public function consume()
} catch (AMQPTimeoutException $e) {
if (!$this->isEmptyBatch()) {
$this->batchConsume();
} elseif ($this->keepAlive === true) {
continue;
} elseif (null !== $this->getIdleTimeoutExitCode()) {
return $this->getIdleTimeoutExitCode();
} else {
Expand Down Expand Up @@ -399,6 +406,18 @@ public function setIdleTimeoutExitCode($idleTimeoutExitCode)
return $this;
}

/**
* keepAlive
*
* @return $this
*/
public function keepAlive()
{
$this->keepAlive = true;

return $this;
}

/**
* Purge the queue
*/
Expand Down