Automatically starts and stops the daemons in a Rails application.
Add the following entry to your Gemfile:
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.
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.
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
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
Fork the project.
Make your feature addition or bug fix.
Add tests for it. This is important so I don't break it in a future version unintentionally.
Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
Send me a pull request. Bonus points for topic branches.
Copyright © 2008-2012 Victor Costan, released under the MIT license