如何在Amazon Elastic Beanstalk上部署rails项目时自动重启delayed_job?

Ast*_*skk 15 deployment ruby-on-rails amazon-web-services delayed-job amazon-elastic-beanstalk

我在Amazon Elastic Beanstalk上托管一个rails项目,我尝试配置一个容器命令,以便在每次部署后自动重启服务器上的delayed_job worker.

我试过这个:

container_commands:
  restartdelayedjob:
    command: "RAILS_ENV=production script/delayed_job --pid-dir=/home/ec2-user/pids start"
    cwd: /var/app/current
Run Code Online (Sandbox Code Playgroud)

但是,似乎在重新启动worker之后部署了推送版本,因此作业无法处理作业.

当我通过ssh连接我的实例时,杀死工作进程并从已部署的版本文件夹重新启动一个新进程,一切正常.

你对我如何处理这个有什么想法吗?

谢谢

Mar*_*cin 21

根据亚马逊文档container_commands:

它们在设置应用程序和Web服务器并且已提取应用程序版本文件之后但在部署应用程序版本之前运行.

(强调我的)

这意味着在/var/app/current您设置的那一点上,您cwd的命令仍然指向以前的版本.但是,默认情况下,再次从文档中cwd:

是解压缩的应用程序的目录.

这意味着如果您想delayed_job从刚刚解压缩但尚未部署的应用程序目录中运行,请不要覆盖cwd,并且应该为即将部署的应用程序启动delayed_job.

参考:http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/customize-containers-ec2.html#customize-containers-format-container_commands

更新:

我现在自己设置了这个,并发现通过标准来实现这一点container_commands- 基本上,delayed_job将在它仍在/var/app/ondeck目录中时启动.通常情况下这是可以的,但是我在某些工作中遇到了一些问题,因为该路径已经卡住了它会导致应用程序现在出现错误/var/app/current.

我找到了一个未记录的(如此警告!)方法,您可以添加脚本,以便在您的应用服务器重新启动后运行(并且您的新部署已经进入/var/app/current).

基本上Elastic Beanstalk将在/opt/elasticbeanstalk/hooks/appdeploy/post重新启动Web服务器后执行任何脚本.这意味着如果您将shell脚本放在此目录中,它们将被运行.

我创建了一个这样的shell脚本:

#!/usr/bin/env bash
. /opt/elasticbeanstalk/support/envvars
cd $EB_CONFIG_APP_CURRENT
su -c "RAILS_ENV=production script/delayed_job --pid-dir=$EB_CONFIG_APP_SUPPORT/pids restart" $EB_CONFIG_APP_USER
Run Code Online (Sandbox Code Playgroud)

我将此脚本上传到S3存储桶,并确保它是"公共"的.然后,您可以使用.ebextensions目录中的选项脚本(例如99delayed_job.config)将此脚本部署为应用程序部署的一部分,请注意该post目录可能不存在:

commands:
  create_post_dir:
    command: "mkdir /opt/elasticbeanstalk/hooks/appdeploy/post"
    ignoreErrors: true
files:
  "/opt/elasticbeanstalk/hooks/appdeploy/post/99_restart_delayed_job.sh":
    mode: "000755"
    owner: root
    group: root
    source: http://YOUR_BUCKET.s3.amazonaws.com/99_restart_delayed_job.sh
Run Code Online (Sandbox Code Playgroud)

部署时,您应该在以下内容中看到类似的内容/var/log/eb-tools.log:

2013-05-16 01:20:53,759 [INFO] (6467 MainThread) [directoryHooksExecutor.py-29] [root directoryHooksExecutor info] Executing directory: /opt/elasticbeanstalk/hooks/appdeploy/post/
2013-05-16 01:20:53,760 [INFO] (6467 MainThread) [directoryHooksExecutor.py-29] [root directoryHooksExecutor info] Executing script: /opt/elasticbeanstalk/hooks/appdeploy/post/99_restart_delayed_job.sh
2013-05-16 01:21:02,619 [INFO] (6467 MainThread) [directoryHooksExecutor.py-29] [root directoryHooksExecutor info] Output from script: delayed_job: trying to stop process with pid 6139...
delayed_job: process with pid 6139 successfully stopped.

2013-05-16 01:21:02,620 [INFO] (6467 MainThread) [directoryHooksExecutor.py-29] [root directoryHooksExecutor info] Script succeeded.
Run Code Online (Sandbox Code Playgroud)

正如我所说的那样,把东西放在这个"post"目录中是没有记录的 - 但希望在某些时候亚马逊添加对.options脚本的实际支持以在部署后运行命令,在这种情况下你可以将其移动到官方支持的方法.

  • 谢谢!也为我工作过.只是一些注意事项: - 如果你正在使用rails 4,修改shell脚本来执行bin/delayed_job而不是script/delayed_job; - 我必须在脚本cd $ EB_CONFIG_APP_CURRENT行之后添加这个(chmod + x bin/delayed_job).获得许可错误; - 如果在执行脚本时遇到"没有此类文件或目录"错误,则可能需要在.ebextensions .config文件中添加一些行.添加dos2unix软件包安装和命令dos2unix /opt/elasticbeanstalk/hooks/appdeploy/post/99_restart_delayed_job.sh (2认同)
  • 神奇的99号是什么,为什么不是1或79,我们可以做109吗?我在几个地方看到90年代的东西,但是没人能解释! (2认同)

Jon*_*ran 6

运行Ruby 2.1(Passenger Standalone)的64位亚马逊Linux 2014.09 v1.1.0上,由于这篇文章得到了它.

请注意,此脚本以root身份运行,但您的worker应作为webapp用户运行.

# Adds a post-deploy hook such that after a new version is deployed
# successfully, restarts the delayed_job workers.
#
# http://stackoverflow.com/questions/14401204/how-to-automatically-restart-delayed-job-when-deploying-a-rails-project-on-amazo
# http://www.dannemanne.com/posts/post-deployment_script_on_elastic_beanstalk_restart_delayed_job
files:
  "/opt/elasticbeanstalk/hooks/appdeploy/post/50_restart_delayed_job.sh":
    mode: "000755"
    owner: root
    group: root
    encoding: plain
    content: |
      #!/usr/bin/env bash

      EB_SCRIPT_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k script_dir)
      EB_APP_CURRENT_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k app_deploy_dir)
      EB_APP_USER=$(/opt/elasticbeanstalk/bin/get-config container -k app_user)
      EB_SUPPORT_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k support_dir)
      EB_APP_PIDS_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k app_pid_dir)

      . $EB_SUPPORT_DIR/envvars
      . $EB_SCRIPT_DIR/use-app-ruby.sh

      cd $EB_APP_CURRENT_DIR

      # Switch to the webapp user.  Worker shouldn't be run as root.
      su -s /bin/bash -c "bundle exec bin/delayed_job --pid-dir=$EB_APP_PIDS_DIR restart" $EB_APP_USER
Run Code Online (Sandbox Code Playgroud)