Scheduling tasks is something we all need to know to do, for it’s quite common in applications. Fetching feeds, indexing some data, processing files at a periodical time, that happens a lot. You are probably quite familiar then with the linux cron, if you had to deal with scheduling stuff in the past, but there is something you may not. Let me introduce you the Whenever(Whenever) gem. What is it? A simple gem to schedule tasks writing them in nice ruby syntax…just let the gem work it’s magic and deal with the cron.
In order to install it, you have to add first the github source, only if you never done it :
To get started, just place yourself in the app path and type
This will create an initial config/schedule.rb for you.
There you can nicely write tasks to run.
Some examples are:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Another nice thing to do is integrate it with Capistrano.
1 2 3 4 5 6 7 8
The official documentation provides this way to integrate it, but we found some little details, and the solution is here for you.
If you integrate it by the regular way, when making multiple deploys, it will configure several tasks to run in your cron file. Why? Because the path of the current application changed, and the gem uses the path (by adding a comment line) when updating the cron. What should we do then? Simple, change the run line with this:
What have we done? We used the -i option, which makes an update, but passing the comment we want, and make it not to change in every deploy.
Feel free to try it and leave your comments !!