【问题标题】:Using Upstart to manage Unicorn w/ rbenv + bundler binstubs w/ ruby-local-exec shebang使用 Upstart 管理 Unicorn w/ rbenv + bundler binstubs w/ ruby​​-local-exec shebang
【发布时间】:2011-12-29 11:41:34
【问题描述】:

好吧,这正在融化我的大脑。这可能与我不太了解 Upstart 的事实有关。提前很抱歉问了这么长的问题。

我正在尝试使用 Upstart 来管理 Rails 应用的 Unicorn 主进程。这是我目前的/etc/init/app.conf

description "app"

start on runlevel [2]
stop on runlevel [016]

console owner

# expect daemon

script
  APP_ROOT=/home/deploy/app
  PATH=/home/deploy/.rbenv/shims:/home/deploy/.rbenv/bin:$PATH
  $APP_ROOT/bin/unicorn -c $APP_ROOT/config/unicorn.rb -E production # >> /tmp/upstart.log 2>&1
end script

# respawn

效果很好 - Unicorns 开始时很棒。不好的是检测到的 PID 不是 Unicorn master 的,它是一个sh 进程。这本身并没有那么糟糕——如果我没有使用自动独角兽零停机部署策略的话。因为在我将-USR2 发送给我的独角兽主人后不久,一个新的主人产生了,而旧的主人死了……sh 进程也是如此。所以 Upstart 认为我的工作已经死了,我不能再用 restart 重新启动它,或者如果我想用 stop 停止它。

我玩过配置文件,尝试将 -D 添加到 Unicorn 行(如下所示:$APP_ROOT/bin/unicorn -c $APP_ROOT/config/unicorn.rb -E production -D)以守护 Unicorn,我添加了 expect daemon 行,但这也不起作用.我也试过expect fork。所有这些事情的各种组合都可能导致startstop 挂起,然后Upstart 对工作的状态感到非常困惑。那我得重启机器修复一下。

我认为 Upstart 在检测 Unicorn 何时/是否分叉时遇到问题,因为我在我的 $APP_ROOT/bin/unicorn 脚本中使用了 rbenv + ruby-local-exec shebang。这里是:

#!/usr/bin/env ruby-local-exec
#
# This file was generated by Bundler.
#
# The application 'unicorn' is installed as part of a gem, and
# this file is here to facilitate running it.
#

require 'pathname'
ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
  Pathname.new(__FILE__).realpath)

require 'rubygems'
require 'bundler/setup'

load Gem.bin_path('unicorn', 'unicorn')

此外,ruby-local-exec 脚本如下所示:

#!/usr/bin/env bash
#
# `ruby-local-exec` is a drop-in replacement for the standard Ruby
# shebang line:
#
#    #!/usr/bin/env ruby-local-exec
#
# Use it for scripts inside a project with an `.rbenv-version`
# file. When you run the scripts, they'll use the project-specified
# Ruby version, regardless of what directory they're run from. Useful
# for e.g. running project tasks in cron scripts without needing to
# `cd` into the project first.

set -e
export RBENV_DIR="${1%/*}"
exec ruby "$@"

所以我很担心里面有一个exec。它启动了一个 Ruby 进程,该进程启动了 Unicorn,它可能会或可能不会自行守护进程,这一切都首先发生在 sh 进程中......这让我严重怀疑 Upstart 跟踪所有这些的能力废话。

我正在尝试做的事情是否可能?据我了解,Upstart 中的expect 节只能被告知(通过daemonfork)预计最多有两个分叉。

【问题讨论】:

    标签: ruby-on-rails ruby unicorn upstart


    【解决方案1】:

    需要配置您的 upstart 作业,以便 upstart 确切知道它分叉了多少次。而且它只能分叉一次或两次,不能再分叉了。

    在 unix 领域有两个有助于运行程序的关键系统调用:forkexec

    fork 复制调用它的进程。一个进程调用fork,并将控制权返回给两个进程。每个进程都必须从 fork 返回的值中识别它是哪个(父进程或子进程)(有关详细信息,请参阅手册页)。

    exec 运行一个新程序,替换调用exec 的进程。

    当您只是在 shell 中运行命令时,shell 会调用 fork 来创建一个具有自己 id 的新进程,并且该新进程(经过一些设置后)会立即调用 exec 来启动您键入的命令。这就是大多数程序的运行方式,无论是通过 shell 还是您的窗口管理器或其他方式。 请参阅 C 中的 system 函数,该函数在大多数脚本语言中也有变体。

    如果您认为它效率低下,那么您可能是对的。这就是从昔日以来在 unix 中的做法,而且显然没有人愿意改变它。原因之一是exec没有替换了很多东西,包括(有时)打开的文件,以及进程的用户和组 ID。

    另一个原因是已经花费了很多努力来提高 fork 的效率,而且他们实际上做得很好 - 在现代 unix 中(在 CPU 的帮助下)fork 实际上复制的很少的过程。我想没有人愿意放弃所有这些工作。

    并且,(暂停生效)进程 pid。

    演示:

    mslade@mickpc:~$ echo $$
    3652
    mslade@mickpc:~$ bash
    mslade@mickpc:~$ echo $$
    6545
    mslade@mickpc:~$ exec bash
    mslade@mickpc:~$ echo $$
    6545
    mslade@mickpc:~$ exit
    exit
    mslade@mickpc:~$ echo $$
    3652
    

    大多数流行语言都有 fork 和 exec 的变体,包括 shell、C、perl、ruby 和 python。但不是java。

    因此,考虑到所有这些,您需要做的是确保您的新贵工作正常进行,确保它分叉的次数与新贵认为的相同。

    ruby-local-exec 中的exec 行实际上是一件好事,它可以防止分叉。同样load 不会启动新进程,它只是将代码加载到现有的 ruby​​ 解释器中并运行它。

    但是您的 shell 脚本在这一行中分叉:

    $APP_ROOT/bin/unicorn -c $APP_ROOT/config/unicorn.rb -E production # >> /tmp/upstart.log 2>&1
    

    为了防止这种情况,您可以将其更改为

    exec $APP_ROOT/bin/unicorn -c $APP_ROOT/config/unicorn.rb -E production # >> /tmp/upstart.log 2>&1
    

    如果你这样做,AFAICT 独角兽根本不应该分叉,而且你不需要告诉暴发户期待分叉。

    【讨论】:

    • 感谢您的详细解答!它只是帮助我在相同的情况下设置了 God+Upstart。
    猜你喜欢
    • 1970-01-01
    • 2010-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-04
    • 2012-06-26
    相关资源
    最近更新 更多