Skip to content

Instantly share code, notes, and snippets.

@el-sai
Last active October 15, 2018 02:05
Show Gist options
  • Save el-sai/113aaaf11894348bd6282808492be6c1 to your computer and use it in GitHub Desktop.
Save el-sai/113aaaf11894348bd6282808492be6c1 to your computer and use it in GitHub Desktop.
Sidekiq configuration(script) for running the Sidekiq Server on the Amazon Elastic Beanstalk instance alongside the application server(Puma). Both Application Server and Sidekiq Server run on the same instance using the same resources.
# I'm using this script for a 64-bit Linux Environment running Puma with Ruby 2.3 version.
# I have tested that the Sidekiq server stops and restarts by handling the USR1 and TERM signals while deploying an app, rebooting an instance and while restarting the app server.
# Below are the steps to include this script in the EB instance and start sidekiq server on deployment.
## Include this file inside the .ebextensions folder in the application directory.
## Set up the sidekiq.yml(sidekiq concurrency, queues and other settings) file inside the config folder in the application directory.
## Make sure sidekiq pid file path and sidekiq log file path SHOULD NOT be specified in the above sidekiq.yml file.(only when using this script on EB instance)
## Set the RAILS_ENV variable in the EB configuration.
## Deploy the application to the EB instance.
## ssh into the instance and search if sidekiq is running. ( ps aux | grep sidekiq )
## Once this script is added to the EB instance, add the .ebextensions/sidekiq.config to the .gitignore file as it is not required for every later deployments unless the instance changes.
## try restarting the app server or rebooting the instance. Now, check the sidekiq log file inside /var/app/containerfiles/logs folder which shows that sidekiq is stopped gracefully and restarted again.
# NOTE : Based on the auto scaling configuration of the EB instance, the instance might get terminated( when sidekiq is used heavily ) due to memory issues as both the sidekiq server and application server run on one instance. In this case the new instance started automatically by EB does not have the Sidekiq script files and hence sidekiq is not started.
# Below are the links which I took as reference for customizing this script for starting/restarting the sidekiq on EB instance.
# http://www.dannemanne.com/posts/post-deployment_script_on_elastic_beanstalk_restart_delayed_job
# http://www.mcbsys.com/blog/2015/03/manage-delayed_job-on-elastic-beanstalk/
# http://junkheap.net/blog/2013/05/20/elastic-beanstalk-post-deployment-scripts/
# https://gist.github.com/gcarrion-gfrmedia/11396682
commands:
create-post-dir:
command: "mkdir /opt/elasticbeanstalk/hooks/appdeploy/post"
ignoreErrors: true
files:
"/opt/elasticbeanstalk/hooks/appdeploy/post/50_restart_sidekiq.sh":
mode: "000755"
owner: root
group: root
content: |
#!/bin/bash
set -xe
# Loading environment data. Taken '11_bundle_install.sh' script as reference for loading environment data.
EB_SCRIPT_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k script_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_APP_CURRENT_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k app_deploy_dir)
EB_APP_PIDS_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k app_pid_dir)
# Setting up correct environment and ruby version so that bundle can load all gems
. $EB_SUPPORT_DIR/envvars
. $EB_SCRIPT_DIR/use-app-ruby.sh
PIDFILE=$EB_APP_PIDS_DIR/sidekiq.pid
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`
sleep 5
rm -rf $PIDFILE
fi
fi
cd $EB_APP_CURRENT_DIR
su -s /bin/bash -c "bundle exec sidekiq \
-e $RAILS_ENV \
-P /var/app/containerfiles/pids/sidekiq.pid \
-C /var/app/current/config/sidekiq.yml \
-L /var/app/containerfiles/logs/sidekiq.log \
-d" $EB_APP_USER
"/opt/elasticbeanstalk/hooks/appdeploy/pre/03_mute_sidekiq.sh":
mode: "000755"
owner: root
group: root
content: |
#!/bin/bash
# Loading environment data
EB_APP_PIDS_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k app_pid_dir)
PIDFILE=$EB_APP_PIDS_DIR/sidekiq.pid
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`
sleep 10
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment