Israel Barba
Israel Barba

Reputation: 1494

Cron Job with Whenever Gem, is failing in amazon elastic beanstalk

I've installed the gem: whenever-elasticbeanstalk but I can't install it correctly, what's wrong?

Errors:

[2015-02-03T22:34:04.789Z] INFO  [19261] - [CMD-

AppDeploy/AppDeployStage0/AppDeployPreHook] : Completed activity. Result:
  Successfully execute directory: /opt/elasticbeanstalk/hooks/appdeploy/pre.
[2015-02-03T22:34:04.789Z] INFO  [19261] - [CMD-AppDeploy/AppDeployStage0/EbExtensionPostBuild] : Starting activity...
[2015-02-03T22:34:06.365Z] INFO  [19261] - [CMD-AppDeploy/AppDeployStage0/EbExtensionPostBuild] : Activity execution failed, because: command failed with error code 1: Error occurred during build: Command cron_01_set_leader failed (Executor::NonZeroExitStatus)
    at /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/executor-1.0/lib/executor/exec.rb:81:in `sh'
    from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/executor-1.0/lib/executor/exec.rb:24:in `sh'
    from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.0/lib/elasticbeanstalk/cfn-wrapper.rb:51:in `call_cfn_script'
    from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.0/lib/elasticbeanstalk/cfn-wrapper.rb:28:in `run_config_sets'
    from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.0/lib/elasticbeanstalk/infrahooks/infra-embeddedpostbuild.rb:20:in `execute!'
    from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.0/lib/elasticbeanstalk/executable.rb:56:in `instance_eval'
    from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.0/lib/elasticbeanstalk/executable.rb:56:in `execute!'
    from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.0/lib/elasticbeanstalk/command.rb:213:in `block (3 levels) in exec_stage'
    from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.0/lib/elasticbeanstalk/activity.rb:169:in `call'
    from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.0/lib/elasticbeanstalk/activity.rb:169:in `exec'
    from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.0/lib/elasticbeanstalk/activity.rb:126:in `timeout_exec'
    from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.0/lib/elasticbeanstalk/activity.rb:110:in `block in create'
    from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.0/lib/elasticbeanstalk/activity.rb:109:in `instance_eval'
    from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.0/lib/elasticbeanstalk/activity.rb:109:in `create'
    from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.0/lib/elasticbeanstalk/command.rb:209:in `block (2 levels) in exec_stage'
    from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.0/lib/elasticbeanstalk/command.rb:207:in `each'
    from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.0/lib/elasticbeanstalk/command.rb:207:in `each_with_index'
    from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.0/lib/elasticbeanstalk/command.rb:207:in `block in exec_stage'
    from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.0/lib/elasticbeanstalk/activity.rb:169:in `call'
    from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.0/lib/elasticbeanstalk/activity.rb:169:in `exec'
    from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.0/lib/elasticbeanstalk/activity.rb:126:in `timeout_exec'
    from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.0/lib/elasticbeanstalk/activity.rb:110:in `block in create'
    from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.0/lib/elasticbeanstalk/activity.rb:109:in `instance_eval'
    from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.0/lib/elasticbeanstalk/activity.rb:109:in `create'
    from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.0/lib/elasticbeanstalk/command.rb:204:in `exec_stage'
    from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.0/lib/elasticbeanstalk/command.rb:158:in `execute!'
    from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.0/lib/elasticbeanstalk/command.rb:68:in `run'
    from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.0/lib/elasticbeanstalk/command-processor.rb:64:in `block (2 levels) in execute!'
    from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.0/lib/elasticbeanstalk/activity.rb:169:in `call'
    from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.0/lib/elasticbeanstalk/activity.rb:169:in `exec'
    from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.0/lib/elasticbeanstalk/activity.rb:126:in `timeout_exec'
    from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.0/lib/elasticbeanstalk/activity.rb:110:in `block in create'
    from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.0/lib/elasticbeanstalk/activity.rb:109:in `instance_eval'
    from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.0/lib/elasticbeanstalk/activity.rb:109:in `create'
    from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.0/lib/elasticbeanstalk/command-processor.rb:62:in `block in execute!'
    from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.0/lib/elasticbeanstalk/command-processor.rb:106:in `execute_command'
    from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.0/lib/elasticbeanstalk/command-processor.rb:60:in `execute!'
    from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.0/bin/command-processor:46:in `<top (required)>'
    from /opt/elasticbeanstalk/lib/ruby/bin/command-processor:23:in `load'
    from /opt/elasticbeanstalk/lib/ruby/bin/command-processor:23:in `<main>'

cron.config

files:
# Reload the on deployment
  /opt/elasticbeanstalk/hooks/appdeploy/post/10_reload_cron.sh:
    mode: "00700"
    owner: root
    group: root
    content: |
      #!/usr/bin/env bash
      . /opt/elasticbeanstalk/containerfiles/envvars
      cd $EB_CONFIG_APP_CURRENT
      su -c "/usr/local/bin/bundle exec setup_cron" $EB_CONFIG_APP_USER
  # Add Bundle to the PATH
  "/etc/profile.d/bundle.sh":
    mode: "000755"
    owner: root
    group: root
    content: |
      #!/usr/bin/env bash
      export PATH=$PATH:/usr/local/bin
    encoding: plain
container_commands:
  cron_01_set_leader:
    test: test ! -f /opt/elasticbeanstalk/containerfiles/.cron-setup-complete
    leader_only: true
    cwd: /var/app/ondeck
    command: su -c "/usr/local/bin/bundle exec create_cron_leader --no-update" $EB_CONFIG_APP_USER
  cron_02_write_cron_setup_complete_file:
    cwd: /opt/elasticbeanstalk/containerfiles
    command: touch .cron-setup-complete

policy:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Action": "ec2:*",
      "Effect": "Allow",
      "Resource": "*"
    },
    {
      "Effect": "Allow",
      "Action": "elasticloadbalancing:*",
      "Resource": "*"
    },
    {
      "Effect": "Allow",
      "Action": "cloudwatch:*",
      "Resource": "*"
    },
    {
      "Effect": "Allow",
      "Action": "autoscaling:*",
      "Resource": "*"
    }
  ]
}

Some clue for this problem? What does mean the constant $EB_CONFIG_APP_USER, when do I define it?

Upvotes: 1

Views: 1038

Answers (1)

Mark Berry
Mark Berry

Reputation: 19032

What version of Elastic Beanstalk AMI are you using? Starting with 2014.09, the old environment variables (like $EB_CONFIG_APP_USER) don't work. See the WARNING at the top of the whenever-elasticbeanstalk readme and that's gem's Issue 18.

They may update the whenever-elasticbeanstalk gem to handle the new environment variable approach, but in the meantime you are probably stuck with using the default whenever gem and writing your own code to call whenever --update. My whenever-cron.config looks like this. Note that it doesn't handle the "leader only" option.

# Use the whenever gem to update webapp's crontab from config/schedule.rb after successful deployment
# See notes in Gemfile re. "whenever" vs. "whenever-elasticbeanstalk"

# Adapted from http://junkheap.net/blog/2013/05/20/elastic-beanstalk-post-deployment-scripts/
# with environment corrections from 
# http://www.dannemanne.com/posts/post-deployment_script_on_elastic_beanstalk_restart_cron-whenever

commands:
  70.1-create-post-dir:
    # "mkdir -p" ignores error if directory already exists
    command: "mkdir -p /opt/elasticbeanstalk/hooks/appdeploy/post"

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

      # Load 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_DEPLOY_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k app_deploy_dir)
      # Export EB_APP_LOG_DIR so we can access it when running "whenever" below,
      # which accesses config/schedule.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 cron-whenever.log exists and is owned by $EB_APP_USER
      touch $EB_APP_LOG_DIR/cron-whenever.log
      chown $EB_APP_USER:$EB_APP_USER $EB_APP_LOG_DIR/cron-whenever.log

      # cron requires a home directory.  Make sure it exists and is owned by $EB_APP_USER.
      mkdir -p /home/$EB_APP_USER
      chown $EB_APP_USER:$EB_APP_USER /home/$EB_APP_USER

      # Set up correct environment and ruby version so that bundle can load all gems
      . $EB_SUPPORT_DIR/envvars
      . $EB_SCRIPT_DIR/use-app-ruby.sh

      # Run the whenever --update command to update the cron job for $EB_APP_USER.
      cd $EB_APP_DEPLOY_DIR
      whenever --update -u $EB_APP_USER

      # After setup, you can run "crontab -l -u webapp" to check the configuration.

Upvotes: 4

Related Questions