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.
$ rails blog
$ ./script/generate scaffold Post title:string body:text expires_at:datetime
$ rake db:migrate
$ ./script/server
Open up http://localhost:3000/posts
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.
$ script/plugin install git://github.com/contrast/exceptional.git
Edit the config/exceptional.yml file
PASTE_YOUR_API_KEY_HERE with your exceptional API_Key
(available from the ‘installation instructions’ screen within Exceptional)
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)
The Daemons gem is a great gem for wrapping ruby code as a daemon process.
$ sudo gem install daemons
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
$ 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
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
$ RAILS_ENV=development ./lib/daemons/expire_posts_ctl start
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 < ....
$ 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
Go to http://localhost:3000/posts and create and test some expiring posts!
We always love to hear your comments, feedback and ideas. Please either leave a comment below, or let us know at feedback@getexceptional.com
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.
Excellent stuff guys... Are daemons are not exceptional, but they soon will be :-) !
Exceptional is the first and best error tracking service for Ruby on Rails applications.
“Exceptional has vastly improved our ability to prioritize and track down bugs on our site. Completely worth the money and highly recommended.” — Andrew Grim, Kongregate