سؤال

I have two ruby server scripts, powerupserver.rb and outputserver.rb, of the form:

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

In development I use Foreman to run them and that works great. Now I am trying to run and monitor them in the background as daemons with Bluepill. I chose Bluepill mostly because Foreman has an option to export to a Bluepill config file (.pill file). So I did that and then altered the .pill file as needed to get the following:

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

When I load this .pill, and check the status (sudo bluepill status), I see:

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

So it is supposedly up (albeit with pid's of 0? which certainly doesn't seem good), but I can see that they are not running/doing what they're supposed to do. Could someone with Bluepill knowledge help me figure out what I'm doing wrong here? Thank you so so much in advance!

هل كانت مفيدة؟

المحلول

I ended up using the Daemons ruby gem to daemonize my scripts and using Bluepill to monitor them. I know you can do both using only Bluepill, but I couldn't figure it out at the time and my system has been working just fine. I am using Rails 3.0.3, Daemons 1.1.5 and Bluepill 0.0.51. So first things first, make sure you have Daemons and Bluepill installed.

So let's say we have myserver.rb, a ruby server script living inside my rails app root. In order to daemonize it with Daemons, create myserver_control.rb within the root folder to tell Daemons how to daemonize:

# 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)

Check out the Daemon documentation to learn more about the options hash. You can now run your daemon from inside your rails app root folder at the command line using 'sudo ruby myserver_control.rb start'. This is the daemon start up command that you can put in your Bluepill config file (myrailsapp.pill file) like so:

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

Definitely go read up on the Bluepill documentation to see all of your many options. Then when you start up Bluepill you have a monitored daemon. Make sure all of the folders referenced in the examples above exist (e.g. pids).

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top