AARON VAN BOKHOVEN
Moving one of my projects over from delayed_job to resque/redis, for reasons I wont go into here, I needed to have a few of my workers on a cron job. I was initially going to use the resque-scheduler plugin, but the fact that it runs as a daemon made me a little nervous. I didn’t want to worry about watching the scheduler process for memory leaks and or crashes, and cron is a proven, reliable, scheduling service in itself.
Basically, I took the same concept from my other post, Recurring delayed_job with cron and applied it to redis, instead of using mysql as I did with delayed_job. It works by manually injecting jobs into the queue without using the Rails environment, saving memory, cpu, and time, and also able to be run externally by the system cron.
In this example I’m going to use a Payment model, which holds payment information, and an ‘update’ method which should be run nightly at 0000. I’m using Rails 3 as well, which uses the ‘mysql2’ gem.
This is the resque worker.
This is the ruby file that the cron will run. I usually place this in root.rails/lib/crons folder.
And this is what goes in the crontab, which can be accessed by typing ‘crontab -e’ in the console.
Redis - http://redis.io
Resque - https://github.com/defunkt/resque
Resque Intro - https://github.com/blog/542-introducing-resque
Crontab Cheat Sheet(googled this real quick) - http://nanotaboada.wordpress.com/2008/05/25/self-contained-crontab-cheatsheet
Redis Ruby Gem(should get installed when installing resque) - https://github.com/ezmobius/redis-rb