Ruby on rails 如何使用Bluepill启动并运行ruby服务器脚本?
我有两个ruby服务器脚本,powerupserver.rb和outputserver.rb,形式如下:Ruby on rails 如何使用Bluepill启动并运行ruby服务器脚本?,ruby-on-rails,ruby,bluepill,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
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 status时,我看到:
$ sudo bluepill status
powerupserver(pid:0): up
outputserver(pid:0): up
所以它应该是上升的,尽管pid为0?这当然看起来不太好,但我可以看出他们并没有做他们应该做的事情。有蓝丸知识的人能帮我找出我做错了什么吗?提前非常感谢你 我最终使用守护进程ruby gem来守护我的脚本,并使用Bluepill来监视它们。我知道你可以用Bluepill同时完成这两项工作,但我当时还没弄明白,我的系统一直工作得很好。我使用的是Rails 3.0.3、守护进程1.1.5和Bluepill 0.0.51。因此,首先,确保安装了守护程序和Bluepill 假设我们有myserver.rb,它是一个ruby服务器脚本,位于我的rails应用程序根目录中。要使用守护进程对其进行守护,请在根文件夹中创建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)
查看守护程序文档以了解有关选项哈希的更多信息。现在,您可以使用“sudoruby 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 我们用蓝丸。潜在问题:进程是否自行后台监控?启动速度慢吗?例如,我们必须给unicorn+rails整整30秒的启动时间,否则Bluepill永远不会得到pid。