根据
Amazon关于container_commands
的文档,如下所示:
它们在应用程序和Web服务器设置完成且已提取应用程序版本文件之后运行,但在应用程序版本部署之前运行。
(我强调的)
这意味着此时您为命令设置的cwd
的/var/app/current
仍指向先前的版本。 但是从文档中默认情况下,cwd
如下:
是未解压缩应用程序的目录。
这意味着如果您想从刚提取但尚未部署的应用程序目录中运行delayed_job
,请勿覆盖cwd
,则应该启动即将部署的应用程序的延迟作业。
参考:http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/customize-containers-ec2.html#customize-containers-format-container_commands
更新:
我现在已经自己设置好了,发现通过标准的container_commands
进行设置存在限制——基本上,当它仍在/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
我将这个脚本上传到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.
就像我说的一样,把东西放到“post”目录下是没有记录的 - 但希望在某些时候Amazon会在.options脚本中添加实际支持以运行部署后的命令,在那种情况下,您可以将其移动到官方支持的方法。
container_commands
中,而不是files
配置选项中。 - King'ori Maina