AWS Elastic Beanstalk 和 Whenever Gem

AWS Elastic Beanstalk and Whenever Gem

我有一个 Rails 4.2.1 应用程序,使用 Ruby 2.2。我正在尝试使用 Whenever Gem 从我的代码库更新我的 Elastic Beanstalk 上的 cron 任务。我关注了 AWS 的一些资源,您可以在其中将文件添加到 .ebextensions 文件夹并通过 shell 文件使用 EB 的 post 部署挂钩。这里有一些资源:

按照博客文章,我添加了下面的文件,取消了有关 .ebextensions 文件夹中文件的 gitignore 行的注释,并部署了我的应用程序。不幸的是,我已经能够看到任何变化。我检查了日志文件(log/eb-tools.loglog/cron 等),并为我创建的 shell 文件搜索了所有日志文件,无论何时,等等。不过运气不好。

commands:
  create-post-dir:
  command: "mkdir /opt/elasticbeanstalk/hooks/appdeploy/post"
  ignoreErrors: true
files:
  "/opt/elasticbeanstalk/hooks/appdeploy/post/99_update_cron.sh"
  mode: "000755"
  owner: root
  group: root
  content: |
    #!/usr/bin/env bash
    # Using similar syntax as the appdeploy pre hooks that is managed by AWS

    # Loading 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_CURRENT_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k app_deploy_dir)
    EB_APP_PIDS_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k app_pid_dir)

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

    # Now we can do the actual restart of the worker. Make sure to have double quotes when using env vars in the command.
    su -c "cd $EB_APP_CURRENT_DIR; bundle exec whenever --update-cron --set='environment=$RACK_ENV'" - $EB_APP_USER

如何确保这个 shell 文件被调用?我可以在每次都没有新部署的情况下测试它吗?此外,如果 Whenever gem 不是最佳选择,我愿意接受其他选择。我主要希望能够在代码和版本控制下管理我的 cron 任务。

提前致谢!

更新:

我在更正 .ebextensions 文件夹名称中的拼写错误后解决了这个问题。之后,脚本被编译,日志消息开始出现。阅读日志消息后,我想出了以下配置脚本 (.ebextensions/01_cron.config):

files:
  "/opt/elasticbeanstalk/hooks/appdeploy/post/01_cron.sh":
    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"