Light-daemon is a ruby gem. It could help developers quickly build a daemon which preforks worker processes and monitors them.
gem install light-daemon
The usage is very simple:
require 'rubygems' require 'light_daemon' class Client def initialize end def call `echo "process: #{Process.pid}" >> /tmp/light-daemon.txt` sleep 3 true end end LightDaemon::Daemon.start(Client.new, :children=> 2, :pid_file => "/tmp/light-daemon.pid")
What you need to do is:
-
Create your worker class. Define a method “call” and put the real work into it.
-
The method “call” needs to return true for the daemon to continuously call it.
-
You need to tell the daemon how many worker processes you want and a filename for daemon to store the PID of the daemon process.
-
If the method “call” returns false, the daemon will kill this worker process and create a new one. This would be very helpful if your code might have memory leaking and you want it to restart after certain criteria is met. An example is as following:
require 'rubygems' require 'light_daemon' class Client def initialize @count = 0 end def call `echo "process: #{Process.pid}" >> /tmp/light-daemon.txt` sleep 3 @count +=1 (@count < 100)? true : false end end LightDaemon::Daemon.start(Client.new, :children=> 2, :pid_file => "/tmp/light-daemon.pid" )
Copyright © 2012 [Yi Zhang], released under the MIT license