每当宝石AWS弹性豆茎

时间:2018-01-09 05:41:42

标签: ruby-on-rails amazon-web-services whenever

道歉,如果这是重复的 - 有一些结果有类似的问题,但大多数答案都很陈旧,我无法让它工作。

我使用随时随地的gem在我的rails应用程序中每小时运行一个任务,但我似乎无法让AWS正确配置Cron设置。

我的第一个想法是通过SSH进入实例并运行每次更新/写入命令,但是我的任务没有运行。

在这里查看旧问题后,我创建了一个带有配置文件的ebextensions文件夹来运行更新命令但仍然没有乐趣。我使用了这个配置脚本,但并不是真的了解它,所以我认为我没有正确设置 - 有人可以帮忙吗?

files:
"/opt/elasticbeanstalk/hooks/appdeploy/post/99_update_cron.sh"
mode: "000755"
owner: root
group: root
content: |
#! /usr/bin/env bash
. /opt/elasticbeanstalk/containerfiles/envvars
su -c "cd $EB_CONFIG_APP_CURRENT; bundle exec whenever --update-cron" 
- $EB_CONFIG_APP_USER

编辑添加日志

2018-01-09T23:24:45.282Z] INFO  [1641]  - [Application update app-5c64-
180109_162309@15/AppDeployStage1/AppDeployPostHook] : Starting activity...
[2018-01-09T23:24:45.282Z] INFO  [1641]  - [Application update app-5c64-
180109_162309@15/AppDeployStage1/AppDeployPostHook/01_cron.sh] : Starting 
activity...
[2018-01-09T23:24:46.618Z] INFO  [1641]  - [Application update app-5c64-
180109_162309@15/AppDeployStage1/AppDeployPostHook/01_cron.sh] : Completed 
activity. Result:
[write] crontab file updated
[2018-01-09T23:24:46.618Z] INFO  [1641]  - [Application update app-5c64-
180109_162309@15/AppDeployStage1/AppDeployPostHook] : Completed activity. 
Result:
Successfully execute hooks in directory 
/opt/elasticbeanstalk/hooks/appdeploy/post.
[2018-01-09T23:24:46.618Z] INFO  [1641]  - [Application update app-5c64-
180109_162309@15/AppDeployStage1] : Completed activity. Result:
Application version switch - Command CMD-AppDeploy stage 1 completed
[2018-01-09T23:24:46.618Z] INFO  [1641]  - [Application update app-5c64-
180109_162309@15/AddonsAfter] : Starting activity...
[2018-01-09T23:24:46.618Z] INFO  [1641]  - [Application update app-5c64-
180109_162309@15/AddonsAfter/ConfigLogRotation] : Starting activity...
[2018-01-09T23:24:46.618Z] INFO  [1641]  - [Application update app-5c64-
180109_162309@15/AddonsAfter/ConfigLogRotation/10-config.sh] : Starting 
activity...
[2018-01-09T23:24:46.778Z] INFO  [1641]  - [Application update app-5c64-
180109_162309@15/AddonsAfter/ConfigLogRotation/10-config.sh] : Completed 
activity. Result:
Disabled forced hourly log rotation.
[2018-01-09T23:24:46.779Z] INFO  [1641]  - [Application update app-5c64-
180109_162309@15/AddonsAfter/ConfigLogRotation] : Completed activity. 
Result:
Successfully execute hooks in directory 
/opt/elasticbeanstalk/addons/logpublish/hooks/config.
[2018-01-09T23:24:46.779Z] INFO  [1641]  - [Application update app-5c64-
180109_162309@15/AddonsAfter] : Completed activity.
[2018-01-09T23:24:46.779Z] INFO  [1641]  - [Application update app-5c64-

编辑2我的新配置代码现在形成以下错误:

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/01_cron.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.

2 个答案:

答案 0 :(得分:1)

首先 - 你的.config文件是如何格式化的?该文件采用YAML格式,因此空格/缩进很重要。此外,您可以确认文件的名称及其所在位置吗?应该是这样的:

<强> .ebextensions / 01-whenever.config

files:
  "/opt/elasticbeanstalk/hooks/appdeploy/post/99_update_cron.sh"
    mode: "000755"
    owner: root
    group: root
    content: |
      #!/usr/bin/env bash
      . /opt/elasticbeanstalk/containerfiles/envvars
      su -c "cd $EB_CONFIG_APP_CURRENT; bundle exec whenever --update-cron" - $EB_CONFIG_APP_USER

如果不能解决问题,请检查以确保通过/opt/elasticbeanstalk/hooks/appdeploy/post/99_update_cron.sh连接正确创建文件eb ssh

然后,检查/var/log/eb-activity.log处的日志文件,并查找正在执行99_update_cron.sh挂钩的部分。可能存在一些信息性错误消息。

答案 1 :(得分:0)

如果您有多个 EC2 实例正在运行,并且只想在一个实例中运行 When ,您可能需要以下配置:

files:
  "/tmp/99_update_cron.template":
    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"

container_commands:
  enable_cron:
    command: "mv /tmp/99_update_cron.template /opt/elasticbeanstalk/hooks/appdeploy/post/99_update_cron.sh"
    leader_only: true