如何在Amazon Elastic Beanstalk上部署rails项目时自动重新启动delayed_job? [英] How to automatically restart delayed_job when deploying a rails project on Amazon Elastic Beanstalk?

查看:149
本文介绍了如何在Amazon Elastic Beanstalk上部署rails项目时自动重新启动delayed_job?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Amazon Elastic Beanstalk 上托管rails项目,我尝试配置一个容器命令在每次部署后自动重新启动服务器上的 delayed_job 工作人员。



我试过这个:

  container_commands:
restartdelayedjob:
command: RAILS_ENV = production script / delayed_job --pid-dir = / home / ec2-user / pids start
cwd:/ var / app / current

但是,似乎在重新启动工作人员后部署推送版本,因此工作无法由工作人员处理。



当我通过ssh连接我的实例时,杀死工作进程和res从部署的版本文件夹挞一个新的一切,一切正常。



您有什么想法可以处理吗?



谢谢

解决方案

根据 Amazon文档 container_commands


它们在应用程序和Web服务器设置完成之后运行,应用程序版本文件已被解压缩,但在部署应用程序版本之前


(强调我的)



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


是解压缩的应用程序。


这意味着如果要运行 delayed_job 刚刚解压缩的应用程序的目录(但尚未部署),不要覆盖 cwd ,它应该为即将部署的应用程序启动delayed_job。 / p>

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



更新:



我现在已经设置好了,发现通过标准 container_commands - 基本上delayed_job将在它仍然在 / var / app / onde中启动ck 目录。通常这是可以的,但是我有一些工作有一些问题,因为这个路径被卡住了会导致错误,因为应用程序现在在 / var / app / current 中。 / p>

我发现一个无文档(如此警告!)方法,您可以添加脚本以在应用服务器重新启动后运行(并且您的新部署在 / var / app / current )。



基本上弹性Beanstalk将执行 / opt / elasticbeanstalk /钩子/ appdeploy / post 重新启动Web服务器后。这意味着如果你删除这个目录中的shell脚本,它们将被运行。



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

 #!/ usr / bin / env bash 
。 / opt / elasticbeanstalk / support / envvars
cd $ EB_CONFIG_APP_CURRENT
su -cRAILS_ENV = production script / delayed_job --pid-dir = $ EB_CONFIG_APP_SUPPORT / pids restart$ EB_CONFIG_APP_USER

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

 命令
create_post_dir:
命令:mkdir / opt / elasticbeanstalk / hooks / appdeploy / post
ignoreErrors:true
文件:
/ opt /elasticbeanstalk/hooks/appdeploy/post/99_restart_delayed_job.sh:
模式:000755
所有者:root
组:root
源:http://YOUR_BUCKET.s3 .amazonaws.com / 99_restart_delayed_job.sh

部署时,您应该在 /var/log/eb-tools.log

  2013-05- 16 01:20:53,759 [INFO](6467 MainThread)[directoryHooksExecutor.py-29] [root directoryHooksExecutor info]执行目录:/ opt / elasticbeanstalk / hooks / appdeploy / post / 
2013-05-16 01: 20:53,760 [INFO ](6467 MainThread)[directoryHooksExecutor.py-29] [root directoryHooksExecutor info]执行脚本:/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]从脚本输出:delayed_job:尝试停止进程与pid 6139 ...
delayed_job:进程与pid 6139成功停止。

2013-05-16 01:21:02,620 [INFO](6467 MainThread)[directoryHooksExecutor.py-29] [root directoryHooksExecutor info]脚本成功。

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


I'm hosting a rails project on Amazon Elastic Beanstalk and I try to configure a container command to automatically restart my delayed_job worker on the server after each deployment.

I tried with this one :

container_commands:
  restartdelayedjob:
    command: "RAILS_ENV=production script/delayed_job --pid-dir=/home/ec2-user/pids start"
    cwd: /var/app/current

But, it seems that the pushed version is deployed after the restarting of the worker so the jobs failed to be processed by the worker.

When I connect on my instance by ssh, kill the worker process and restart a new one from the deployed version folder, everything works fine.

Do you have any ideas of how I can handle this?

Thanks

解决方案

As per the Amazon documentation for container_commands:

They run after the application and web server have been set up and the application version file has been extracted, but before the application version is deployed.

(emphasis mine)

This means at that point /var/app/current which you are setting as the cwd for your command is still pointing to the previous version. However by default, from the docs again, cwd:

is the directory of the unzipped application.

This means that if you want to run delayed_job from the directory of the app that just got extracted (but not yet deployed), don't override cwd and it should start the delayed_job for the app that's about to be deployed.

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

Update:

I've now set this up myself and found there's limitations to doing it via the standard container_commands - basically delayed_job will be started while it is still in the /var/app/ondeck directory. Usually this is OK, but I had some issues with some jobs because that path had stuck around it would cause errors as the app was now in /var/app/current.

I found an undocumented (so warning!) approach that you can add scripts to be run AFTER your app server is restarted (and your new deploy is in /var/app/current).

Basically Elastic Beanstalk will execute any scripts in /opt/elasticbeanstalk/hooks/appdeploy/post after the web server is restarted. This means if you drop shell scripts in this directory they will be run.

I created a shell script like this:

#!/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

I uploaded this script to an S3 bucket, and made sure it was "public". You can then use an options script in your .ebextensions directory (eg. 99delayed_job.config) to deploy this script as part of your app deploy, taking note that the post directory might not exist:

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

When you deploy you should see something like this in your /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.

As I said, putting stuff in this "post" directory is undocumented - but hopefully at some point Amazon add actual support to the .options scripts to run commands post-deploy, in that case you could move this to the officially supported approach.

这篇关于如何在Amazon Elastic Beanstalk上部署rails项目时自动重新启动delayed_job?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆