如何使用Bluepill启动并运行ruby服务器脚本?

时间:2011-12-07 15:00:55

标签: ruby-on-rails ruby bluepill

我有两个ruby服务器脚本,powerupserver.rb和outputserver.rb,格式为:

require '/Library/WebServer/sample_app/config/environment'
# more requires

@server = TCPServer.open(port_number)                          
loop do                                                 
  Thread.start(@server.accept) do |sock| 
    # do stuff
  end
end

在开发中,我使用Foreman来运行它们并且效果很好。现在我试图在后台运行并监视它们作为使用Bluepill的守护进程。我选择Bluepill主要是因为Foreman可以选择导出到Bluepill配置文件(.pill文件)。所以我这样做,然后根据需要更改.pill文件以获得以下内容:

Bluepill.application("sample_app", :foreground => false, :log_file => "/var/bluepill/log/bluepill.log") do |app|

  app.process("powerupserver") do |process|
    process.start_command = "ruby powerupserver.rb"
    process.working_dir = "/Library/WebServer/sample_app"
    process.pid_file = "/var/bluepill/pids/powerupserver-1.pid"
    process.daemonize = true
    process.stdout = process.stderr = "/var/bluepill/log/powerupserver-1.log"

    process.start_grace_time = 3.seconds
    process.stop_grace_time = 5.seconds
    process.restart_grace_time = 8.seconds
    process.stop_signals = [:quit, 30.seconds, :term, 5.seconds, :kill]

    process.checks :cpu_usage, :every => 10.seconds, :below => 5, :times => 3
    process.checks :mem_usage, :every => 10.seconds, :below => 100.megabytes, :times => [3,5]
    process.checks :flapping, :times => 2, :within => 30.seconds, :retry_in => 7.seconds

  end

  # more lines here mimicking above, but for server script 'outputserver.rb'
end

当我加载.pill并检查状态(sudo bluepill状态)时,我看到:

$ sudo bluepill status
powerupserver(pid:0): up
outputserver(pid:0): up

所以它应该是up(尽管pid为0?这当然看起来并不好),但我可以看到他们没有运行/做他们应该做的事情。拥有Bluepill知识的人可以帮我弄清楚我在这里做错了什么吗?非常感谢你这么多!

1 个答案:

答案 0 :(得分:0)

我最终使用Daemons ruby​​ gem来守护我的脚本并使用Bluepill来监控它们。我知道你只能使用Bluepill来做这两件事,但我当时无法理解,而且我的系统工作得很好。我使用的是Rails 3.0.3,Daemons 1.1.5和Bluepill 0.0.51。首先,确保安装了守护进程和Bluepill。

所以假设我们有myserver.rb,一个ruby服务器脚本存在于我的rails app根目录中。要使用守护进程对其进行守护,请在根文件夹中创建myserver_control.rb,以告知Daemons如何进行守护:

# myserver_control.rb
require 'rubygems'
require 'daemons'

@options = {
    :dir_mode => :normal,
    :dir => '/Library/WebServer/myrailsapp/pids',
    :multiple => true,
    :backtrace => true,
    :monitor => false,
    :log_dir => '/Library/WebServer/myrailsapp/log',
    :log_output => true
}

Daemons.run('myserver.rb', @options)

查看守护程序文档以了解有关选项哈希的更多信息。您现在可以使用'sudo ruby​​ myserver_control.rb start'在命令行的rails app根文件夹中运行您的守护程序。这是守护进程启动命令,您可以将其放入Bluepill配置文件(myrailsapp.pill文件)中,如下所示:

Bluepill.application("myrailsapp", :foreground => false, :log_file => "/Library/WebServer/myrailsapp/log/bluepill.log") do |app|

  app.process("myserver") do |process|

    process.start_command = "sudo ruby myserver_control.rb start"
    process.stop_command = "sudo ruby myserver_control.rb stop"
    process.restart_command = "sudo ruby myserver_control.rb restart"

    process.pid_file = "/Library/WebServer/myrailsapp/pids/myserver.rb0.pid"

    process.working_dir = "/Library/WebServer/myrailsapp"

    process.start_grace_time = 5.seconds
    process.stop_grace_time = 5.seconds
    process.restart_grace_time = 8.seconds

  end
end

绝对可以阅读Bluepill文档,了解您的所有选项。然后当你启动Bluepill时,你有一个受监控的守护进程。确保上面示例中引用的所有文件夹都存在(例如pids)。