Safety nets for your Ruby daemons
by Kenneth Kalmer on May 13, 2009
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.
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 = ['you@gmail.com'] # 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.
Quick *nix shadow passwords with Ruby
by Kenneth Kalmer on May 1, 2009
Just thought I’d share this one to boost the available online information. Using String#crypt and man crypt you’ll come up with something similar to the gist below (extract from a project I’m busy working on).
module Linux class User class << self # Generate an MD5 salt string def salt seeds = ('a'..'z').to_a seeds.concat( ('A'..'Z').to_a ) seeds.concat( (0..9).to_a ) seeds.concat ['/', '.'] seeds.compact! salt_string = '$1$' 8.times { salt_string << seeds[ rand(seeds.size) ].to_s } salt_string end # Crypt a password suitable for use in shadow files def crypt( string ) string.crypt( self.salt ) end end end end
And the spec
require File.dirname(__FILE__) + '/../spec_helper' describe Linux::User do describe "generating shadow passwords" do it "should generate a salt for crypt" do salt = Linux::User.salt salt.length.should be(11) salt.should match(/^\$1\$[a-zA-Z0-9\.\/]{8}$/) end it "should generate a shadow password" do pass = Linux::User.crypt( 'secret' ) pass.should match(/^\$1\$[a-zA-Z0-9\.\/]{8}\$[a-zA-Z0-9\.\/]{22}$/) pass.length.should be(34) end end end
HTH
Nanite agents now in daemon-kit
by Kenneth Kalmer on April 30, 2009
My recent love affair with AMQP & nanite has been quite a nice boost to daemon-kit. First it got a neat AMQP consumer generator, and today it got a nanite agent generator with sample actor.
Here is how to get going quickly:
$ sudo gem install kennethkalmer-daemon-kit -s http://gems.github.com $ daemon_kit myagent -i nanite_agent $ cd myagent $ ruby ./bin/myagent run
And in another console:
$ nanite-mapper -i
>> request('/sample/echo', 'ping') { |r| puts r.inspect }
You should get a ping message back from the sample actor.
The daemon-kit nanite agent implementation is more than just a replacement for the nanite-agent command line tool. It allows you to build more functionality into your agents as well. In my case I’m combining the cron features of daemon-kit with the nanite agents to build a multi-purpose power daemon.
Fine print
This generator is still in it’s infancy and will undoubtedly have some changes moving forward, but so far it works just great in my simple tests. I’m publishing this to solicit some feedback from the nanite community in general.
Please follow the nanite README closely to ensure your nanite installation is working correctly. Ezra merged my changes to nanite, so the examples are working out the box again.
Disclaimer: I’m still learning nanite and AMQP, so I’m definitely no authority on either and asking me for help might yield longer than expected replies while I figure stuff out myself!
Easy Ruby cronjobs with daemon-kit
by Kenneth Kalmer on April 28, 2009
I started conceptualizing and playing with a pet project called daemon-kit earlier this year, with the aim to ultimately be the preferred way of assembling daemon processes written in Ruby.
Today I took the opportunity to add two more generators to daemon-kit, as well as fix some small annoying issues. The first generator is a ‘cron’ generator, which I’ll cover in this article. The second is an AMQP consumer, that my day job requires.
Running cron-style daemon processes seems to be a common need in the Ruby world, and my first ever daemon process was a cron-style implementation using the remarkable rufus-scheduler gem by John Mettraux. The second, an SQS client. Writing Ruby daemon processes is quick and simple, but getting to know the ins and outs of these hidden beasts can be quit a nightmare.
As of late I’ve been threatening in #ruote that daemon-kit will sport a ‘cron’ style generator when I get the time. Today I made time, and you can now get a simple cron daemon up and running in minutes, heres how:
1. Get daemon-kit & co
$ sudo gem install kennethkalmer-daemon-kit $ sudo gem install rufus-scheduler
rufus-scheduler is not a direct dependency of daemon-kit, but required by the daemons generated using the cron generator.
2. Generate a stub daemon
$ daemon_kit mycrond -i cron
This creates a project layout in a directory named ‘mycrond‘. You can populate the lib folder with your custom code. What matters though is that your generated ‘cron’ daemon lives in libexec/mycrond.rb.
3. Profit
Open up libexec/mycrond.rb to reveal a fully functional cron-style daemon, complete with sample 1 minute task.
Behind the scenes
All of daemon-kit is basically two things: abstracting daemonizing routines and environment configurations, and wrapping supporting libraries in thin wrapper classes for easing their use inside daemon processes. The cron wrapper class is extremely thin, in part due to the excellent implementation of the rufus-scheduler gem.
As with all projects, I cannot imagine every possible use for the gem and rely on feedback from the community. If you are going to attempt using ActiveRecord inside the cron daemon, beware that you might have to juggle some balls with ActiveRecord and threads. Please report these issues on the github tracker and I’ll attempt to find solutions for you. I highly recommend using ActiveRecord 2.3.2 or later to benefit from the connection pooling and thread safety improvements.

