Daemonz

Automatically starts and stops the daemons in a Rails application.

Installation

Add the following entry to your Gemfile:

gem 'daemonz'

Get the gem wired and generate the default configuration:

bundle install
rails g daemonz:config

You don’t need to do anything in your code for daemonz to work. It will start the daemons when your server starts, and stop them when your server exits. It does work with multiple servers.

Configuration

The main configuration file is config/daemonz.yml. Daemons are configured by individual files in the config/daemonz directory. Configuration files are ran through Erb, so you can go crazy.

Daemonz comes with a few configuration examples that can also be used as they are, by removing the line containing “:disabled: true”. Please contribute your configuration if you think others could use it.

Daemon Generator

Daemonz includes a generator for a daemon intended to do background processing inside a Rails environment. If you’re writing your first daemon, give it a try. The scaffolded code includes the configuration file, a daemon skeleton using the simple-daemon gem, and an integration test skeleton.

rails g daemon YourDaemonName

Testing

You can test your daemonz configuration with rake, as shown below. Keep in mind that the Rake tasks are only provided for testing, and you should not use them while your Rails application is running. Nothing bad should happen if you run the tasks by mistake, but things may go astray if you mix the Rake tasks with application starts and stops.

rake daemons:start # Starts your daemons (for testing only)
rake daemons:stop  # Stops your daemons (for testing only)

Using the default configuration, daemons are not running during tests. The snippet below shows how you can have daemons running during a specific test.

class DaemonsTest < ActionController::IntegrationTest
  # If this isn't here, Rails runs the entire test in a transaction, so
  # the database changes performed by daemons won't be visible to the test
  # code.
  #
  # Keep in mind that SQLite can't do multi-process access, so you'll need
  # to upgrade to a database server, such as MySQL or PostgreSQL if you're
  # accessing the database from your daemons.
  self.use_transactional_fixtures = false

  test "something needing daemons" do
    Daemonz.with_daemons do
      # daemons will be alive while this code is executed
    end
  end
end

Note on Patches/Pull Requests

Copyright © 2008-2012 Victor Costan, released under the MIT license