Skip to content

Commit 18defab

Browse files
committed
Add ability to use user-supplied supervisord.conf
User may specify a file location using elasticbeanstalkworker.php to use instead of generating one from environmental variables. Closes #4
1 parent 9660361 commit 18defab

File tree

1 file changed

+108
-31
lines changed

1 file changed

+108
-31
lines changed

src/.ebextensions/parseConfig.php

Lines changed: 108 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<?php
22

3-
function generateProgram($connection, $queue, $tries, $sleep, $numProcs, $startSecs){
4-
$program = <<<EOT
3+
function generateProgram($connection, $queue, $tries, $sleep, $numProcs, $startSecs)
4+
{
5+
$program = <<<EOT
56
67
[program:$queue]
78
command=sudo php artisan doctrine:queue:work $connection --queue=$queue --tries=$tries --sleep=$sleep --daemon
@@ -13,42 +14,118 @@ function generateProgram($connection, $queue, $tries, $sleep, $numProcs, $startS
1314
startsecs=$startSecs
1415
1516
EOT;
16-
return $program;
17+
18+
return $program;
19+
}
20+
21+
function getEBWorkerConfig($path)
22+
{
23+
if (null === $path)
24+
{
25+
return null;
26+
}
27+
else
28+
{
29+
$filePath = $path . '/elasticbeanstalkworker.php';
30+
echo 'File path for worker config: ' . $filePath . PHP_EOL;
31+
if (is_file($filePath) && is_readable($filePath))
32+
{
33+
return include($filePath);
34+
}
35+
echo 'Worker config is not a file or is not readable. Skipping.' . PHP_EOL;
36+
37+
return null;
38+
}
1739
}
1840

19-
//relative dir is not working for some reason, need to test them all!
20-
$envLocation = '';
21-
if(file_exists('/var/app/ondeck/jsonEnv')){
22-
$envLocation = '/var/app/ondeck/jsonEnv';
23-
} else if(file_exists('/var/app/current/jsonEnv')){
24-
$envLocation = '/var/app/current/jsonEnv';
41+
echo 'Starting supervisor configuration parsing.' . PHP_EOL;
42+
43+
// determine what directory we are in
44+
$deployDirectory = null;
45+
if (is_dir('/var/app/ondeck'))
46+
{
47+
$deployDirectory = '/var/app/ondeck';
48+
}
49+
else if (is_dir('/var/app/current'))
50+
{
51+
$deployDirectory = '/var/app/current';
2552
}
26-
$vars = json_decode(file_get_contents($envLocation), true);
27-
$envVars = array_change_key_case($vars); // convert keys to lower case so environmental variables don't have to be case-sensitive
2853

29-
$programs = '';
54+
// set location of our clean supervisor config
55+
$superLocation = $deployDirectory . '/.ebextensions/supervisord.conf';
56+
57+
// determine config directory
58+
$configDirectory = null;
59+
if (is_dir($deployDirectory . '/config'))
60+
{
61+
$configDirectory = $deployDirectory . '/config';
62+
}
63+
else
64+
{
65+
echo 'Could not find project configuration directory, oh well.' . PHP_EOL;
66+
}
3067

31-
foreach($envVars as $key => $val){
32-
if(strpos($key, 'queue') !== false && strpos($key, 'queue_driver') === false){
33-
$tryKey = substr($key, 10) . 'tries'; //get queue $key + tries to see if custom tries is set
34-
$sleepKey = substr($key, 5) . 'sleep'; //get queue $key + sleep to see if custom sleep is set
35-
$numProcKey = substr($key, 5) . 'numprocs'; //get queue $key + num process to see if custom number of processes is set
36-
$startSecsKey = substr($key, 5) . 'startsecs'; //get queue $key + number of seconds the process should stay up
68+
// determine user supervisord.conf location
69+
$relativeConfigFilePath = null;
70+
if (null !== $workerConfig = getEBWorkerConfig($configDirectory))
71+
{
72+
$relativeConfigFilePath = $workerConfig['supervisorConfigPath'];
73+
}
3774

38-
$tries = isset($envVars[$tryKey]) ? $envVars[$tryKey] : 5;
39-
$sleep = isset($envVars[$sleepKey]) ? $envVars[$sleepKey] : 5;
40-
$numProcs = isset($envVars[$numProcKey]) ? $envVars[$numProcKey] : 1;
41-
$startSecs = isset($envVars[$startSecsKey]) ? $envVars[$startSecsKey] : 1;
42-
$connection = isset($envVars['queue_driver']) ? $envVars['queue_driver'] : 'beanstalkd';
43-
$programs .= generateProgram($connection, $val, $tries, $sleep, $numProcs, $startSecs);
44-
}
75+
if (null !== $relativeConfigFilePath)
76+
{
77+
$absoluteConfigFilePath = $deployDirectory . '/' . $relativeConfigFilePath;
78+
echo 'User-supplied supervisor config path: ' . $absoluteConfigFilePath . PHP_EOL;
79+
$programs = file_get_contents($relativeConfigFilePath);
80+
if (false === $programs)
81+
{
82+
echo 'Tried to parse user-supplied supervisord.conf but it failed!' . PHP_EOL;
83+
exit(1);
84+
}
85+
else
86+
{
87+
echo 'Found user supervisor.conf file! Using it instead of generating from environmental variables.' . PHP_EOL;
88+
}
89+
$writeType = 0;
4590
}
46-
$superLocation = '';
47-
if(file_exists('/var/app/ondeck/.ebextensions/supervisord.conf')){
48-
$superLocation = '/var/app/ondeck/.ebextensions/supervisord.conf';
49-
} else if(file_exists('/var/app/current/.ebextensions/supervisord.conf')){
50-
$superLocation = '/var/app/current/.ebextensions/supervisord.conf';
91+
else
92+
{
93+
$writeType = FILE_APPEND;
94+
95+
if (null !== $relativeConfigFilePath)
96+
{
97+
echo 'Found path for user-supplied supervisord.conf but it was not a valid file. Continuing with parsing from environmental variables.' . PHP_EOL;
98+
}
99+
else
100+
{
101+
echo 'No user-supplied supervisord.conf found. Generating one from environmental variables.' . PHP_EOL;
102+
}
103+
104+
$envLocation = $deployDirectory . '/jsonEnv';
105+
$vars = json_decode(file_get_contents($envLocation), true);
106+
$envVars = array_change_key_case($vars); // convert keys to lower case so environmental variables don't have to be case-sensitive
107+
108+
$programs = '';
109+
110+
foreach ($envVars as $key => $val)
111+
{
112+
if (strpos($key, 'queue') !== false && strpos($key, 'queue_driver') === false)
113+
{
114+
$tryKey = substr($key, 10) . 'tries'; //get queue $key + tries to see if custom tries is set
115+
$sleepKey = substr($key, 5) . 'sleep'; //get queue $key + sleep to see if custom sleep is set
116+
$numProcKey = substr($key, 5) . 'numprocs'; //get queue $key + num process to see if custom number of processes is set
117+
$startSecsKey = substr($key, 5) . 'startsecs'; //get queue $key + number of seconds the process should stay up
118+
119+
$tries = isset($envVars[ $tryKey ]) ? $envVars[ $tryKey ] : 5;
120+
$sleep = isset($envVars[ $sleepKey ]) ? $envVars[ $sleepKey ] : 5;
121+
$numProcs = isset($envVars[ $numProcKey ]) ? $envVars[ $numProcKey ] : 1;
122+
$startSecs = isset($envVars[ $startSecsKey ]) ? $envVars[ $startSecsKey ] : 1;
123+
$connection = isset($envVars['queue_driver']) ? $envVars['queue_driver'] : 'beanstalkd';
124+
$programs .= generateProgram($connection, $val, $tries, $sleep, $numProcs, $startSecs);
125+
}
126+
}
51127
}
52128

53-
file_put_contents($superLocation, $programs.PHP_EOL, FILE_APPEND);
129+
130+
file_put_contents($superLocation, $programs . PHP_EOL, $writeType);
54131

0 commit comments

Comments
 (0)