Project

asmodai

0.0
No commit activity in last 3 years
No release in over 3 years
A simple daemon generator
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

>= 1.0.9
>= 0.5.0
>= 0.14.6
 Project Readme

Asmodai

Description

Asmodai is a simple-to-use generator for daemon tasks written in Ruby. It makes creating a daemon as straight forward as creating a Rails-application and saves you the hassle of boring and repetitive tasks like creating start- and stop-scripts, pid-file logic or implementing logging.

Installation

$ gem install asmodai

Usage

$ asmodai new foobar
$ cd foobar

Here you will find a dummy implementation:

class Foobar < Asmodai::Daemon
  attr_accessor :running
  
  def on_signal(sig)
    # perform cleanup or stop an eventloop
    self.running=false
  end
  
  def run
    self.running=true
    while running
      logger.info { "I'm still running" }
      sleep 1
    end
  end
end

You can develop your daemon by executing

$ asmodai foreground

This executes the daemon in the foreground and outputs all logging to standard output. It can be terminated with Ctrl-C.

To start your daemon in the background run

$ asmodai start

This will redirect all the output from the process to log/foobar.log

In order to check the status of the daemon run

$ asmodai status
foobar runs with pid 75952

Stop the daemon

$ asmodai stop

Example: Using Asmodai with EventMachine

$ asmodai new echo
$ cd echo

Edit the Gemfile

source 'http://rubygems.org'

gem 'activesupport', ">= 3.0.3"
gem 'eventmachine'

Edit lib/server.rb

module Echo::Server
  def post_init
    puts "-- someone connected to the echo server!"
  end

  def receive_data data
    send_data ">>>you sent: #{data}"
    close_connection if data =~ /quit/i
  end

  def unbind
    puts "-- someone disconnected from the echo server!"
  end
end

Edit echo.rb

class Echo < Asmodai::Daemon
  require 'server' # lib is automatically added to the $LOAD_PATH

  def on_signal(sig)
    EventMachine::stop_event_loop
  end
  
  def run
    EventMachine::run do 
      EventMachine::start_server "127.0.0.1", 8081, Echo::Server
    end
  end
end

Now, run

$ bundle install
$ asmodai start

And you have your own running Echo server. Wheee!