每当 Gem 的 Cron 作业在亚马逊弹性豆茎中失败

Posted

技术标签:

【中文标题】每当 Gem 的 Cron 作业在亚马逊弹性豆茎中失败【英文标题】:Cron Job with Whenever Gem, is failing in amazon elastic beanstalk 【发布时间】:2015-04-03 07:34:13 【问题描述】:

我已经安装了 gem:every-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】:

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

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

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

【讨论】:

以上是关于每当 Gem 的 Cron 作业在亚马逊弹性豆茎中失败的主要内容,如果未能解决你的问题,请参考以下文章

芹菜工人在 aws 弹性豆茎中失败 [退出:芹菜工人(退出状态 1;未预期)]

弹性豆茎和 django 的 Cron 作业

使用弹性 beantalk 和 SQS 处理失败的 cron 作业

Cron 工作在每当 gem 中不起作用

在亚马逊 AWS 弹性豆茎(或本地?)上安装解析仪表板

不支持请求方法“POST”和亚马逊弹性豆茎