2011-12-07 90 views
0

我有两个红宝石服务器脚本,powerupserver.rb和outputserver.rb,形式为:如何使用Bluepill启动并运行ruby服务器脚本?

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 

在发展中,我使用工头来运行他们,伟大工程。现在我试图在后台运行并监视它们作为守护进程与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,并检查其状态(须藤bluepill状态),我看到:

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

所以它被认为是(尽管pid为0?这看起来不太好),但我可以看到他们没有运行/做他们应该做的事情。具有Bluepill知识的人能帮我弄清楚我在这里做错了什么吗?提前谢谢你!

+0

我们使用bluepill。潜在问题:流程是否自动守护进程?启动起来很慢吗?例如,我们必须给独角兽(+导轨)整整30秒才能启动,否则Bluepill永远不会获得pid。 – d11wtq

回答

0

我最终使用守护程序ruby gem来守护我的脚本并使用Bluepill来监视它们。我知道你可以只使用Bluepill,但当时我无法弄清楚,我的系统一直工作得很好。我正在使用Rails 3.0.3,守护进程1.1.5和Bluepill 0.0.51。所以首先要确保你已经安装了Daemons和Bluepill。

所以我们假设我们有myserver.rb,一个生活在我的Rails应用程序根目录下的ruby服务器脚本。为了与后台程序的守护进程它的根文件夹中创建myserver_control.rb告诉守护进程如何守护进程:

# 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应用程序根文件夹中运行守护进程。这是守护进程,像这样的启动命令,你可以把你的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时,您将拥有一个受监控的守护进程。确保以上示例中引用的所有文件夹都存在(例如,pid)。

相关问题