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
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:
- Particular to Unix and Linux systems.
- How do you access your active-record models that make up your Rails app?
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…
- Install the Daemons Gem on your system using the command:
sudo gem install daemons
- Navigate to the root of your rails app, install the daemons_generator plugin in your app using the command:
See the the top of this article for the new download location.
ruby script/plugin install http://svn.kylemaxwell.com/rails_plugins/daemon_generator/trunk
- Unfortunately this will be installed in your
trunk, so change the name of this to
daemon_generatorso it’s easy to see at a glance what it is:
mv vendor/plugins/trunk vendor/plugins/daemon_generator
- Now you can create your first daemon. I’m going to call mine
That should create a bunch of files, including
ruby script/generate daemon test
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.
- Start the daemons with the command:
ruby script/daemons start
- Look at the production log, you should see output similar to the following:
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
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.
lib/daemons/test.rbshould you wish so for testing purposes.
- Stop your daemon with the command:
ruby script/daemons stop</pre>
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.
Hope that helped some people out. If you have any questions or problems leave a comment and I’ll hopefully see you good.
You can now see Daemon Generator in moving pictures, courtesy of Ryan Bates’ Railscasts.