diff --git a/README.md b/README.md index 8400647..c9cccac 100644 --- a/README.md +++ b/README.md @@ -62,17 +62,17 @@ IS_WORKER = true ### Set Queue Driver -Set the [driver](https://laravel.com/docs/5.1/queues#introduction) in your your EB envronmental variables: +Set the [driver](https://laravel.com/docs/5.1/queues#introduction) in your your EB environmental variables: ``` QUEUE_DRIVER = [driver] ``` -**Note: If no `QUEUE_DRIVER` key is present in your EB envronmental variables then `beanstalkd` will be used.** +**Note: If no `QUEUE_DRIVER` key is present in your EB environmental variables then `beanstalkd` will be used.** ### Add Queues -All queues are configured using EB envronmental variables with the following syntax: +All queues are configured using EB environmental variables with the following syntax: **Note**: brackets are placeholders only, do not use them in your actual configuration @@ -131,17 +131,19 @@ Supervisor requires port 9001 to be open if you want to access its web monitor. `parseConfig.php` looks for either a user-supplied `supervisord.conf` file specified in configuration. If one exists then it is used. -Otherwise `parseConfig.php` looks for a json file generated earlier that contains all of the environmental variables configured for elastic beanstalk. It then parses out any queue configurations found (see `Add Queues`) section above and generates a supervisor program for each. The program to be generated looks like this: +Otherwise `parseConfig.php` looks for a json file generated earlier that contains all of the environmental variables configured for elastic beanstalk. It then parses out any queue configurations found (see `Add Queues`) section above and generates a supervisor program for each as well as supplying each program with all the environmental variables set for EB. The program to be generated looks like this: ``` [program:$queue] -command=sudo php artisan queue:work $connection --queue=$queue --tries=$tries --sleep=$sleep --daemon +command=php artisan queue:work $connection --queue=$queue --tries=$tries --sleep=$sleep --daemon directory=/var/app/current/ autostart=true autorestart=true process_name=$queue-%(process_num)s numprocs=$numProcs startsecs=$startSecs +user=webapp +environment=$environmentVal ``` After parsing all queues it then appends the programs to a clean `supervisord.conf` file in the same directory. @@ -169,7 +171,6 @@ Now a bash script `workerDeploy.sh` checks for `IS_WORKER=TRUE` in the EB enviro This is almost verbatim how I have things setup for another project so some usage is limited because of how it was originally written: * Queue driver defaults to beanstalkd if not explicitly set -* There is no way to generate a supervisor program without `--queue=[queue]` right now All of these are simple fixes though! Check out issues to see these and more and if you need them please make a PR! diff --git a/src/.ebextensions/parseConfig.php b/src/.ebextensions/parseConfig.php index ca9648d..bc04b7f 100755 --- a/src/.ebextensions/parseConfig.php +++ b/src/.ebextensions/parseConfig.php @@ -1,17 +1,20 @@ $val) { + if(ctype_alnum($val) // alphanumeric doesn't need quotes + || (strpos($val, '"') === 0 && strrpos($val, '"') === count($val) -1)) // if the value is already quoted don't double-quote it + { + $formattedVal = $val; + } else { // otherwise put everything in quotes for environment param http://supervisord.org/configuration.html#program-x-section-values + $formattedVal = "\"{$val}\""; + } + $envKvArray[] = "{$key}={$formattedVal}"; + } + $envKv = implode(',', $envKvArray); $programs = ''; - foreach ($envVars as $key => $val) + $isBeanstalk = $lowerEnvVars['queue_driver'] === 'beanstalkd'; + + foreach ($lowerEnvVars as $key => $val) { if (strpos($key, 'queue') !== false && strpos($key, 'queue_driver') === false) { @@ -117,13 +135,19 @@ function getEBWorkerConfig($path) $startSecsKey = substr($key, 5) . 'startsecs'; //get queue $key + number of seconds the process should stay up $delayKey = substr($key, 5) . 'delay'; //get queue $key + delay in seconds before a job should re-enter the ready queue - $tries = isset($envVars[ $tryKey ]) ? $envVars[ $tryKey ] : 5; - $sleep = isset($envVars[ $sleepKey ]) ? $envVars[ $sleepKey ] : 5; - $numProcs = isset($envVars[ $numProcKey ]) ? $envVars[ $numProcKey ] : 1; - $startSecs = isset($envVars[ $startSecsKey ]) ? $envVars[ $startSecsKey ] : 1; - $delay = isset($envVars[ $delayKey]) ? $envVars[ $delayKey ] : 0; - $connection = isset($envVars['queue_driver']) ? $envVars['queue_driver'] : 'beanstalkd'; - $programs .= generateProgram($connection, $val, $tries, $sleep, $numProcs, $delay, $startSecs); + $tries = isset($lowerEnvVars[ $tryKey ]) ? $lowerEnvVars[ $tryKey ] : 5; + $sleep = isset($lowerEnvVars[ $sleepKey ]) ? $lowerEnvVars[ $sleepKey ] : 5; + $numProcs = isset($lowerEnvVars[ $numProcKey ]) ? $lowerEnvVars[ $numProcKey ] : 1; + $startSecs = isset($lowerEnvVars[ $startSecsKey ]) ? $lowerEnvVars[ $startSecsKey ] : 1; + $delay = isset($lowerEnvVars[ $delayKey ]) ? $lowerEnvVars[ $delayKey ] : 0; + // if using beanstalk connection should always be beanstalkd and specify tube in queue, otherwise us queue name as connection + $connection = $isBeanstalk ? 'beanstalkd' : $val; + // if not using beanstalk we don't need queue probably + $queue = $isBeanstalk ? $val : null; + $programEnvArray = $envKvArray; + // if any vars need to be specific per worker this is where to put them + // $programEnvArray[] = + $programs .= generateProgram($connection, $queue, $tries, $sleep, $numProcs, $delay, $startSecs, implode(',', $programEnvArray)); } } } diff --git a/src/.ebextensions/workerDeploy.sh b/src/.ebextensions/workerDeploy.sh index ce5898c..81c6f07 100755 --- a/src/.ebextensions/workerDeploy.sh +++ b/src/.ebextensions/workerDeploy.sh @@ -8,6 +8,8 @@ updateSupervisor(){ cp .ebextensions/supervisord.conf /etc/supervisord.conf sudo service supervisord stop php /var/app/current/artisan queue:restart # If this worker is running in daemon mode (most likely) we need to restart it with the new build + echo "Sleeping a few seconds to make sure supervisor shuts down..." # https://github.com/Supervisor/supervisor/issues/48#issuecomment-2684400 + sleep 5 sudo service supervisord start } @@ -42,8 +44,6 @@ if test "${ary['IS_WORKER']+isset}" #if key exists if [ ${ary['IS_WORKER']} == "'true'" ] #if the value is true then echo "Found worker key!" - echo "Copying environmental variables to dotenv" - cp bashEnv .env echo "Starting worker deploy process..."; if [ -f /etc/init.d/supervisord ];