Daemon-kit has been getting a lot of TLC from me lately, and it’s not going to stop anytime soon. As I wander deeper and deeper into AMQP territory, I need to extend daemon-kit to cope with all kinds of new scenarios. One of those being unhandled exceptions.
The second thing I put on the TODO list was Rails-style exception handling. With version 0.1.6 there has been some progress made in that regard. Daemon-kit now sports a configurable safety net for dangerous code. By wrapping blocks of code in a “safety net”, unhandled exceptions are caught and logged, and optionally sent via email or to Hoptoad for review.
Hoptoad? In a Ruby daemon? Sure, inspiration came via these tweets.
We added hoptoad to our custom ruby daemon... Already finding lots of errors, hoptoad is great for visibility into a daemon.
— Josh Owens (@joshowens) May 7, 2009
Integrated Hoptoad into a regular ruby daemon process.
— Andy Shen (@shenie) May 7, 2009
And it only makes sense to do it. Now for some code:
safely do
# do something silly
silly.action!
end
safely is mixed into Object and can be used freely. It is important to note that you have to handle your daemon-specific applications on your own and rely on safely as a fall over mechanism.
To configure the safety net, you can edit your config/environment.rb file and add the following lines to the configure block:
# for email notifications
config.safety_net.handler = :mail
config.safety_net.mail.recipients = ['[email protected]']
# for hoptoad
config.safety_net.handler = :hoptoad
config.safety_net.hoptoad.api_key = 'your-hoptoad-key'
The documentation is very rough at the moment, but the files you want to explore are lib/daemon_kit/safety.rb and the error handlers in lib/daemon_kit/error_handlers.
NOTE: If you are upgrading from an earlier daemon-kit, please upgrade your daemons as well by running the following rake task in the root of your daemon projects:
$ rake daemon_kit:upgrade
In the coming days/weeks you can look forward to the following enhancements as well:
- Improved logging
- Improved backtrace cleanups
- Improved rdoc’s
- rack application generator (with rack-mount)
I’m patching things up as I go along, adding features as I need them (and stuff I recall from my first daemons). There is still a lot of things that need attention, but they’ll be addressed and hopefully daemon-kit grows to becoming the premier framework for writing daemon processes in our beloved Ruby.