Cron Job with Whenever Gem,在亚马逊弹性豆茎中失败

时间:2015-02-03 22:45:32

标签: ruby-on-rails amazon-web-services cron elastic-beanstalk

我已经安装了gem:whatever-elasticbeanstalk但我无法正确安装,出了什么问题?

  

错误:

[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
  

政策:

{
  "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": "*"
    }
  ]
}

这个问题的一些线索?什么意思是常量$ EB_CONFIG_APP_USER,我什么时候定义它?

1 个答案:

答案 0 :(得分:4)

您使用的是哪种版本的Elastic Beanstalk AMI?从2014.09开始,旧的环境变量(如$ EB_CONFIG_APP_USER)不起作用。请参阅when-elasticbeanstalk readme及其宝石Issue 18顶部的警告。

他们可能会更新whatever-elasticbeanstalk gem以处理新的环境变量方法,但与此同时,您可能仍然使用默认的whenever gem并编写自己的代码来调用whenever --update。我的whatever-cron.config看起来像这样。请注意,它不会仅仅处理&#34;领导者#34;选项。

# 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.