Skip to content

Commit

Permalink
- Class is no longer final (#521)
Browse files Browse the repository at this point in the history
- Properties can be overwritten as far as they became protected
- Graceful max execution time has been added.
  • Loading branch information
chernecov authored and skafandri committed May 2, 2018
1 parent 95f23bb commit cf67ada
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 10 deletions.
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 @@ -416,6 +416,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();
}
}

0 comments on commit cf67ada

Please sign in to comment.