Skip to content

Commit

Permalink
replace templating usage by simple array-to-config helper method
Browse files Browse the repository at this point in the history
closes #41
  • Loading branch information
Phobetor committed May 7, 2018
1 parent fff62ad commit 30f1b83
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 66 deletions.
24 changes: 24 additions & 0 deletions Helpers/ConfigurationHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Phobetor\RabbitMqSupervisorBundle\Helpers;

class ConfigurationHelper
{
public function getConfigurationStringFromDataArray(array $data)
{
$configurationString = '';

foreach ($data as $key => $value) {
if (is_array($value)) {
$configurationString .= sprintf("[%s]\n", $key);
$configurationString .= $this->getConfigurationStringFromDataArray($value);
$configurationString .= "\n";
}
else {
$configurationString .= sprintf("%s=%s\n", $key, $value);
}
}

return $configurationString;
}
}
1 change: 0 additions & 1 deletion Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ services:
public: true
arguments:
- "@phobetor_rabbitmq_supervisor.supervisor_service"
- "@templating"
- "%phobetor_rabbitmq_supervisor.paths%"
- "%phobetor_rabbitmq_supervisor.commands%"
- "%phobetor_rabbitmq_supervisor.consumers%"
Expand Down
11 changes: 0 additions & 11 deletions Resources/views/Supervisor/program.conf.twig

This file was deleted.

27 changes: 0 additions & 27 deletions Resources/views/Supervisor/supervisord.conf.twig

This file was deleted.

54 changes: 29 additions & 25 deletions Services/RabbitMqSupervisor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Phobetor\RabbitMqSupervisorBundle\Services;

use Symfony\Component\Templating\EngineInterface;
use Phobetor\RabbitMqSupervisorBundle\Helpers\ConfigurationHelper;

/**
* @license MIT
Expand All @@ -14,11 +14,6 @@ class RabbitMqSupervisor
*/
private $supervisor;

/**
* @var \Symfony\Component\Templating\EngineInterface
*/
private $templating;

/**
* @var array
*/
Expand Down Expand Up @@ -48,17 +43,15 @@ class RabbitMqSupervisor
* Initialize Handler
*
* @param \Phobetor\RabbitMqSupervisorBundle\Services\Supervisor $supervisor
* @param \Symfony\Component\Templating\EngineInterface $templating
* @param array $paths
* @param array $commands
* @param array $consumers
* @param array $multipleConsumers
* @param array $config
*/
public function __construct(Supervisor $supervisor, EngineInterface $templating, array $paths, array $commands, $consumers, $multipleConsumers, $config)
public function __construct(Supervisor $supervisor, array $paths, array $commands, $consumers, $multipleConsumers, $config)
{
$this->supervisor = $supervisor;
$this->templating = $templating;
$this->paths = $paths;
$this->commands = $commands;
$this->consumers = $consumers;
Expand Down Expand Up @@ -237,15 +230,26 @@ private function createPathDirectories() {

public function generateSupervisorConfiguration()
{
$content = $this->templating->render(
'RabbitMqSupervisorBundle:Supervisor:supervisord.conf.twig',
array(
'pidFile' => $this->paths['pid_file'],
'sockFile' => $this->paths['sock_file'],
'logFile' => $this->paths['log_file'],
'workerConfigurationDirectory' => $this->paths['worker_configuration_directory'],
$configurationHelper = new ConfigurationHelper();
$content = $configurationHelper->getConfigurationStringFromDataArray(array(
'unix_http_server' => array(
'file' => $this->paths['sock_file'],
'chmod' => '0700'
),
'supervisord' => array(
'logfile' => $this->paths['log_file'],
'pidfile' => $this->paths['pid_file']
),
'rpcinterface:supervisor' => array(
'supervisor.rpcinterface_factory' => 'supervisor.rpcinterface:make_main_rpcinterface'
),
'supervisorctl' => array(
'serverurl' => sprintf('unix://%s', $this->paths['sock_file'])
),
'include' => array(
'files' => sprintf('%s*.conf', $this->paths['worker_configuration_directory'])
)
);
));
file_put_contents(
$this->createSupervisorConfigurationFilePath(),
$content
Expand Down Expand Up @@ -295,18 +299,17 @@ private function generateWorkerConfigurations($names, $baseCommand)
$this->generateWorkerConfiguration(
$name,
array(
'name' => $name,
'command' => $command,
'executablePath' => $executablePath,
'workerOutputLog' => $this->paths['worker_output_log_file'],
'workerErrorLog' => $this->paths['worker_error_log_file'],
'numprocs' => (int)$this->getConsumerWorkerOption($name, 'count'),
'options' => array(
sprintf('program:%s', $name) => array(
'command' => sprintf('php %s %s --env=%s', $executablePath, $command, 'prod'), // todo {{ app.environment }}
'process_name' => '%(program_name)s%(process_num)02d',
'numprocs' => (int) $this->getConsumerWorkerOption($name, 'count'),
'startsecs' => $this->getConsumerWorkerOption($name, 'startsecs'),
'autorestart' => $this->transformBoolToString($this->getConsumerWorkerOption($name, 'autorestart')),
'stopsignal' => $this->getConsumerWorkerOption($name, 'stopsignal'),
'stopasgroup' => $this->transformBoolToString($this->getConsumerWorkerOption($name, 'stopasgroup')),
'stopwaitsecs' => $this->getConsumerWorkerOption($name, 'stopwaitsecs'),
'stdout_logfile' => $this->paths['worker_output_log_file'],
'stderr_logfile' => $this->paths['worker_error_log_file']
)
)
);
Expand Down Expand Up @@ -406,7 +409,8 @@ private function transformBoolToString($value)
*/
public function generateWorkerConfiguration($fileName, $vars)
{
$content = $this->templating->render('RabbitMqSupervisorBundle:Supervisor:program.conf.twig', $vars);
$configurationHelper = new ConfigurationHelper();
$content = $configurationHelper->getConfigurationStringFromDataArray($vars);
file_put_contents(
sprintf('%s%s.conf', $this->paths['worker_configuration_directory'], $fileName),
$content
Expand Down
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
"require": {
"symfony/framework-bundle": "~2.5|~3.0|~4.0",
"symfony/console": "~2.5|~3.0|~4.0",
"symfony/process": "~2.5|~3.0|~4.0",
"symfony/templating": "~2.5|~3.0|~4.0"
"symfony/process": "~2.5|~3.0|~4.0"
},
"suggest": {
"php-amqplib/rabbitmq-bundle": "The rabbitmq bundle for symfony which this bundle takes the consumer information from",
Expand Down

0 comments on commit 30f1b83

Please sign in to comment.