Skip to content

Commit 36963b3

Browse files
author
Eligijus Vitkauskas
committed
Implement an ability to configure worker options
1 parent c7ec058 commit 36963b3

File tree

5 files changed

+71
-8
lines changed

5 files changed

+71
-8
lines changed

DependencyInjection/Configuration.php

+33
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public function getConfigTreeBuilder()
2727
->end();
2828
$this->addPaths($rootNode);
2929
$this->addCommands($rootNode);
30+
$this->addWorkerOptions($rootNode);
3031

3132
return $tree;
3233
}
@@ -79,4 +80,36 @@ protected function addCommands(ArrayNodeDefinition $node)
7980
->end()
8081
;
8182
}
83+
84+
/**
85+
* Add worker options configuration
86+
*
87+
* @param ArrayNodeDefinition $node
88+
*/
89+
protected function addWorkerOptions(ArrayNodeDefinition $node)
90+
{
91+
$node
92+
->children()
93+
->arrayNode('worker_options')
94+
->addDefaultsIfNotSet()
95+
->children()
96+
->integerNode('startsecs')
97+
->min(0)
98+
->defaultValue(2)
99+
->end()
100+
->booleanNode('autorestart')->defaultTrue()->end()
101+
->enumNode('stopsignal')
102+
->values(array('TERM', 'INT', 'KILL'))
103+
->defaultValue('INT')
104+
->end()
105+
->booleanNode('stopasgroup')->defaultTrue()->end()
106+
->integerNode('stopwaitsecs')
107+
->min(0)
108+
->defaultValue(60)
109+
->end()
110+
->end()
111+
->end()
112+
->end()
113+
;
114+
}
82115
}

DependencyInjection/RabbitMqSupervisorExtension.php

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public function load(array $configs, ContainerBuilder $container)
3030
$container->setParameter('phobetor_rabbitmq_supervisor.workspace', $config['paths']['workspace_directory']);
3131
$container->setParameter('phobetor_rabbitmq_supervisor.configuration_file', $config['paths']['configuration_file']);
3232
$container->setParameter('phobetor_rabbitmq_supervisor.commands', $config['commands']);
33+
$container->setParameter('phobetor_rabbitmq_supervisor.worker_options', $config['worker_options']);
3334
}
3435

3536
public function prepend(ContainerBuilder $container)

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@ rabbit_mq_supervisor:
9595
commands:
9696
rabbitmq_consumer: user-specific-command:consumer -m %%1$d %%2$s
9797
rabbitmq_multiple_consumer: user-specific-command:multiple-consumer -m %%1$d %%2$s
98+
worker_options:
99+
startsecs: 2
100+
autorestart: true
101+
stopsignal: INT
102+
stopasgroup: true
103+
stopwaitsecs: 60
98104
```
99105
100106
## Usage

Resources/config/services.yml

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ services:
1313
- "%phobetor_rabbitmq_supervisor.consumers%"
1414
- "%phobetor_rabbitmq_supervisor.multiple_consumers%"
1515
- "%phobetor_rabbitmq_supervisor.worker_count%"
16+
- "%phobetor_rabbitmq_supervisor.worker_options%"
1617

1718
phobetor_rabbitmq_supervisor.supervisor_service:
1819
class: "%phobetor_rabbitmq_supervisor.supervisor_service.class%"

Services/RabbitMqSupervisor.php

+30-8
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ class RabbitMqSupervisor
4444
*/
4545
private $workerCount;
4646

47+
/**
48+
* @var array
49+
*/
50+
private $workerOptions;
51+
4752
/**
4853
* Initialize Handler
4954
*
@@ -54,8 +59,9 @@ class RabbitMqSupervisor
5459
* @param array $consumers
5560
* @param array $multipleConsumers
5661
* @param int $workerCount
62+
* @param array $workerOptions
5763
*/
58-
public function __construct(Supervisor $supervisor, EngineInterface $templating, array $paths, array $commands, $consumers, $multipleConsumers, $workerCount)
64+
public function __construct(Supervisor $supervisor, EngineInterface $templating, array $paths, array $commands, $consumers, $multipleConsumers, $workerCount, array $workerOptions = array())
5965
{
6066
$this->supervisor = $supervisor;
6167
$this->templating = $templating;
@@ -64,6 +70,7 @@ public function __construct(Supervisor $supervisor, EngineInterface $templating,
6470
$this->consumers = $consumers;
6571
$this->multipleConsumers = $multipleConsumers;
6672
$this->workerCount = $workerCount;
73+
$this->workerOptions = $workerOptions;
6774
}
6875

6976
/**
@@ -271,13 +278,7 @@ private function generateWorkerConfigurations($names, $command, $maxMessages = 2
271278
'workerOutputLog' => $this->paths['worker_output_log_file'],
272279
'workerErrorLog' => $this->paths['worker_error_log_file'],
273280
'numprocs' => $this->workerCount,
274-
'options' => array(
275-
'startsecs' => '2',
276-
'autorestart' => 'true',
277-
'stopsignal' => 'INT',
278-
'stopasgroup' => 'true',
279-
'stopwaitsecs' => '60',
280-
)
281+
'options' => $this->transformBoolsToStrings($this->workerOptions),
281282
)
282283
);
283284
}
@@ -303,4 +304,25 @@ private function createSupervisorConfigurationFilePath()
303304
{
304305
return $this->paths['configuration_file'];
305306
}
307+
308+
/**
309+
* Transform bool array values to string representation.
310+
*
311+
* @param array $options
312+
*
313+
* @return array
314+
*/
315+
private function transformBoolsToStrings(array $options)
316+
{
317+
$transformedOptions = array();
318+
foreach ($options as $key => $value) {
319+
if (is_bool($value)) {
320+
$value = $value ? 'true' : 'false';
321+
}
322+
323+
$transformedOptions[$key] = $value;
324+
}
325+
326+
return $transformedOptions;
327+
}
306328
}

0 commit comments

Comments
 (0)