Attention!

It has come to my attention that this plugin is no longer available from it’s original source. I have made it available on Github.

In place of steps 2 and 3 below you can download the tarball and unpack it to your vendor/plugins directory.

Let me know in the comments how this goes, thanks.

Back to the action

Sometimes you need some precess to run at set time intervals, independent of the request-response cycle a standard web server. Things like (as I did) polling Flickr to see if you’ve added any new images to your account, or looking at a to-do list to see if any email reminders are due to be sent out.

Cron jobs under Unix are a great answer to this , but you have the problem of these being:

  1. Particular to Unix and Linux systems.
  2. How do you access your active-record models that make up your Rails app?

Enter Kyle Maxwell’s Daemon_generator plugin, a simple way to create and control daemons that run your own code within the Rails Environment.

It took me a while to figure out how to get it all set up, so I decided to write a guide to help people get to grips with it.

OK, here we go…

  1. Install the Daemons Gem on your system using the command:
    sudo gem install daemons
  2. Navigate to the root of your rails app, install the daemons_generator plugin in your app using the command:
    ruby script/plugin install http://svn.kylemaxwell.com/rails_plugins/daemon_generator/trunk
    See the the top of this article for the new download location.
  3. Unfortunately this will be installed in your vendor/plugins folder as trunk, so change the name of this to daemon_generator so it’s easy to see at a glance what it is:
    mv vendor/plugins/trunk vendor/plugins/daemon_generator
  4. Now you can create your first daemon. I’m going to call mine test:
    ruby script/generate daemon test
    That should create a bunch of files, including lib/daemons/test.rb, which will hold your actual code. If you have a look at this, it should be fairly self-explanatory where you put your own code and how you set the time interval. Leave this unchanged for the moment, just to see it in action.
  5. Start the daemons with the command:
    ruby script/daemons start
  6. Look at the production log, you should see output similar to the following:
    dougal@tempy:~/test123$ tail -f log/production.log 
    This daemon is still running at Thu Nov 09 00:33:54 GMT 2006.
    This daemon is still running at Thu Nov 09 00:34:04 GMT 2006.
    This daemon is still running at Thu Nov 09 00:34:14 GMT 2006.
    It might seem strange to be looking at the production log, daemons run in production environment by default as they can be run separately from your web server (The we server doesn’t even have to be running to get this to work). You can of course change the environment at the top of lib/daemons/test.rb should you wish so for testing purposes.
  7. Stop your daemon with the command:
    ruby script/daemons stop</pre>

Having Trouble?

This plugin is no longer maintained by the original author. However, commenters have been kind enough to suggest fixes to problems. See Rob Lucas’s comment in particular. I will of course accept patches and fixes, and apply these to the repository.

Wrapup

That’s it, you created, run and halted your first daemon! More commands for controlling daemons can be found in the readme, and the complete source in Kyle’s SVN repository.

Hope that helped some people out. If you have any questions or problems leave a comment and I’ll hopefully see you good.

Screencast

You can now see Daemon Generator in moving pictures, courtesy of Ryan Bates’ Railscasts.