首页 > 解决方案 > ElasticBeanstalk Ruby PostDeploy 脚本任务不可能

问题描述

我们最近将我们的 ruby​​/elasticbeanstalk 平台更新为 AWS Linux 2 / Ruby(在 64 位 Amazon Linux 2/3.2.0 上运行的 Ruby 2.7)

我们的 Ruby 部署的一部分是 delay_job(守护进程 gem)

在多次尝试从 .platform/hooks/postdeploy/ 文件夹中获取 bash 脚本之后,我正式宣布我被卡住了。这是来自 eb-engine.log 的错误:

2020/12/08 04:18:44.162454 [INFO] Running platform hook: .platform/hooks/postdeploy/restart_delayed_job.sh
2020/12/08 04:18:44.191301 [ERROR] An error occurred during execution of command [app-deploy] - [RunAppDeployPostDeployHooks]. Stop running the command. Error: Command .platform/hooks/postdeploy/restart_delayed_job.sh failed with error exit status 127

2020/12/08 04:18:44.191327 [INFO] Executing cleanup logic
2020/12/08 04:18:44.191448 [INFO] CommandService Response: {"status":"FAILURE","api_version":"1.0","results":[{"status":"FAILURE","msg":"Engine execution has encountered an error.","returncode":1,"events":[{"msg":"Instance deployment failed. For details, see 'eb-engine.log'.","timestamp":1607401124,"severity":"ERROR"}]}]}```

这是我尝试过的众多脚本之一:

#!/bin/bash
#Using similar syntax as the appdeploy pre hooks that is managed by AWS
exec 3>&1 4>&2
trap 'exec 2>&4 1>&3' 0 1 2 3
exec 1>delayed_job_err.out 2>&1

# Loading environment data
# source /etc/profile.d/sh.local #created from other .ebextension file
EB_APP_USER=$(/opt/elasticbeanstalk/bin/get-config platformconfig -k AppUser)
EB_APP_CURRENT_DIR=$(/opt/elasticbeanstalk/bin/get-config platformconfig -k AppDeployDir)
#EB_APP_PIDS_DIR=/home/webapp/pids

/opt/elasticbeanstalk/bin/get-config environment | jq -r 'to_entries | .[] | "export \(.key)=\"\(.value)\""' > /tmp/envvars
source /tmp/envvars
cd /var/app
cd $EB_APP_CURRENT_DIR
su -s /bin/bash -c "bin/delayed_job restart" $EB_APP_USER```

这是延迟作业文件:

#!/usr/bin/env ruby

require File.expand_path(File.join(File.dirname(__FILE__), '..', 'config', 'environment'))
require 'delayed/command'
Delayed::Command.new(ARGV).daemonize

如您所见,我正在尽最大努力加载环境变量。在加载了环境变量的 EB Linux 2 主机中,delayed_job 似乎以 root 身份运行得很好。

total 12
-rwxrwxr-x 1 webapp webapp  179 Dec  8 04:15 001_load_envs.sh
-rw-r--r-- 1 root   root    251 Dec  8 04:46 delayed_job_err.out
-rwxrwxr-x 1 webapp webapp 1144 Dec  8 04:15 restart_delayed_job.sh
[root@ip-172-16-100-178 postdeploy]# cat delayed_job_err.out
/var/app/current/vendor/bundle/ruby/2.7.0/gems/json-1.8.6/lib/json/common.rb:155: warning: Using the last argument as keyword parameters is deprecated
delayed_job: warning: no instances running. Starting...
delayed_job: process with pid 5292 started.

任何帮助,将不胜感激..

标签: ruby-on-railsrubyamazon-web-servicesamazon-elastic-beanstalk

解决方案


我还在 Amazon Linux 2 上使用 elasticbeanstalk

我正在使用需要重新启动 postdeploy 的 resque。以下是我的 postdeploy 钩子,它重新启动 resque 工作人员 .platform/hooks/postdeploy/0020_restart_resque_workers.sh

#!/usr/bin/env bash
. /opt/elasticbeanstalk/deployment/env
cd /var/app/current/
su -c "RAILS_ENV=production bundle exec rake resque:restart_workers" webapp ||
echo "resque workers restarted."
true

注意环境变量设置。它只是执行/opt/elasticbeanstalk/deployment/env将为您提供环境。

希望您能够通过简单地重新启动延迟作业而不是重新启动工作人员的命令来使用上述脚本。


推荐阅读