我主持一个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的,杀了工作进程,并重新从部署的版本文件夹一个新的,一切工作正常。
你有我如何能处理这个任何想法?
谢谢
由于按照亚马逊的文档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
)。
基本上弹性青苗会在执行任何脚本/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
我上传这个脚本的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
当你部署你应该会看到像这样/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.
正如我所说的,把东西在这个“后”目录无证-但希望在某个时候亚马逊将增加实际的支持, .options
脚本运行命令部署后,在这种情况下,可以将这个移动到官方支持的做法。
在运行的Ruby 2.1(客运独立)64位Linux的亚马逊V1.1.0 2014.09,得到它的工作多亏了这个帖子 。
请注意,这个脚本以root身份运行,但你的工人应该运行的web应用程序的用户。
# 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
如果有任何人希望得到的delayed_job在最新ElasticBeanstalk工作(64位Linux的亚马逊2014.09 v1.0.9运行的Ruby 2.1(彪马)):我得到了它使用下面的代码(感谢工作damontorgerson )。 该文件就转至.ebextensions文件夹ruby.config。
# Install git in order to be able to bundle gems from git
packages:
yum:
git: []
files:
"/opt/elasticbeanstalk/hooks/appdeploy/post/50_restart_delayed_job":
mode: "000777"
owner: root
group: root
content: |
EB_SCRIPT_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k script_dir)
EB_APP_STAGING_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k app_staging_dir)
EB_CONFIG_APP_CURRENT=$(/opt/elasticbeanstalk/bin/get-config container -k app_deploy_dir)
EB_CONFIG_APP_LOGS=$(/opt/elasticbeanstalk/bin/get-config container -k app_log_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_CONFIG_APP_PIDS=$(/opt/elasticbeanstalk/bin/get-config container -k app_pid_dir)
. $EB_SUPPORT_DIR/envvars
. $EB_SCRIPT_DIR/use-app-ruby.sh
cd $EB_CONFIG_APP_CURRENT
. $EB_SUPPORT_DIR/envvars.d/sysenv
bin/delayed_job --pid-dir=/var/tmp restart
我得到了我,像这样的工作与“守护”宝石:
commands:
create_post_dir:
command: "mkdir /opt/elasticbeanstalk/hooks/appdeploy/post"
ignoreErrors: true
webapp_pids:
command: "mkdir /home/webapp/pids"
ignoreErrors: true
files:
"/opt/elasticbeanstalk/hooks/appdeploy/post/99_restart_delayed_job.sh":
mode: "000755"
owner: root
group: root
content: |
#!/usr/bin/env bash
. /opt/elasticbeanstalk/support/envvars
chown webapp:webapp /home/webapp/pids
su -l -c "$EB_CONFIG_APP_CURRENT/bin/delayed_job --pid-dir=/home/webapp/pids restart" $EB_CONFIG_APP_USER
echo "worker starting" >> /var/log/directory-hooks-executor.log
文章来源: How to automatically restart delayed_job when deploying a rails project on Amazon Elastic Beanstalk?