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.
14 comments
How do you start it from rails?
by Chris on April 28, 2009 at 11:07 am. #
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)
by Chris on April 28, 2009 at 11:09 am. #
@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.
by Kenneth Kalmer on April 28, 2009 at 12:02 pm. #
[...] This post was Twitted by peterdierx [...]
by Twitted by peterdierx on April 28, 2009 at 2:46 pm. #
“ERROR: could not find gem kennethkalmer-daemon-kit locally or in a repository”
by Slow Joe on April 28, 2009 at 3:06 pm. #
@Joe you need to add github as a source for your gems -> sudo gem sources -a http://gems.github.com/
by Kenneth Kalmer on April 28, 2009 at 4:57 pm. #
@Kenneth thanks.
by Slow Joe on April 28, 2009 at 5:36 pm. #
[...] This post was Twitted by JonathanNelson – Real-url.org [...]
by Twitted by JonathanNelson on April 30, 2009 at 7:54 pm. #
[...] nice stress tests (for the 1.0 and the 2.0 branches), thanks as well to Kenneth Kalmer for its daemon-kit + rufus-scheduler [...]
by rufus-scheduler 2.0, with em flavour « processi on May 7, 2009 at 4:27 am. #
[...] Easy Ruby cronjobs with daemon-kit | Open Sourcery [...]
by Ennuyer.net » Blog Archive » I am way behind on my rails link blogging. Link dump and reboot. on May 9, 2009 at 12:42 pm. #
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?
by Gary S. Weaver on August 12, 2009 at 7:06 pm. #
(answering my own question)
Some coworkers pointed me to this for Rails cron:
http://www.workingwithrails.com/railsplugin/4962-rails-cron
Thanks!
by Gary S. Weaver on August 12, 2009 at 7:22 pm. #
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!
by Gary S. Weaver on August 12, 2009 at 7:57 pm. #
@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
by Kenneth Kalmer on August 12, 2009 at 10:57 pm. #