Skip to content

Instantly share code, notes, and snippets.

@kpheasey
Forked from gcarrion-gfrmedia/0000_packages.config
Last active September 16, 2019 21:56
Show Gist options
  • Save kpheasey/d010608c2971408bf02c to your computer and use it in GitHub Desktop.
Save kpheasey/d010608c2971408bf02c to your computer and use it in GitHub Desktop.
AWS Elastic Beanstalk Ruby 2.2 Puma Sidekiq
packages:
yum:
git: []
htop: []
# Modified system bundle script to run 'bundle package'
files:
"/opt/elasticbeanstalk/hooks/appdeploy/pre/10_bundle_install.sh":
content: |
#!/usr/bin/env bash
. /opt/elasticbeanstalk/hooks/common.sh
set -xe
EB_SCRIPT_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k script_dir)
EB_APP_STAGING_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k app_staging_dir)
EB_APP_USER=$(/opt/elasticbeanstalk/bin/get-config container -k app_user)
EB_SUPPORT_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k support_dir)
. $EB_SUPPORT_DIR/envvars
. $EB_SCRIPT_DIR/use-app-ruby.sh
cd $EB_APP_STAGING_DIR
if [ -f Gemfile ]; then
echo "running 'bundle install' with Gemfile:"
cat Gemfile
if [ -d $EB_CONFIG_APP_ONDECK/vendor/cache ]; then
bundle install --local
# Incase there is a gem that is missing from the cache
bundle pack --all
bundle install
else
bundle pack --all
bundle install
fi
else
echo "no Gemfile found! Skipping bundle install stage!"
fi
if [ -f Gemfile.lock ]; then
echo "encountered a Gemfile.lock, setting proper permissions"
chown $EB_APP_USER:$EB_APP_USER Gemfile.lock
else
echo "no Gemfile.lock file found, so no permissions to set on it"
fi
if [ "$BUNDLE_DISABLE_SHARED_GEMS" = "1" ]; then
cd $EB_APP_STAGING_DIR;
chown -R $EB_APP_USER:$EB_APP_USER ./$BUNDLE_PATH;
echo "Modified the owner of '$BUNDLE_PATH' files";
fi
mode: "000755"
# Sidekiq interaction and startup script
files:
"/opt/elasticbeanstalk/hooks/appdeploy/post/50_restart_sidekiq.sh":
mode: "000755"
content: |
#!/bin/bash
. /opt/elasticbeanstalk/hooks/common.sh
. /opt/elasticbeanstalk/support/envvars
set -xe
EB_SCRIPT_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k script_dir)
EB_CONFIG_APP_CURRENT=$(/opt/elasticbeanstalk/bin/get-config container -k app_deploy_dir)
EB_CONFIG_APP_LOGS=$(/opt/elasticbeanstalk/bin/get-config container -k app_log_dir)
EB_CONFIG_APP_PIDS=$(/opt/elasticbeanstalk/bin/get-config container -k app_pid_dir)
. $EB_SCRIPT_DIR/use-app-ruby.sh
BUNDLE=`which bundle`
SIDEKIQ=`which sidekiq`
cd $EB_CONFIG_APP_CURRENT
# for 2 instances of sidekiq, this can be increased are decreased, just make sure it is updated in the both areas
for i in `seq 1 2`
do
PIDFILE=$EB_CONFIG_APP_PIDS/sidekiq-$i.pid
# stop any running instance
if [ -f $PIDFILE ]
then
SIDEKIQ_LIVES=$(/bin/ps -o pid= -p `cat $PIDFILE`)
if [ -z $SIDEKIQ_LIVES ]
then
rm -rf $PIDFILE
else
kill -TERM `cat $PIDFILE`
fi
fi
# start a new instance
$BUNDLE exec $SIDEKIQ \
-e production \
-P $PIDFILE.pid \
-C $EB_CONFIG_APP_CURRENT/config/sidekiq.yml \
-L $EB_CONFIG_APP_LOGS/sidekiq.log \
-d
done
"/opt/elasticbeanstalk/hooks/appdeploy/pre/03_quiet_sidekiq.sh":
mode: "000755"
content: |
#!/bin/bash
. /opt/elasticbeanstalk/support/envvars
EB_CONFIG_APP_PIDS=$(/opt/elasticbeanstalk/bin/get-config container -k app_pid_dir)
# for 2 instances of sidekiq, this can be increased are decreased, just make sure it is updated in the both areas
for i in `seq 1 2`
do
PIDFILE=$EB_CONFIG_APP_PIDS/sidekiq-$i.pid
# quiet any running instance
if [ -f $PIDFILE ]
then
SIDEKIQ_LIVES=$(/bin/ps -o pid= -p `cat $PIDFILE`)
if [ -z $SIDEKIQ_LIVES ]
then
rm -rf $PIDFILE
else
kill -USR1 `cat $PIDFILE`
fi
fi
done
@topray
Copy link

topray commented Jul 2, 2015

It works! Thanks 😄 but '.pid' should be deleted on the line 45, '0010_sidekiq.config'

@JeremiahChurch
Copy link

I had to make two changes to 0010_sidekiq.config on top of topray's recommendation

add '. /opt/elasticbeanstalk/hooks/common.sh' at line 55
remove 'e' from line 11 so it was just 'set -x'

without those changes every time I deployed on the same EB instance I'd get new instances of sidekiq and the old ones would not go away - do that a few times on a t2.micro and you're out of memory :(

@hamadmj
Copy link

hamadmj commented Nov 25, 2015

Took me several hours to discover this fix. Thanks a lot for sharing!

@ebosantos
Copy link

It didn't work to me. My use case is to send emails. No email is sent and no error is shown and logged. Do you have some ideas on how to fix and even inspect it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment