简体   繁体   English

每当 Gem AWS 弹性 beanstalk

[英]Whenever Gem AWS elastic beanstalk

Apologies if this is a duplicate - there are a few results with similar questions but most of the answers are quite dated and I am having trouble getting it to work.抱歉,如果这是重复的 - 有一些类似问题的结果,但大多数答案都过时了,我很难让它发挥作用。

I am using the whenever gem to run a single task every hour in my rails app but I can't seem to get AWS to configure the Cron settings right.我正在使用 whenever gem 在我的 Rails 应用程序中每小时运行一个任务,但我似乎无法让 AWS 正确配置 Cron 设置。

My first thought was to SSH into the instance and run the whenever update /write command which went through fine but my task didn't run.我的第一个想法是将 SSH 放入实例并运行 whenever update /write 命令,该命令运行良好,但我的任务没有运行。

After reviewing the old questions on here I created a an ebextensions folder with a config file to run the update command but still no joy.在回顾了这里的旧问题后,我创建了一个带有配置文件的 ebextensions 文件夹来运行更新命令,但仍然没有任何乐趣。 I used this config script but don't really understand it so I assume I have not set it up right - can someone please assist?我使用了这个配置脚本,但并不真正理解它,所以我认为我没有正确设置它 - 有人可以帮忙吗?

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

EDIT Added Logs编辑添加的日志

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-

EDIT 2 my new config code that now forms the following errors:编辑 2 我的新配置代码现在 forms 以下错误:

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.

First off - is that how your .config file is formatted?首先 - 这就是你的.config文件的格式吗? The file is in YAML format, so whitespace/indenting is important.该文件采用 YAML 格式,因此空格/缩进很重要。 Also, can you confirm what the file is called and where it is located?另外,您能否确认该文件的名称和位置? Should look like this:应该是这样的:

.ebextensions/01-whenever.config .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

If that doesn't fix it, check to make sure that the file /opt/elasticbeanstalk/hooks/appdeploy/post/99_update_cron.sh is being created correctly by connecting via eb ssh .如果这不能解决它,请检查以确保文件/opt/elasticbeanstalk/hooks/appdeploy/post/99_update_cron.sh通过eb ssh连接正确创建。

Then, check the log file at /var/log/eb-activity.log and look for the section where your 99_update_cron.sh hook is being executed.然后,检查/var/log/eb-activity.log的日志文件并查找正在执行99_update_cron.sh挂钩的部分。 There may be some informative error messages.可能会有一些信息性错误消息。

If you have multiple EC2 instances running and want to run Whenever only in one instance, you probably want the following config:如果您有多个 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

2022 AWS EB CHANGED - LINUX 2 2022 AWS EB 变更 - LINUX 2

Note if you are running the newer LINUX 2 platform on elastic beanstalk, the custom platform hooks most people refer to here are deprecated and instead you need to do the following:请注意,如果您在 elastic beanstalk 上运行较新的 LINUX 2 平台,大多数人在这里提到的自定义平台挂钩已弃用,您需要执行以下操作:

create a simple shell script in the new.platform/hooks/postdeploy directory (create it if it doesn't exist).在 new.platform/hooks/postdeploy 目录中创建一个简单的 shell 脚本(如果不存在则创建它)。

.platform/hooks/postdeploy/01_whenever.sh .platform/hooks/postdeploy/01_whenever.sh

#!/usr/bin/env bash
cd /var/app/current
bundle exec whenever --update-crontab

However there is a catch, if you need any environment variables that are not in rails credentials or.env files, the environment variables are not available to the system so when your cron task runs, if it needs to for example to run rails, rails will break since it doesn't have access to the env variables.但是有一个问题,如果您需要任何不在 rails 凭据或 .env 文件中的环境变量,则系统无法使用环境变量,因此当您的 cron 任务运行时,如果它需要运行 rails,rails将会中断,因为它无法访问环境变量。

To solve this, you need to either, move those variables into rails credentials or.env or make the eb env vars available to the system (see below).要解决这个问题,您需要将这些变量移动到 rails credentials 或 .env 中,或者使 eb 环境变量对系统可用(见下文)。

under.ebextensions create a file called 01_export_vars.config under.ebextensions 创建一个名为 01_export_vars.config 的文件

commands:
  setvars:
    command: /opt/elasticbeanstalk/bin/get-config environment | jq -r 'to_entries | .[] | "export \(.key)=\"\(.value)\""' > /etc/profile.d/sh.local
packages:
  yum:
    jq: []

Now, when cron runs rails it will have access to eb env vars and run correctly.现在,当 cron 运行 rails 时,它将可以访问 eb env vars 并正确运行。

Do note, that if you add a new eb env var you need to redeploy so that this script gets run again and exports that eb env var.请注意,如果您添加一个新的 eb env var,则需要重新部署,以便该脚本再次运行并导出该 eb env var。

This is the best solution I have found, but if someone knows a better way, please tell.这是我找到的最好的解决方案,但如果有人知道更好的方法,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM