Skip to content

light-daemon is a ruby gem to help developers quickly build a daemon process which prefork worker processes and monitor them.

Notifications You must be signed in to change notification settings

yzhanginwa/light-daemon

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 

Repository files navigation

Light Daemon

Light-daemon is a ruby gem. It could help developers quickly build a daemon which preforks worker processes and monitors them.

Install

gem install light-daemon

Usage

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:

  1. Create your worker class. Define a method “call” and put the real work into it.

  2. The method “call” needs to return true for the daemon to continuously call it.

  3. 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.

  4. 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" )
    

License

Copyright © 2012 [Yi Zhang], released under the MIT license

About

light-daemon is a ruby gem to help developers quickly build a daemon process which prefork worker processes and monitor them.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages