为什么我不会在我指定的端口上开始瘦身?

时间:2014-04-30 18:42:34

标签: ruby thin

我[误]用上帝管理瘦弱的工人,并得到一个意外的结果,我在诊断时遇到了麻烦。我的上帝watch阻止相当简单:

(1..4).each do |index|
  God.watch do |w|
    name     = "thin-#{index}"
    port     = 8300 + index
    pid_name = "thin.#{port}.pid"
    pid_path = God.pid_file_directory.join pid_name

    # General settings.
    w.name     = name
    w.interval = 5.seconds
    w.pid_file = pid_name

    # Configuration for thin instances.
    start_options = {
      address:     '0.0.0.0',
      environment: RAILS_ENV,
      only:        index,
      port:        port,
    }.inject("") { |s,(k,v)| s << " --#{k}=#{v}" }

    # Process management commands.
    bin       = "/vagrant/bin/thin -c #{RAILS_ROOT}"
    w.start   = "#{bin} #{start_options} start"
    w.restart = "#{bin} --pid #{pid_path} restart"
    w.stop    = "#{bin} --pid #{pid_path} stop"

    # Management conditions.
    w.start_if do |start|
      start.condition(:process_running) do |c|
        # Ensure the worker is running.
        c.interval = 5.seconds
        c.running  = false
      end
    end

  end
end

它将启动4个瘦服务器,但是在错误的端口上。这是在调试模式下运行神的输出:

vagrant@localhost:/vagrant$ RAILS_ENV=development god -c config/app.god -D
I [2014-04-30 18:27:05]  INFO: Loading config/app.god
I [2014-04-30 18:27:05]  INFO: Syslog enabled.
I [2014-04-30 18:27:05]  INFO: Using pid file directory: /vagrant/tmp/pids
I [2014-04-30 18:27:06]  INFO: Started on drbunix:///tmp/god.17165.sock
I [2014-04-30 18:27:06]  INFO: thin-1 move 'unmonitored' to 'up'
I [2014-04-30 18:27:06]  INFO: thin-1 moved 'unmonitored' to 'up'
I [2014-04-30 18:27:06]  INFO: thin-3 move 'unmonitored' to 'up'
I [2014-04-30 18:27:06]  INFO: thin-4 move 'unmonitored' to 'up'
I [2014-04-30 18:27:06]  INFO: thin-4 moved 'unmonitored' to 'up'
I [2014-04-30 18:27:06]  INFO: thin-2 move 'unmonitored' to 'up'
I [2014-04-30 18:27:06]  INFO: thin-3 moved 'unmonitored' to 'up'
I [2014-04-30 18:27:06]  INFO: thin-2 moved 'unmonitored' to 'up'
I [2014-04-30 18:27:06]  INFO: thin-1 [trigger] process is not running (ProcessRunning)
I [2014-04-30 18:27:06]  INFO: thin-3 [trigger] process is not running (ProcessRunning)
I [2014-04-30 18:27:06]  INFO: thin-4 [trigger] process is not running (ProcessRunning)
I [2014-04-30 18:27:06]  INFO: thin-1 move 'up' to 'start'
I [2014-04-30 18:27:06]  INFO: thin-3 move 'up' to 'start'
I [2014-04-30 18:27:06]  INFO: thin-4 move 'up' to 'start'
I [2014-04-30 18:27:06]  INFO: thin-4 start: /vagrant/bin/thin -c /vagrant  --address=0.0.0.0 --environment=development --only=4 --port=8304 start
I [2014-04-30 18:27:06]  INFO: thin-2 [trigger] process is not running (ProcessRunning)
I [2014-04-30 18:27:06]  INFO: thin-2 move 'up' to 'start'
I [2014-04-30 18:27:06]  INFO: thin-1 start: /vagrant/bin/thin -c /vagrant  --address=0.0.0.0 --environment=development --only=1 --port=8301 start
I [2014-04-30 18:27:06]  INFO: thin-3 start: /vagrant/bin/thin -c /vagrant  --address=0.0.0.0 --environment=development --only=3 --port=8303 start
I [2014-04-30 18:27:06]  INFO: thin-2 start: /vagrant/bin/thin -c /vagrant  --address=0.0.0.0 --environment=development --only=2 --port=8302 start

但是,不是我的瘦服务器在830 {1,2.3.4}上收听,而是在830 {2,4.6.8}上收听:

vagrant@localhost:/vagrant$ ps aux | grep thin
vagrant   4535 49.8  2.3 113712 83936 ?        Sl   18:27   0:03 thin server (0.0.0.0:8306)                                                                                                                                                                                                                                   
vagrant   4541 51.4  2.3 113464 83716 ?        Sl   18:27   0:03 thin server (0.0.0.0:8304)                                                                                                                                                                                                                                   
vagrant   4547 55.5  2.3 113276 83504 ?        Sl   18:27   0:03 thin server (0.0.0.0:8302)                                                                                                                                                                                                                                   
vagrant   4553 54.2  2.3 113740 83928 ?        Sl   18:27   0:03 thin server (0.0.0.0:8308)                                                                                                                                                                                                                                   
vagrant   4569  0.0  0.0   3912   828 pts/0    S+   18:27   0:00 grep --color=auto thin

我的所有pid文件,日志条目等都显示{2.4.6,8}个数字。考虑到我的代码和来自上帝的调试输出,我认为瘦的结局有些不对劲,但我不知道该寻找什么。

此外,这种模式(碱基+ 2,4,6,8代替碱基+ 1,2,3,4)是可重复的 - 例如如果我将端口基数设置为5555,则结果不是5556,5557,5558和5559,而是5557,5559,5561和5563。

我是否遗漏了精简文档中的内容?是否存在冲突,我需要告诉上帝将start命令错开几秒钟?我可以使用其他诊断工具吗?我在这里陷入困境。


EDIT1

如果尝试&#34;顺其自然&#34;并改变

(1..4).each do |index|

[2,4,6,8].each do |index|
然后,而不是{2,4,6,8},我得到{4,8,12,16},这让我和以前一样困惑。


EDIT2

我现在很确定这与上帝无关,所以我改变了问题标题。以下是新证据:

vagrant@localhost:/vagrant$ /vagrant/bin/thin -c /vagrant  --address=0.0.0.0 --environment=development --only=1 --port=8301 --servers=1 start
Starting server on 0.0.0.0:8302 ... 

为什么精简拒绝在我请求的端口上启动?


EDIT3

将这一点分解为最简单的情况,我找到了罪魁祸首:

vagrant@localhost:/vagrant$ /vagrant/bin/thin -c /vagrant --port=8301 start
Using rack adapter
Thin web server (v1.6.2 codename Doc Brown)
Maximum connections set to 1024
Listening on 0.0.0.0:8301, CTRL+C to stop
^CStopping ...
vagrant@localhost:/vagrant$ /vagrant/bin/thin -c /vagrant --only=1 --port=8301 start
Starting server on 0.0.0.0:8302 ... 
vagrant@localhost:/vagrant$ ^C

因此,由于某种原因,--only指令会导致对端口绑定的错误行为。 现在的问题在于,当我没有指定--only=1时,我无法控制瘦身放置其pid文件的位置。

1 个答案:

答案 0 :(得分:0)

在我尝试过的所有事情之间找到答案。在最终形式中,我有以下内容:

# Configuration for thin servers.
start_options = {
  address:     '0.0.0.0',
  chdir:       RAILS_ROOT,
  environment: RAILS_ENV,
  port:        port,
}.inject("") { |s,(k,v)| s << " --#{k}=#{v}" }

# Process management commands.
bin       = "/vagrant/bin/thin"
# NOTE: pid not specified during start
w.start   = "#{bin} #{start_options} start"
# NOTE: pid specified for restart and stop
w.restart = "#{bin} --pid tmp/pids/thin-#{index}.pid restart"
w.stop    = "#{bin} --pid tmp/pids/thin-#{index}.pid stop"

在省略God.pid_file_directory = 'tmp/pids'的同时设置w.pid_file允许上帝“自然地”放置pid文件(例如,基于像tmp/pids/thin-1.pid这样的进程名称)。另外,在w.start中省略--pid--daemonize--only以防止瘦身进行守护和群集本身非常重要。在这个设置中,上帝正在处理所有这些细节,所以我需要保持薄弱,不要尝试智能本身。

我以前的配置搞砸了这个干净的分离,结果是上帝试图管理瘦身,但瘦也试图管理自己。就像一个咄咄逼人的父母在与一个反叛的少年的尖叫比赛中,每个人试图对这种情况施加的控制越多,整个系统就越扭曲和不稳定。

谜题的最后一部分 - 通过上帝实现全面的过程控制 - 在--pid和{{1}期间必须将start(在restart期间未指定)传递给瘦身因为上帝(正确地)管理守护进程,所以 - thin不知道在没有PID的情况下在哪一点发出信号。