Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a config option to configure an optional inet_http_server #64

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 31 additions & 8 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public function getConfigTreeBuilder()
->scalarNode('sock_file_permissions')->defaultValue('0700')->end()
->end();
$this->addPaths($rootNode);
$this->addInetHttpServer($rootNode);
$this->addCommands($rootNode);
$this->addConsumer($rootNode);

Expand All @@ -51,21 +52,43 @@ protected function addPaths(ArrayNodeDefinition $node)
->arrayNode('paths')
->addDefaultsIfNotSet()
->children()
->scalarNode('workspace_directory')->defaultValue('%kernel.root_dir%/supervisor/%kernel.environment%/')->end()
->scalarNode('configuration_file')->defaultValue('%kernel.root_dir%/supervisor/%kernel.environment%/supervisord.conf')->end()
->scalarNode('pid_file')->defaultValue('%kernel.root_dir%/supervisor/%kernel.environment%/supervisor.pid')->end()
->scalarNode('sock_file')->defaultValue('%kernel.root_dir%/supervisor/%kernel.environment%/supervisor.sock')->end()
->scalarNode('log_file')->defaultValue('%kernel.root_dir%/supervisor/%kernel.environment%/supervisord.log')->end()
->scalarNode('worker_configuration_directory')->defaultValue('%kernel.root_dir%/supervisor/%kernel.environment%/worker/')->end()
->scalarNode('worker_output_log_file')->defaultValue('%kernel.root_dir%/supervisor/%kernel.environment%/logs/stdout.log')->end()
->scalarNode('worker_error_log_file')->defaultValue('%kernel.root_dir%/supervisor/%kernel.environment%/logs/stderr.log')->end()
->scalarNode('workspace_directory')->defaultValue('%phobetor_rabbitmq_supervisor.project_dir%/supervisor/%kernel.environment%/')->end()
->scalarNode('configuration_file')->defaultValue('%phobetor_rabbitmq_supervisor.project_dir%/supervisor/%kernel.environment%/supervisord.conf')->end()
->scalarNode('pid_file')->defaultValue('%phobetor_rabbitmq_supervisor.project_dir%/supervisor/%kernel.environment%/supervisor.pid')->end()
->scalarNode('sock_file')->defaultValue('%phobetor_rabbitmq_supervisor.project_dir%/supervisor/%kernel.environment%/supervisor.sock')->end()
->scalarNode('log_file')->defaultValue('%phobetor_rabbitmq_supervisor.project_dir%/supervisor/%kernel.environment%/supervisord.log')->end()
->scalarNode('worker_configuration_directory')->defaultValue('%phobetor_rabbitmq_supervisor.project_dir%/supervisor/%kernel.environment%/worker/')->end()
->scalarNode('worker_output_log_file')->defaultValue('%phobetor_rabbitmq_supervisor.project_dir%/supervisor/%kernel.environment%/logs/stdout.log')->end()
->scalarNode('worker_error_log_file')->defaultValue('%phobetor_rabbitmq_supervisor.project_dir%/supervisor/%kernel.environment%/logs/stderr.log')->end()
->scalarNode('php_executable')->defaultValue('php')->end()
->end()
->end()
->end()
;
}

/**
* Add inet_http_server configuration
*
* @param ArrayNodeDefinition $node
*/
protected function addInetHttpServer(ArrayNodeDefinition $node)
{
$node
->children()
->arrayNode('inet_http_server')
->addDefaultsIfNotSet()
->children()
->booleanNode('enabled')->defaultFalse()->end()
->scalarNode('port')->defaultNull()->end()
->scalarNode('username')->defaultNull()->end()
->scalarNode('password')->defaultNull()->end()
->end()
->end()
->end()
;
}

/**
* Add commands configuration
*
Expand Down
7 changes: 7 additions & 0 deletions DependencyInjection/RabbitMqSupervisorExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ class RabbitMqSupervisorExtension extends Extension implements PrependExtensionI
*/
public function load(array $configs, ContainerBuilder $container)
{
if ($container->hasParameter('kernel.root_dir')) {
$container->setParameter('phobetor_rabbitmq_supervisor.project_dir', '%kernel.root_dir%/..');
} else {
$container->setParameter('phobetor_rabbitmq_supervisor.project_dir', '%kernel.project_dir%');
}

$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);

Expand Down Expand Up @@ -59,6 +65,7 @@ public function load(array $configs, ContainerBuilder $container)
$container->setParameter('phobetor_rabbitmq_supervisor.supervisor_instance_identifier', $config['supervisor_instance_identifier']);
$container->setParameter('phobetor_rabbitmq_supervisor.sock_file_permissions', $config['sock_file_permissions']);
$container->setParameter('phobetor_rabbitmq_supervisor.paths', $config['paths']);
$container->setParameter('phobetor_rabbitmq_supervisor.inet_http_server', $config['inet_http_server']);
$container->setParameter('phobetor_rabbitmq_supervisor.workspace', $config['paths']['workspace_directory']);
$container->setParameter('phobetor_rabbitmq_supervisor.configuration_file', $config['paths']['configuration_file']);
$container->setParameter('phobetor_rabbitmq_supervisor.commands', $config['commands']);
Expand Down
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,8 @@ supervisor/
├── queue3.conf
└── queue4.conf
```
In symfony 2 and 3 this will be placed inside your `app/` directory.

Caution with symfony 4: to not have this inside of your `src/` directory you need to set the paths to suit your needs.
E. g. to use the standard structure inside of the `var/` directory, use this:
If you want to use the standard structure inside of the `var/` directory, change the configuration like this:
```yml
rabbit_mq_supervisor:
paths:
Expand Down
3 changes: 2 additions & 1 deletion Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ services:
arguments:
- "@phobetor_rabbitmq_supervisor.supervisor_service"
- "%phobetor_rabbitmq_supervisor.paths%"
- "%phobetor_rabbitmq_supervisor.inet_http_server%"
- "%phobetor_rabbitmq_supervisor.commands%"
- "%phobetor_rabbitmq_supervisor.consumers%"
- "%phobetor_rabbitmq_supervisor.multiple_consumers%"
- "%phobetor_rabbitmq_supervisor.batch_consumers%"
- "%phobetor_rabbitmq_supervisor.rpc_servers%"
- "%phobetor_rabbitmq_supervisor.config%"
- "%phobetor_rabbitmq_supervisor.sock_file_permissions%"
- "%kernel.root_dir%"
- "%phobetor_rabbitmq_supervisor.project_dir%"
- "%kernel.environment%"

phobetor_rabbitmq_supervisor.supervisor_service:
Expand Down
33 changes: 24 additions & 9 deletions Services/RabbitMqSupervisor.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ class RabbitMqSupervisor
*/
private $paths;

/**
* @var array
*/
private $inet_http_server;

/**
* @var array
*/
Expand Down Expand Up @@ -52,7 +57,7 @@ class RabbitMqSupervisor
/**
* @var string
*/
private $rootDir;
private $projectDir;

/**
* @var string
Expand All @@ -69,28 +74,30 @@ class RabbitMqSupervisor
*
* @param \Phobetor\RabbitMqSupervisorBundle\Services\Supervisor $supervisor
* @param array $paths
* @param array $inet_http_server
* @param array $commands
* @param array $consumers
* @param array $multipleConsumers
* @param array $batchConsumers
* @param array $rpcServers
* @param array $config
* @param $sockFilePermissions
* @param string $kernelRootDir
* @param string $projectDir
* @param string $environment
*/
public function __construct(Supervisor $supervisor, array $paths, array $commands, $consumers, $multipleConsumers, $batchConsumers, $rpcServers, $config, $sockFilePermissions, $kernelRootDir, $environment)
public function __construct(Supervisor $supervisor, array $paths, array $inet_http_server, array $commands, $consumers, $multipleConsumers, $batchConsumers, $rpcServers, $config, $sockFilePermissions, $projectDir, $environment)
{
$this->supervisor = $supervisor;
$this->paths = $paths;
$this->inet_http_server = $inet_http_server;
$this->commands = $commands;
$this->consumers = $consumers;
$this->multipleConsumers = $multipleConsumers;
$this->batchConsumers = $batchConsumers;
$this->rpcServers = $rpcServers;
$this->config = $config;
$this->sockFilePermissions = $sockFilePermissions;
$this->rootDir = dirname($kernelRootDir);
$this->projectDir = $projectDir;
$this->environment = $environment;
}

Expand Down Expand Up @@ -289,8 +296,7 @@ private function createPathDirectories()

public function generateSupervisorConfiguration()
{
$configurationHelper = new ConfigurationHelper();
$content = $configurationHelper->getConfigurationStringFromDataArray(array(
$configuration = array(
'unix_http_server' => array(
'file' => $this->paths['sock_file'],
'chmod' => $this->sockFilePermissions
Expand All @@ -316,7 +322,16 @@ public function generateSupervisorConfiguration()
array_keys($this->rpcServers)
))
)
));
);

$inetHttpServer = $this->inet_http_server;
if ($inetHttpServer['enabled']) {
unset($inetHttpServer['enabled']);
$configuration['inet_http_server'] = $inetHttpServer;
}

$configurationHelper = new ConfigurationHelper();
$content = $configurationHelper->getConfigurationStringFromDataArray($configuration);
file_put_contents(
$this->createSupervisorConfigurationFilePath(),
$content
Expand All @@ -328,15 +343,15 @@ private function generateWorkerConfigurations($names, $baseCommand)
// try different possible console paths (realpath() will throw away the not existing ones)
$consolePaths = [];
foreach (['bin', 'app'] as $consoleDirectory) {
$consolePath = sprintf('%s/%s/console', $this->rootDir, $consoleDirectory);
$consolePath = sprintf('%s/%s/console', $this->projectDir, $consoleDirectory);
if (!empty(realpath($consolePath))) {
$consolePaths[] = $consolePath;
}
}

// fall back to standard console path if none of the paths was valid
if (empty($consolePaths)) {
$consolePaths[] = sprintf('%s/%s/console', $this->rootDir, 'bin');
$consolePaths[] = sprintf('%s/%s/console', $this->projectDir, 'bin');
}

$executablePath = $consolePaths[0];
Expand Down