From 30f1b83f6d63a3f7bf0cbb1edb6cd191f5ca492c Mon Sep 17 00:00:00 2001 From: Phobetor Date: Mon, 7 May 2018 16:14:15 +0200 Subject: [PATCH] replace templating usage by simple array-to-config helper method closes #41 --- Helpers/ConfigurationHelper.php | 24 +++++++++ Resources/config/services.yml | 1 - Resources/views/Supervisor/program.conf.twig | 11 ---- .../views/Supervisor/supervisord.conf.twig | 27 ---------- Services/RabbitMqSupervisor.php | 54 ++++++++++--------- composer.json | 3 +- 6 files changed, 54 insertions(+), 66 deletions(-) create mode 100644 Helpers/ConfigurationHelper.php delete mode 100644 Resources/views/Supervisor/program.conf.twig delete mode 100644 Resources/views/Supervisor/supervisord.conf.twig diff --git a/Helpers/ConfigurationHelper.php b/Helpers/ConfigurationHelper.php new file mode 100644 index 0000000..8d5130a --- /dev/null +++ b/Helpers/ConfigurationHelper.php @@ -0,0 +1,24 @@ + $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; + } +} diff --git a/Resources/config/services.yml b/Resources/config/services.yml index b6aff1b..d441408 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -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%" diff --git a/Resources/views/Supervisor/program.conf.twig b/Resources/views/Supervisor/program.conf.twig deleted file mode 100644 index 2096ed2..0000000 --- a/Resources/views/Supervisor/program.conf.twig +++ /dev/null @@ -1,11 +0,0 @@ -[program:{{ name }}] -command={% block cmd %}php {{ executablePath }} {{ command }} --env={{ app.environment }}{% endblock cmd %} - -process_name=%(program_name)s%(process_num)02d -numprocs={{ numprocs|default('1') }} -{% block options %} -{% for name, value in options %}{{ name }}={{ value }} -{% endfor %} -stdout_logfile={{ workerOutputLog }} -stderr_logfile={{ workerErrorLog }} -{% endblock options %} diff --git a/Resources/views/Supervisor/supervisord.conf.twig b/Resources/views/Supervisor/supervisord.conf.twig deleted file mode 100644 index 154609e..0000000 --- a/Resources/views/Supervisor/supervisord.conf.twig +++ /dev/null @@ -1,27 +0,0 @@ -; supervisor config file - -[unix_http_server] -file={{ sockFile }} ; (the path to the socket file) -chmod=0700 ; sockef file mode (default 0700) - -[supervisord] -logfile={{ logFile }} ; (main log file;default $CWD/supervisord.log) -pidfile={{ pidFile }} ; (supervisord pidfile;default supervisord.pid) - -; the below section must remain in the config file for RPC -; (supervisorctl/web interface) to work, additional interfaces may be -; added by defining them in separate rpcinterface: sections -[rpcinterface:supervisor] -supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface - -[supervisorctl] -serverurl=unix://{{ sockFile }} ; use a unix:// URL for a unix socket - -; The [include] section can just contain the "files" setting. This -; setting can list multiple files (separated by whitespace or -; newlines). It can also contain wildcards. The filenames are -; interpreted as relative to this file. Included files *cannot* -; include files themselves. - -[include] -files = {{ workerConfigurationDirectory }}*.conf diff --git a/Services/RabbitMqSupervisor.php b/Services/RabbitMqSupervisor.php index ac0fe4a..336002b 100644 --- a/Services/RabbitMqSupervisor.php +++ b/Services/RabbitMqSupervisor.php @@ -2,7 +2,7 @@ namespace Phobetor\RabbitMqSupervisorBundle\Services; -use Symfony\Component\Templating\EngineInterface; +use Phobetor\RabbitMqSupervisorBundle\Helpers\ConfigurationHelper; /** * @license MIT @@ -14,11 +14,6 @@ class RabbitMqSupervisor */ private $supervisor; - /** - * @var \Symfony\Component\Templating\EngineInterface - */ - private $templating; - /** * @var array */ @@ -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; @@ -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 @@ -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'] ) ) ); @@ -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 diff --git a/composer.json b/composer.json index e4e8e24..a2123ac 100644 --- a/composer.json +++ b/composer.json @@ -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",