Ben
Ben

Reputation: 362

Deploy Rails Application with delayed_job running as daemon task

I realise there are a few articles out there about this but i'm trying to deploy a Rails application on Elastic Beanstalk and start delayed_job in the deployment process but I haven't managed to get this to work yet.

I've tried setting up a config file in the .ebextensions folder:

commands: create-post-dir: command: "mkdir /opt/elasticbeanstalk/hooks/appdeploy/post" ignoreErrors: true

files: "/opt/elasticbeanstalk/hooks/appdeploy/post/restart_delayed_job.sh": mode: "000755" owner: root group: root content: | #!/usr/bin/env bash

  # Loading environment data
  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)
  EB_APP_PID_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k app_pid_dir)
  EB_SCRIPT_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k script_dir)
  EB_SUPPORT_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k support_dir)
  # Export EB_APP_LOG_DIR so we can access it when running bin/delayed_job below,
  # which accesses config/initializers/delayed_job.rb, which uses EB_APP_LOG_DIR.
  export EB_APP_LOG_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k app_log_dir)

  # Make sure the delayed_job.log exists and is owned by $EB_APP_USER
  touch $EB_APP_LOG_DIR/delayed_job.log
  chown $EB_APP_USER:$EB_APP_USER $EB_APP_LOG_DIR/delayed_job.log

  # 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

  # Now we can do the actual restart of the worker. Make sure to have double quotes when using env vars in the command.
  # For Rails 4, replace script/delayed_job with bin/delayed_job
  cd $EB_APP_DEPLOY_DIR
  su -s /bin/bash -c "bundle exec bin/delayed_job --pid-dir=$EB_APP_PID_DIR restart" $EB_APP_USER
  su -s /bin/bash -c "bundle exec bin/delayed_job --pid-dir=$EB_APP_PID_DIR status" $EB_APP_USER

Any help with getting this to work is greatly appreciated!

Upvotes: 2

Views: 898

Answers (1)

Steve L
Steve L

Reputation: 365

After spending way to much time on this, I finally got it working with the following. While it's not for delayed_job, I believe this solves the same issue of getting the rails environment setup so that bundle will run.

.ebextensions/01_my_server.config

files:
"/opt/elasticbeanstalk/hooks/appdeploy/post/99_restart_my_servers.sh":
    mode: "000755"
    owner: root
    group: root
    content: |
        #!/usr/bin/env bash
        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)
        EB_APP_PID_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k app_pid_dir)
        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

        mkdir -p /home/webapp
        chown webapp:webapp /home/webapp
        mkdir -p /home/webapp/pids
        chown webapp:webapp /home/webapp/pids

        cd $EB_APP_DEPLOY_DIR
        su -m -c "bundle exec bin/my_server.rb --pid-dir=/home/webapp/pids restart" -s /bin/bash webapp

Things of note:

  • Use of the su -m option to keep the environment vars.
  • Use of the su -s option to specify the shell for the webapp user.
  • The daemon pid needs to be stored outside of /var/app/current, as this gets deleted with each new deploy (and therefore breaks restarting the daemon)

Upvotes: 1

Related Questions