Ruby Forever

Small daemon framework for ruby, with logging, error handler watcher and much more.

The idea of the name and some concepts was taken by forever for node.js written by Charlie Robbins.

Please don't consider this gem as a port because is not.

The executable name it's called foreverb to prevent collisions.

Why?

There are a lot of alternatives, one of the best is resque, so why another daemons framework? In my servers I've several daemons and what I need is:

  • easily watch the process (memory, cpu)
  • easily manage exceptions
  • easily see logs
  • easily start/stop/restart daemon

As like sinatra and padrino I need a thin framework to do these jobs in few seconds. This mean that:

  • I can create a new job quickly
  • I can watch, start, stop it quickly

So, if you have my needs, Forever can be the right choice for you.

Install:

$ gem install forever

Deamon Example:

Place your script under your standard directory, generally on my env is bin or scripts.

In that case is: bin/foo

#!/usr/bin/ruby
require 'rubygems' unless defined?(Gem)
require 'forever'
require 'mail'

Forever.run do
  ##
  # You can set these values:
  #
  # dir  "foo"     # Default: File.expand_path('../../', __FILE__)
  # file "bar"     # Default: __FILE__
  # log  "bar.log" # Default: File.expand_path(dir, '/log/[file_name].log')
  # pid  "bar.pid" # Default: File.expand_path(dir, '/tmp/[file_name].pid')
  #

  on_error do |e|
    Mail.deliver do
      delivery_method :sendmail, :location => `which sendmail`.chomp
      to      "[email protected]"
      from    "[email protected]"
      subject "[Foo Watcher] #{e.message}"
      body    "%s\n  %s" % [e.message, e.backtrace.join("\n  ")]
    end
  end

  on_ready do
    require 'bundler/setup'
    require 'foo'
    Foo.start_loop
  end
end

Assign right permission:

$ chmod +x bin/foo

start the daemon:

$ bin/foo

you should see an output like:

$ bin/foo
=> Process demonized with pid 19538

you can stop it:

$ bin/foo stop
=> Found pid 19538...
=> Killing process 19538...

Monitor your daemon(s):

List daemons:

$ foreverb list
PID     RSS     CPU   CMD
19838   32512   1.6   Forever: bin/githubwatcher

Stop daemon(s):

$ foreverb stop foo
Do you want really stop Forever: bin/foo  with pid 19538? y
Killing process Forever: bin/foo  with pid 19538...

That's all!