Are your Daemons Exceptional?

Many rails applications have daemon processes running for background processing. Exceptions from these daemon processes are just as important as core application exceptions but frequently are not monitored. It is very straight forward to set up Exceptional monitoring of daemon processes.

Below I go through how to create a background daemon using the daemons gem and the excellent daemon_generator Rails plugin.

We will use the famous blog example, to create a blog with expiring posts so that after a set period of time a background daemon process will delete the expired blog posts.

Creating a rails app with a background daemon monitored by Exceptional

1. Create a new rails application

$ rails blog

2. Add some functionality to the new app

$ ./script/generate scaffold Post title:string body:text expires_at:datetime

3. Test the new application to make sure its working

$ rake db:migrate
  $ ./script/server

Open up http://localhost:3000/posts

4. Create a new application on getexceptional.com

If you do not already have an exceptional account, then you can sign up for a free one, if you like.

Otherwise, create new app within your Exceptional account and give it a name (e.g ‘My Exceptional Daemon Blog’). You of course can re-use an existing test application if you wish.

5. Install The Exceptional Plugin

$ script/plugin install git://github.com/contrast/exceptional.git

6. Configure the Exceptional Plugin with the Exceptional API Key

Edit the config/exceptional.yml file

  1. Overwrite PASTE_YOUR_API_KEY_HERE with your exceptional API_Key (available from the ‘installation instructions’ screen within Exceptional)
  2. By default Exceptional is disabled for the development rails environment, so change enabled: true within the development scope (we will need this later to run and test the application as we will run everything under the development rails environment)

7. Install the daemons gem

The Daemons gem is a great gem for wrapping ruby code as a daemon process.

$ sudo gem install daemons

8. Install the daemon_generator rails plugin

The Daemon Generator rails plug-in provides a generator and some nice templates for managing the life-cycle of a ruby daemon process

$ script/plugin install git://github.com/dougal/daemon_generator.git

9. Create/Generate your Blog Posts Expiry Daemon

$ script/generate daemon expire_posts

This will generate some template files for our daemon including

create lib/daemons
  create script/daemons
  create lib/daemons/expire_posts.rb
  create lib/daemons/expire_posts_ctl
  create config/daemons.yml

10. Implement the Expire Posts Daemon Logic

Edit lib/daemons/expire_posts.rb template, we will wrap our daemon logic with Exceptional.rescue so that Exceptional will handle any exceptions thrown during processing

#!/usr/bin/env ruby

  ENV["RAILS_ENV"] ||= "production"

  require File.dirname(__FILE__) + "/../../config/environment"

  $running = true
  Signal.trap("TERM") do
  $running = false
  end

  while($running) do

  Exceptional.rescue do

  @expired_posts = Post.find(:all, :conditions => ['expiires_at < ?', Time.now])
  @expired_posts.each do |post|
  post.destroy
  end
  end

  sleep 60 #A Minute
  end

11. Start up your blog posts expire daemon

$ RAILS_ENV=development ./lib/daemons/expire_posts_ctl start

12. Error!

There was a tiny intentional typo in Step #10, spot it ?. If everything is setup and running correctly, you should now see an exception within your Exceptional Account.

expire_posts.rb# (ActiveRecord::StatementInvalid) "SQLite3::SQLException: no
  such column: expiires_at: SELECT * FROM \"posts\" WHERE (expiires_at < ....

13. Fix up your Daemon, restart it and your done!

$ RAILS_ENV=development ./lib/daemons/expire_posts_ctl stop

Fix up lib/daemons/expire_posts.rb (change expiires_at to expires_at)

$ RAILS_ENV=development ./lib/daemons/expire_posts_ctl start

14. Play

Go to http://localhost:3000/posts and create and test some expiring posts!

15. Let us know how you got on !

We always love to hear your comments, feedback and ideas. Please either leave a comment below, or let us know at feedback@getexceptional.com

2 Responses to "Are your daemons exceptional?"

  1. Pete Roome says:

    I have been using this plugin recently for a project i have been working on. It was incredibly easy to set up and worked perfectly in development, however i have been having some issues since launch, in production and im unable to actually get my jobs running. A bit annoying as i havent been able to resolve the issue.

    I'm going on to try Whenever (http://github.com/javan/whenever/tree/master), to see if i have any better luck. Fingers crossed.

    Thanks for the post though, ill bare this in mind if i ever decide to have a punt at using daemon_generator again.

  2. Ijonas Kisselbach says:

    Excellent stuff guys... Are daemons are not exceptional, but they soon will be :-) !