Skip to content

Instantly share code, notes, and snippets.

@TimothyRHuertas
Last active August 29, 2015 14:16
Show Gist options
  • Save TimothyRHuertas/405b77604df481f1c2e6 to your computer and use it in GitHub Desktop.
Save TimothyRHuertas/405b77604df481f1c2e6 to your computer and use it in GitHub Desktop.
Elastic BeanstalkRuby 2.1 (Puma) with Resque
commands:
create_post_dir:
command: "mkdir /opt/elasticbeanstalk/hooks/appdeploy/post"
ignoreErrors: true
create_pid_dir:
command: "mkdir /var/app/current/tmp/pids"
ignoreErrors: true
files:
"/etc/init/resque.conf":
mode: "000755"
content: |
description "Starts a Resque worker. For example: start resque."
start on runlevel [2345]
stop on runlevel [!2345]
respawn
respawn limit 5 20
script
exec /bin/bash <<"EOF"
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
EB_APP_USER=$(/opt/elasticbeanstalk/bin/get-config container -k app_user)
EB_APP_DEPLOY_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k app_deploy_dir)
LOGFILE=$EB_APP_DEPLOY_DIR/log/resque_workers.log
cd $EB_APP_DEPLOY_DIR
exec su -s /bin/bash -c "bundle exec rake workers:start >> $LOGFILE 2>&1" webapp
EOF
end script
post-stop script
exec /bin/bash <<"EOF"
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
EB_APP_USER=$(/opt/elasticbeanstalk/bin/get-config container -k app_user)
EB_APP_DEPLOY_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k app_deploy_dir)
LOGFILE=$EB_APP_DEPLOY_DIR/log/resque_workers.log
cd $EB_APP_DEPLOY_DIR
exec su -s /bin/bash -c "bundle exec rake workers:killall >> $LOGFILE 2>&1" webapp
EOF
end script
"/opt/elasticbeanstalk/hooks/appdeploy/post/999_start_resque_job.sh":
mode: "000755"
content: |
#!/usr/bin/env bash
. /opt/elasticbeanstalk/support/envvars
set -ex
initctl restart resque || initctl start resque
echo 'resque restarted'

There are several articles on using elastic beanstalk hooks to create and manage (start|stop|restart) background services (this example uses up-start) that govern rake tasks. This is not one of them; rather it is an amendment to those posts intended to provide you with an updated example. I recently upgraded my Elastic Beanstalk application to use Ruby 2.1 (Puma) and my services stopped working. I made some minor changes to get stuff working again.

Disclaimer: There are better ways to accomplish this (see rails sidekiq), but you may still find this useful. Consider it the "copy/paste path of least resistance" for upgrading older projects.

Put the file in root/.ebextensions/

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