Daemonz

Automatically starts and stops the daemons in a Rails application.

Installation

Install with:


script/plugin install git://github.com/costan/daemonz.git

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 :disabled: true line. 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.


script/generate 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 
<pre>
class DaemonsTest < ActionController::IntegrationTest</p>
<ol>
	<li>If this isn’t here, Rails runs the entire test in a transaction, so daemons’</li>
	<li>database changes aren’t visible. Also, if you share the database with</li>
	<li>daemons, sqlite won’t cut it.
  self.use_transactional_fixtures = false</li>
</ol>
test “something needing daemons” do
Daemonz.with_daemons do
<ol>
	<li>daemons will be alive while this code is executed
    end
  end
end

Copyright © 2008 Victor Costan, released under the MIT license