Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Elastic Beanstalk and Whenever Gem

I have a Rails 4.2.1 app, using Ruby 2.2. I'm trying to use the Whenever Gem to update cron tasks on my Elastic Beanstalk from my code base. I've followed a few resources from AWS where you can add files to the .ebextensions folder and use EB's post deployment hook via shell files. Here are a couple resources:

  • https://forums.aws.amazon.com/thread.jspa?threadID=137136
  • http://www.dannemanne.com/posts/post-deployment_script_on_elastic_beanstalk_restart_delayed_job
  • http://blog.endpoint.com/2015/01/elastic-beanstalk-whenever.html

Following the blog articles, I added the file below, uncommented the gitignore line about files in the .ebextensions folder, and deployed my app. Unfortunatley, I have been able to see any changes. I've checked the log files (log/eb-tools.log, log/cron, etc.), and grepped all the log files for the shell file I create, whenever, and more. No luck though.

commands:
  create-post-dir:
  command: "mkdir /opt/elasticbeanstalk/hooks/appdeploy/post"
  ignoreErrors: true
files:
  "/opt/elasticbeanstalk/hooks/appdeploy/post/99_update_cron.sh"
  mode: "000755"
  owner: root
  group: root
  content: |
    #!/usr/bin/env bash
    # Using similar syntax as the appdeploy pre hooks that is managed by AWS

    # Loading environment data
    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_APP_USER=$(/opt/elasticbeanstalk/bin/get-config container -k app_user)
    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

    # Now we can do the actual restart of the worker. Make sure to have double quotes when using env vars in the command.
    su -c "cd $EB_APP_CURRENT_DIR; bundle exec whenever --update-cron --set='environment=$RACK_ENV'" - $EB_APP_USER

How can I make sure this shell file is getting called? Can I test it without a new deployment each time? Also, I'm open to other options if the Whenever gem is not the best option. I mainly want to be able to have my cron tasks managed in code and under version control.

Thanks in advance!

UPDATE:

  • I had a type on the .ebextensions folder, which was causing it to not be added. After that was fixed, I was able to read error messages, and create a cron script that updated the crontab using the Whenever gem.
like image 958
Evan Johnson Avatar asked Jan 08 '23 09:01

Evan Johnson


1 Answers

I was able to figure this out after fixing a typo in my .ebextensions folder name. Afterwards, the scripts got compiled, and log messages started to appear. After reading the log messages, I came up with the following config script (.ebextensions/01_cron.config):

files:
  "/opt/elasticbeanstalk/hooks/appdeploy/post/01_cron.sh":
    mode: "000755"
    owner: root
    group: root
    content: |
      #!/usr/bin/env bash
      # Using similar syntax as the appdeploy pre hooks that is managed by AWS
      set -xe

      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_DEPLOY_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k app_deploy_dir)

      . $EB_SUPPORT_DIR/envvars
      . $EB_SCRIPT_DIR/use-app-ruby.sh

      cd $EB_DEPLOY_DIR
      su -c "bundle exec whenever --update-cron"
      su -c "crontab -l"
like image 159
Evan Johnson Avatar answered Jan 16 '23 21:01

Evan Johnson