Skip to content

Graceful max execution time for batch consumer #521

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
6 changes: 6 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,12 @@ protected function addBatchConsumers(ArrayNodeDefinition $node)
->scalarNode('timeout_wait')->defaultValue(3)->end()
->scalarNode('idle_timeout_exit_code')->end()
->scalarNode('keep_alive')->defaultFalse()->end()
->arrayNode('graceful_max_execution')
->canBeUnset()
->children()
->integerNode('timeout')->end()
->end()
->end()
->scalarNode('auto_setup_fabric')->defaultTrue()->end()
->arrayNode('qos_options')
->children()
Expand Down
7 changes: 7 additions & 0 deletions DependencyInjection/OldSoundRabbitMqExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,13 @@ protected function loadBatchConsumers()
$definition->addMethodCall('setIdleTimeout', array($consumer['idle_timeout']));
}

if (isset($consumer['graceful_max_execution'])) {
$definition->addMethodCall(
'setGracefulMaxExecutionDateTimeFromSecondsInTheFuture',
array($consumer['graceful_max_execution']['timeout'])
);
}

if (!$consumer['auto_setup_fabric']) {
$definition->addMethodCall('disableAutoSetupFabric');
}
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,8 @@ batch_consumers:
auto_setup_fabric: false
idle_timeout_exit_code: -2
keep_alive: false
graceful_max_execution:
timeout: 60
```

*Note*: If the `keep_alive` option is set to `true`, `idle_timeout_exit_code` will be ignored and the consumer process continues.
Expand Down
63 changes: 53 additions & 10 deletions RabbitMq/BatchConsumer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@
use PhpAmqpLib\Exception\AMQPTimeoutException;
use PhpAmqpLib\Message\AMQPMessage;

final class BatchConsumer extends BaseAmqp implements DequeuerInterface
class BatchConsumer extends BaseAmqp implements DequeuerInterface
{
/**
* @var \Closure|callable
*/
private $callback;
protected $callback;

/**
* @var bool
*/
private $forceStop = false;
protected $forceStop = false;

/**
* @var int
*/
private $idleTimeout = 0;
protected $idleTimeout = 0;

/**
* @var bool
Expand All @@ -32,32 +32,54 @@ final class BatchConsumer extends BaseAmqp implements DequeuerInterface
/**
* @var int
*/
private $idleTimeoutExitCode;
protected $idleTimeoutExitCode;

/**
* @var int
*/
private $memoryLimit = null;
protected $memoryLimit = null;

/**
* @var int
*/
private $prefetchCount;
protected $prefetchCount;

/**
* @var int
*/
private $timeoutWait = 3;
protected $timeoutWait = 3;

/**
* @var array
*/
private $messages = array();
protected $messages = array();

/**
* @var int
*/
private $batchCounter = 0;
protected $batchCounter = 0;

/**
* @var \DateTime|null DateTime after which the consumer will gracefully exit. "Gracefully" means, that
* any currently running consumption will not be interrupted.
*/
protected $gracefulMaxExecutionDateTime;

/**
* @param \DateTime|null $dateTime
*/
public function setGracefulMaxExecutionDateTime(\DateTime $dateTime = null)
{
$this->gracefulMaxExecutionDateTime = $dateTime;
}

/**
* @param int $secondsInTheFuture
*/
public function setGracefulMaxExecutionDateTimeFromSecondsInTheFuture($secondsInTheFuture)
{
$this->setGracefulMaxExecutionDateTime(new \DateTime("+{$secondsInTheFuture} seconds"));
}

/**
* @param \Closure|callable $callback
Expand All @@ -80,6 +102,7 @@ public function consume()
$this->batchConsume();
}

$this->checkGracefulMaxExecutionDateTime();
$this->maybeStopConsumer();

$timeout = $this->isEmptyBatch() ? $this->getIdleTimeout() : $this->getTimeoutWait();
Expand Down Expand Up @@ -530,4 +553,24 @@ public function getMemoryLimit()
{
return $this->memoryLimit;
}

/**
* Check graceful max execution date time and stop if limit is reached
*
* @return void
*/
private function checkGracefulMaxExecutionDateTime()
{
if (!$this->gracefulMaxExecutionDateTime) {
return;
}

$now = new \DateTime();

if ($this->gracefulMaxExecutionDateTime > $now) {
return;
}

$this->forceStopConsumer();
}
}