/Daemonator

Utility code to make use of the daemons gem with RubyOnRails

Primary LanguageRuby

Project: Daemonator

This is a simple utility for using the daemons gem with RubyOnRails(version > 3).

Its roughly 90 lines of code, and just provides the necessary wrapping and the leaves the meat of your daemon to you!  By 'necessary wrapping' I'm referring to loading of the rails env inside the forked process, handling connections to the AR database and to the log file which are lost even after you reinclude the Rails env inside the forked process


Requirements: 

  daemons gem(http://daemons.rubyforge.org/)
  application.rb must have the following line to prevent daemons from being required in production mode:

      config.eager_load_paths -= %W(#{Brandid::Application.config.root}/app/daemons)


Usage:

  in your daemon file(i put all my domain specific daemons in app/daemons, not in lib!):
  
    #!/usr/bin/env ruby
    require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'lib', 'daemonator'))

    Daemonator.new(<app name>, <interval>, <options>).daemonize! do
      Rails.logger.debug "daemons running, woo!"
      #do your thing!
    end 
    
    
Arguments:

  app name - this identifies your daemon, as well, this will be the name of the pid and the log files
  interval - this is the sleep interval of your daemon
  opts     - the options hash and is passed directly to the daemons gem
  
  You can of course fully customize your daemon, as much as the daemons gem supports like so:
    Daemonator.new("notifier", {:log_dir => "log", :monitor => true})
  
  voila!