Skip to content

Instantly share code, notes, and snippets.

@tomchapin
Last active June 13, 2020 02:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomchapin/b2b497cb07efa05286079b1883c5e5f5 to your computer and use it in GitHub Desktop.
Save tomchapin/b2b497cb07efa05286079b1883c5e5f5 to your computer and use it in GitHub Desktop.
Elastic Beanstalk config for running sidekiq workers in the background. Place in your .ebextensions folder and configure sidekiq via environment variables.
##############################################################################
# Example environment variables for your elastic beanstalk app config:
##############################################################################
# SIDEKIQ_ENABLED=true
# SIDEKIQ_WORKER_COUNT=2
# SIDEKIQ_1_EXTRA_OPTIONS="-c 10 --queue default --queue critical"
# SIDEKIQ_2_EXTRA_OPTIONS="-c 10 --queue critical"
##############################################################################
# Sidekiq interaction and startup script
commands:
create_post_dir:
command: "mkdir -p /opt/elasticbeanstalk/hooks/appdeploy/post"
ignoreErrors: true
files:
"/etc/init/sidekiq.conf":
owner: root
group: root
mode: "000644"
content: |
description "Sidekiq Background Worker"
respawn
respawn limit unlimited
post-stop exec sleep 5
# Uncomment the following line if you don't want the sidekiq process to respawn when it exits normally
# normal exit 0 TERM
kill timeout 15
instance ${app}-${worker_number}
script
exec /bin/bash <<'EOT'
. /opt/elasticbeanstalk/support/envvars
EB_APP_DEPLOY_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k app_deploy_dir)
EB_APP_PID_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k app_pid_dir)
EB_APP_USER=$(/opt/elasticbeanstalk/bin/get-config container -k app_user)
EB_SCRIPT_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k script_dir)
EB_SUPPORT_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k support_dir)
. $EB_SUPPORT_DIR/envvars
. $EB_SCRIPT_DIR/use-app-ruby.sh
# Load the environment variables
. /opt/elasticbeanstalk/support/envvars
. /opt/elasticbeanstalk/support/envvars.d/sysenv
SIDEKIQ_CONFIG=$EB_APP_DEPLOY_DIR/config/sidekiq.yml
SIDEKIQ_LOG=$EB_APP_DEPLOY_DIR/log/sidekiq.log
if [ "$SIDEKIQ_ENABLED" == "true" ]; then
cd $EB_APP_DEPLOY_DIR
logger -t sidekiq "Starting process: $app-$worker_number"
exec su -s /bin/bash -c "bundle exec sidekiq -e $RACK_ENV -P $SIDEKIQ_PID -C $SIDEKIQ_CONFIG -L $SIDEKIQ_LOG -i ${worker_number} ${sidekiq_options}" $EB_APP_USER
else
logger -t sidekiq "The SIDEKIQ_ENABLED environment variable is not set to true!"
fi
EOT
end script
"/opt/elasticbeanstalk/hooks/appdeploy/post/50_restart_sidekiq.sh":
mode: "000755"
owner: root
group: root
content: |
#!/usr/bin/env bash
. /opt/elasticbeanstalk/support/envvars
EB_APP_DEPLOY_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k app_deploy_dir)
EB_APP_PID_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k app_pid_dir)
EB_APP_USER=$(/opt/elasticbeanstalk/bin/get-config container -k app_user)
EB_SCRIPT_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k script_dir)
EB_SUPPORT_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k support_dir)
. $EB_SUPPORT_DIR/envvars
. $EB_SCRIPT_DIR/use-app-ruby.sh
# Load the environment variables
. /opt/elasticbeanstalk/support/envvars
. /opt/elasticbeanstalk/support/envvars.d/sysenv
SIDEKIQ_CONFIG=$EB_APP_DEPLOY_DIR/config/sidekiq.yml
SIDEKIQ_LOG=$EB_APP_DEPLOY_DIR/log/sidekiq.log
# Ensure that upstart has the most recent config for sidekiq loaded
sudo initctl reload-configuration
if [ "$SIDEKIQ_ENABLED" == "true" ]; then
cd $EB_APP_DEPLOY_DIR
echo "Stopping any running sidekiq workers..."
for ((i=1; i<=20; i++)); do
echo "Stopping existing Sidekiq process (${i})..."
sudo stop sidekiq app=my_rails_app worker_number=${i} || true
done
sleep 15
sudo pgrep -f 'sidekiq (.*)' | xargs sudo kill -USR1 || true
if [ ! -z "$SIDEKIQ_WORKER_COUNT" ]; then
for ((i=1; i<=$((SIDEKIQ_WORKER_COUNT)); i++)); do
EXTRA_OPTIONS_VAR_NAME="SIDEKIQ_${i}_EXTRA_OPTIONS"
if [[ ! -z ${!EXTRA_OPTIONS_VAR_NAME} ]]; then
echo "Executing command: sudo start sidekiq app=my_rails_app worker_number=${i} sidekiq_options=\"${!EXTRA_OPTIONS_VAR_NAME}\""
sudo start sidekiq app=my_rails_app worker_number=${i} sidekiq_options="${!EXTRA_OPTIONS_VAR_NAME}"
else
echo "The ${EXTRA_OPTIONS_VAR_NAME} environment variable appears to be missing!"
fi
done
else
echo "The SIDEKIQ_WORKER_COUNT environment variable is not set!"
fi
fi
"/opt/elasticbeanstalk/hooks/configdeploy/post/50_restart_sidekiq.sh":
mode: "000755"
owner: root
group: root
content: |
#!/usr/bin/env bash
/opt/elasticbeanstalk/hooks/appdeploy/post/50_restart_sidekiq.sh
"/opt/elasticbeanstalk/hooks/restartappserver/post/50_restart_sidekiq.sh":
mode: "000755"
owner: root
group: root
content: |
#!/usr/bin/env bash
/opt/elasticbeanstalk/hooks/appdeploy/post/50_restart_sidekiq.sh
"/opt/elasticbeanstalk/hooks/appdeploy/pre/03_mute_sidekiq.sh":
mode: "000755"
owner: root
group: root
content: |
#!/usr/bin/env bash
echo "Stopping any running sidekiq workers..."
for ((i=1; i<=20; i++)); do
echo "Stopping existing Sidekiq process (${i})..."
sudo stop sidekiq app=my_rails_app worker_number=${i} || true
done
sleep 15
sudo pgrep -f 'sidekiq (.*)' | xargs sudo kill -USR1 || true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment