Easy Ruby cronjobs with daemon-kit 14

Posted 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.

Trackbacks

Use this link to trackback from your own site.

Comments

Leave a response

  1. Chris Tue, 28 Apr 2009 11:07:15 UTC

    How do you start it from rails?

  2. Chris Tue, 28 Apr 2009 11:09:13 UTC

    I have done exactly as you says and when running “daemon_kit mycrond -i cron” I get the error :

    `gem_original_require’: no such file to load — rubigen (LoadError)

  3. Kenneth Kalmer Tue, 28 Apr 2009 12:02:21 UTC

    @Chris thanks for spotting. Until now I’m sure everyone had newgem installed. I’ve just updated the dependency in the gemspec to add rubigen, which is used by the generators.

    As for Rails, I’ll have to build a daemon-kit plugin for rails at a later stage. daemon-kit is used to create isolated daemon processes.

  4. Twitted by peterdierx Tue, 28 Apr 2009 14:46:13 UTC

    [...] This post was Twitted by peterdierx [...]

  5. Slow Joe Tue, 28 Apr 2009 15:06:01 UTC

    “ERROR: could not find gem kennethkalmer-daemon-kit locally or in a repository”

  6. Kenneth Kalmer Tue, 28 Apr 2009 16:57:53 UTC

    @Joe you need to add github as a source for your gems -> sudo gem sources -a http://gems.github.com/

  7. Slow Joe Tue, 28 Apr 2009 17:36:19 UTC

    @Kenneth thanks.

  8. Twitted by JonathanNelson Thu, 30 Apr 2009 19:54:31 UTC

    [...] This post was Twitted by JonathanNelson – Real-url.org [...]

  9. rufus-scheduler 2.0, with em flavour « processi Thu, 07 May 2009 04:27:52 UTC

    [...] nice stress tests (for the 1.0 and the 2.0 branches), thanks as well to Kenneth Kalmer for its daemon-kit + rufus-scheduler [...]

  10. [...] Easy Ruby cronjobs with daemon-kit | Open Sourcery [...]

  11. Gary S. Weaver Wed, 12 Aug 2009 19:06:57 UTC

    Here is what I did to install:

    sudo gem install rubigen
    sudo gem install eventmachine
    sudo gem install kennethkalmer-daemon-kit –source http://gems.github.com
    sudo gem install rufus-scheduler
    daemon_kit mycrond -i cron

    (actually I used jruby -S gem install …, but same thing.)

    Unfortunately, I needed something to run in the background for Rails. I could set it up in cron, but would like it self-contained within the Rails app. Any suggestions?

  12. Gary S. Weaver Wed, 12 Aug 2009 19:22:39 UTC

    (answering my own question)
    Some coworkers pointed me to this for Rails cron:
    http://www.workingwithrails.com/railsplugin/4962-rails-cron

    Thanks!

  13. Gary S. Weaver Wed, 12 Aug 2009 19:57:38 UTC

    Nevermind. Looks like Railscron is old and buggy. Please post if you have any ideas for scheduling jobs in Rails without requiring cron to be on the system (as Whenever does). Thanks!

  14. Kenneth Kalmer Wed, 12 Aug 2009 22:57:45 UTC

    @Gary

    Daemon-kit hasn’t really been tested with Rails, but I know some people are in fact using it like that. The last message in a recent thread [1] on the daemon-kit group might be exactly what you are looking for. Somehow I want to agree with you on not using whenever, I’ve always had issues with cron as a non-root user.

    1 – http://groups.google.com/group/daemon-kit/browse_thread/thread/8afbc64b5a7ba256

Comments

  • Tags

    activerecord air amqp analytics audits bash bind capistrano cheat convert couchdb daemon-kit dlz dns elsewhere gentoo gist git hoptoad linux macros mercurial messaging mysql nginx olympics plugins postfix postini powerdns presentations projects quickies rails rake review ruby ruby19 ruote security shoes sitemap ssl svn webby
  • Recent Posts

  • Archives

  • Alltop. Seriously?! I got in?