Skip to content

Commit 5bc2b63

Browse files
author
d-ph
committed
Add graceful max execution timeout.
Apply requested changes.
1 parent 2037c2a commit 5bc2b63

File tree

5 files changed

+20
-19
lines changed

5 files changed

+20
-19
lines changed

DependencyInjection/Configuration.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,13 @@ protected function addConsumers(ArrayNodeDefinition $node)
138138
->scalarNode('callback')->isRequired()->end()
139139
->scalarNode('idle_timeout')->end()
140140
->scalarNode('idle_timeout_exit_code')->end()
141-
->scalarNode('graceful_max_execution_timeout')->end()
142-
->scalarNode('graceful_max_execution_timeout_exit_code')->end()
141+
->arrayNode('graceful_max_execution')
142+
->canBeUnset()
143+
->children()
144+
->integerNode('timeout')->end()
145+
->integerNode('exit_code')->defaultValue(0)->end()
146+
->end()
147+
->end()
143148
->scalarNode('auto_setup_fabric')->defaultTrue()->end()
144149
->arrayNode('qos_options')
145150
->canBeUnset()

DependencyInjection/OldSoundRabbitMqExtension.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,16 +200,14 @@ protected function loadConsumers()
200200
if (isset($consumer['idle_timeout_exit_code'])) {
201201
$definition->addMethodCall('setIdleTimeoutExitCode', array($consumer['idle_timeout_exit_code']));
202202
}
203-
if (isset($consumer['graceful_max_execution_timeout'])) {
203+
if (isset($consumer['graceful_max_execution'])) {
204204
$definition->addMethodCall(
205205
'setGracefulMaxExecutionDateTimeFromSecondsInTheFuture',
206-
array($consumer['graceful_max_execution_timeout'])
206+
array($consumer['graceful_max_execution']['timeout'])
207207
);
208-
}
209-
if (isset($consumer['graceful_max_execution_timeout_exit_code'])) {
210208
$definition->addMethodCall(
211209
'setGracefulMaxExecutionTimeoutExitCode',
212-
array($consumer['graceful_max_execution_timeout_exit_code'])
210+
array($consumer['graceful_max_execution']['exit_code'])
213211
);
214212
}
215213
if (!$consumer['auto_setup_fabric']) {

README.md

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -395,9 +395,9 @@ consumers:
395395
396396
#### Graceful max execution timeout ####
397397
398-
If you'd like your consumer to be running up to certain time and then gracefully exit, then set the `graceful_max_execution_timeout` in seconds.
398+
If you'd like your consumer to be running up to certain time and then gracefully exit, then set the `graceful_max_execution.timeout` in seconds.
399399
"Gracefully exit" means, that the consumer will exit either after the currently running task or immediatelly, when waiting for new tasks.
400-
The `graceful_max_execution_timeout_exit_code` specifies what exit code should be returned by the consumer when the graceful max execution timeout occurs. Without specifying it, the consumer will exit with status `0`.
400+
The `graceful_max_execution.exit_code` specifies what exit code should be returned by the consumer when the graceful max execution timeout occurs. Without specifying it, the consumer will exit with status `0`.
401401
402402
This feature is great in conjuction with supervisord, which together can allow for periodical memory leaks cleanup, connection with database/rabbitmq renewal and more.
403403
@@ -408,12 +408,10 @@ consumers:
408408
exchange_options: {name: 'upload-picture', type: direct}
409409
queue_options: {name: 'upload-picture'}
410410
callback: upload_picture_service
411-
412-
# 30 minutes
413-
graceful_max_execution_timeout: 1800
414-
415-
# default is 0
416-
graceful_max_execution_timeout_exit_code: 10
411+
412+
graceful_max_execution:
413+
timeout: 1800 # 30 minutes
414+
exit_code: 10 # default is 0
417415
```
418416
419417
#### Fair dispatching ####

RabbitMq/Consumer.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,15 @@ public function consume($msgAmount)
7777
$waitTimeout['timeoutType'] === self::TIMEOUT_TYPE_GRACEFUL_MAX_EXECUTION
7878
&& $waitTimeout['seconds'] < 1
7979
) {
80-
return $this->getGracefulMaxExecutionTimeoutExitCode();
80+
return $this->gracefulMaxExecutionTimeoutExitCode;
8181
}
8282

8383
if (!$this->forceStop) {
8484
try {
8585
$this->getChannel()->wait(null, false, $waitTimeout['seconds']);
8686
} catch (AMQPTimeoutException $e) {
8787
if (self::TIMEOUT_TYPE_GRACEFUL_MAX_EXECUTION === $waitTimeout['timeoutType']) {
88-
return $this->getGracefulMaxExecutionTimeoutExitCode();
88+
return $this->gracefulMaxExecutionTimeoutExitCode;
8989
}
9090

9191
$idleEvent = new OnIdleEvent($this);
@@ -214,7 +214,7 @@ protected function isRamAlmostOverloaded()
214214
/**
215215
* @param \DateTime|null $dateTime
216216
*/
217-
public function setGracefulMaxExecutionDateTime($dateTime = null)
217+
public function setGracefulMaxExecutionDateTime(\DateTime $dateTime = null)
218218
{
219219
$this->gracefulMaxExecutionDateTime = $dateTime;
220220
}

Tests/RabbitMq/ConsumerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ public function testGracefulMaxExecutionTimeoutExitCode()
285285
->method('wait')
286286
->willThrowException(new AMQPTimeoutException());
287287

288-
$this->assertTrue(10 == $consumer->consume(1));
288+
$this->assertSame(10, $consumer->consume(1));
289289
}
290290

291291
public function testGracefulMaxExecutionWontWaitIfPastTheTimeout()

0 commit comments

Comments
 (0)