如何在 Elastic Beanstalk 上设置延迟作业时修复“需要”错误

Posted

技术标签:

【中文标题】如何在 Elastic Beanstalk 上设置延迟作业时修复“需要”错误【英文标题】:How to get fix 'require' errors when setting up delayed_job on Elastic Beanstalk 【发布时间】:2014-11-27 15:58:24 【问题描述】:

我无法在 Elastic Beanstalk 上运行延迟作业。我正在使用 64 位 Amazon Linux 2014.03 v1.0.0 运行 Ruby 2.1(Passenger Standalone) 容器。

这是我的配置脚本 (delayed_job.config) ...

files:
  "/opt/elasticbeanstalk/hooks/appdeploy/post/99_restart_delayed_job.sh":
    mode: "000755"
    owner: root
    group: root
    encoding: plain
    content: |
      #!/usr/bin/env bash
      . /opt/elasticbeanstalk/support/envvars
      cd $EB_CONFIG_APP_CURRENT
      su -c "RAILS_ENV=production bin/delayed_job --pid-dir=$EB_CONFIG_APP_SUPPORT/pids restart" $EB_CONFIG_APP_USER

99_restart_delayed_job.sh 脚本存在并运行......但后来我偶然发现了这个错误。

2014-10-02 15:28:32,332 [INFO] (17387 MainThread) [directoryHooksExecutor.py-29] [root directoryHooksExecutor info] Script succeeded.
2014-10-02 15:28:32,402 [INFO] (17448 MainThread) [directoryHooksExecutor.py-29] [root directoryHooksExecutor info] Executing directory: /opt/elasticbeanstalk/hooks/appdeploy/post/
2014-10-02 15:28:32,402 [INFO] (17448 MainThread) [directoryHooksExecutor.py-29] [root directoryHooksExecutor info] Executing script: /opt/elasticbeanstalk/hooks/appdeploy/post/99_restart_delayed_job.sh
/usr/share/ruby/vendor_ruby/2.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- bundler/setup (LoadError)
    from /usr/share/ruby/vendor_ruby/2.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from /var/app/current/config/boot.rb:4:in `<top (required)>'
    from /usr/share/ruby/vendor_ruby/2.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from /usr/share/ruby/vendor_ruby/2.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from /var/app/current/config/application.rb:1:in `<top (required)>'
    from /usr/share/ruby/vendor_ruby/2.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from /usr/share/ruby/vendor_ruby/2.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from /var/app/current/config/environment.rb:2:in `<top (required)>'
    from /usr/share/ruby/vendor_ruby/2.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from /usr/share/ruby/vendor_ruby/2.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from bin/delayed_job:3:in `<main>'

2014-10-02 15:28:32,440 [ERROR] (17448 MainThread) [directoryHooksExecutor.py-33] [root directoryHooksExecutor error] Script /opt/elasticbeanstalk/hooks/appdeploy/post/99_restart_delayed_job.sh failed with returncode 1

我已经通过this other thread on SO 向我展示了如何设置。我的问题是我不知道是什么阻止了脚本正常运行。

如果我通过 SSH 连接到 EC2 实例,我可以毫无错误地运行它...

RAILS_ENV=production bin/delayed_job --pid-dir=$EB_CONFIG_APP_SUPPORT/pids restart

虽然这要求我输入密码...

su -c "RAILS_ENV=production bin/delayed_job --pid-dir=$EB_CONFIG_APP_SUPPORT/pids restart" $EB_CONFIG_APP_USER

我可以通过这样做来避免...

sudo su -c "RAILS_ENV=production bin/delayed_job --pid-dir=$EB_CONFIG_APP_SUPPORT/pids restart" $EB_CONFIG_APP_USER

见:'How to automatically restart delayed_job when deploying a rails project on Amazon Elastic Beanstalk?'

更新 1:2014-10-15

应用-l 选项并更改传入的目录后,我收到此错误...

2014-10-15 06:17:28,673 [INFO] (4417 MainThread) [directoryHooksExecutor.py-29] [root directoryHooksExecutor info] Executing script: /opt/elasticbeanstalk/hooks/appdeploy/post/99_restart_delayed_job.sh
2014-10-15 06:17:30,374 [INFO] (4417 MainThread) [directoryHooksExecutor.py-29] [root directoryHooksExecutor info] Output from script: /opt/rubies/ruby-2.1.2/lib/ruby/gems/2.1.0/gems/daemons-1.1.9/lib/daemons/application.rb:393:in `kill': Operation not permitted (Errno::EPERM)
    from /opt/rubies/ruby-2.1.2/lib/ruby/gems/2.1.0/gems/daemons-1.1.9/lib/daemons/application.rb:393:in `stop'
    from /opt/rubies/ruby-2.1.2/lib/ruby/gems/2.1.0/gems/daemons-1.1.9/lib/daemons/application_group.rb:171:in `block (2 levels) in stop_all'

2014-10-15 06:17:30,374 [ERROR] (4417 MainThread) [directoryHooksExecutor.py-33] [root directoryHooksExecutor error] Script /opt/elasticbeanstalk/hooks/appdeploy/post/99_restart_delayed_job.sh failed with returncode 1

更新 2:2014-10-15

原来上面的错误是由root创建的现有pid引起的(在调试时我已经手动启动了delayed_job),因此c2用户无法重新启动/杀死它,因此出现错误。

【问题讨论】:

【参考方案1】:

据我所知,问题是切换到$EB_CONFIG_APP_USER linux 用户时没有建立环境/路径变量。我做了三处改动:

    -l 选项添加到su 命令以模拟$EB_CONFIG_APP_USER 的完整登录。 作为-l 选项的副作用,必须将更改目录命令带入-c 选项。 作为一个很好的措施,但可能不是必需的,包括 bundle exec 以确保使用正确的宝石。

这是我正在运行的content: 区域:

#!/usr/bin/env bash
. /opt/elasticbeanstalk/support/envvars
su -l -c "cd $EB_CONFIG_APP_CURRENT && RAILS_ENV=production bundle exec bin/delayed_job --pid-dir=$EB_CONFIG_APP_SUPPORT/pids restart" $EB_CONFIG_APP_USER

【讨论】:

我应用了建议的更改并得到了一个新错误(请参阅问题的更新)但是是的,您的建议是有道理的。这无疑为我指明了正确的方向,因此我必须通过 SSH 连接到我的实例并手动执行此操作,直到某些工作正常为止。将更新进度。 您的错误似乎表明$EB_CONFIG_APP_USER 无权停止已经运行的进程。确保 bin/delayed_job 没有在您之前的故障排除步骤中以 root 身份在后台运行。同时验证bin/delayed_job的文件权限。 显然环境变量随着下一个 Beanstalk (2014.09) 而消失了。 this AWS forum thread 中的解决方法。 this answer 和 this blog post 中有关新方法的延迟作业的详细信息。

以上是关于如何在 Elastic Beanstalk 上设置延迟作业时修复“需要”错误的主要内容,如果未能解决你的问题,请参考以下文章

如何在 AWS Elastic Beanstalk 上设置 HTTPS

如何在 Elastic Beanstalk 上设置延迟作业时修复“需要”错误

在 Elastic Beanstalk 上设置 Docker 容器主机名

使用 Postgres 在 Elastic Beanstalk 上设置 Django

在 AWS Elastic Beanstalk 应用程序上启用 HTTPS

AWS Elastic beanstalk:使用 docker 镜像时如何设置 ulimit