Small daemon framework for ruby, with logging, error handler and more...
Ruby
Switch branches/tags
Pull request Compare This branch is 102 commits behind DAddYE:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
bin
lib
.gitignore
Gemfile
README.md
Rakefile
forever.gemspec

README.md

Forever

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

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:

  1. I can create a new job quickly
  2. 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      "d.dagostino@lipsiasoft.com"
      from    "exceptions@lipsiasoft.com"
      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:

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

Stop daemon(s):

$ forever 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!