rubydaemons

Daemons do not get restarted?


I am trying to run the same script in multiple daemons.

myapp.rb looks like this:

loop do
  sleep 5
  1 / 0 # crash it
end

my myapp_controller.rb:

require 'rubygems'
require 'daemons'

options = {
  :log_output  => true,
  :backtrace   => true,
  :monitor     => true,
  :multiple    => true,
  :log_dir     => '/mnt/log/',
  :hard_exit   => true
}
Daemons.run(File.join(File.dirname(__FILE__), 'myapp.rb'), options)

When I run ruby myapp_controller.rb start several times in a row, it creates that many daemons, as I expect. But, after a while, due to an error in myapp.rb the daemons crash and the monitor restarts just one and not all. So I end up with a single running daemon.

Why? What am I doing wrong?


Solution

  • I was able to reproduce the behavior. It is not anything you are doing wrong; it is the way the daemons gem behaves.

    Going through the code for the daemons gem, turns out the :multiple option doesn't work well with the :monitor option.

    The :monitor option works only when the daemon is run in single mode.

    I have created a bug report on the daemons project page referencing this question as the source.

    More info about the reproduction of the issue:

    Multiple daemon processes are created when :multiple => true. Each process has its own pid file in the format of <scriptname>.rb<number>.pid.

    However, only one monitor process is created (with a single <scriptname>.rb_monitor.pid file.)

    Here are the list of processes started when I start the daemon process 3 times:

    $ ps -fe | grep my_server
      501  1758     1   0 12:25PM ??         0:00.63 my_server.rb  
      501  1759     1   0 12:25PM ??         0:00.43 my_server.rb_monitor  
      501  1764     1   0 12:25PM ??         0:00.54 my_server.rb  
      501  1834     1   0 12:51PM ??         0:00.31 my_server.rb 
    

    The files in the pid/log folder:

    $ ls /tmp/daemons-2013-01-25/
    my_server.rb.log                my_server.rb1.pid               my_server.rb_monitor.pid
    my_server.rb0.pid               my_server.rb2.pid