#164 Cron in Ruby (revised)
Cron is a common solution for recurring jobs, but it has a confusing syntax. In this episode I show you how to use Whenever to create cron jobs using Ruby. Some alternative scheduling solutions are also mentioned.
- Download:
- source code
- mp4
- m4v
- webm
- ogv
Just a note that if you're using Heroku for hosting your production app, they recently released the Heroku Scheduler (heroku blog) which they recommend using instead of Cron. It's much easier to use but I believe only lets you get down to every 10 minutes.
does this works well along a mulit user rvm installation? will it correctly load my rails environment based on the .rvmrc file?
Anyone know of a way to have certain cron tasks only happen on certain roles in Capistrano? Such as clearing cache files on every app server, but only running a report generator on one utility server. So far I haven't figured out a nice way to get whenever to support conditional jobs.
@Radha - I use it with RVM in my cap recipe and it works fine. You have to ensure to trust all .rvmrc though via the flag or cron will hang (on the github readme for Whenever).
@Joshua:
Q1 -
namespace :deploy do
desc "write the crontab file"
task :write_crontab, :roles => [:whatever] do
run "cd #{release_path} && sudo whenever --load-file config/anyfile.rb --write-crontab #{application}"
end
end
This should work - each role should have its own whenever file - then use a cap task based on role to deploy each crontab. Credit shared with Gordon Isnor (stackoverflow question #1008530).
Q2 - use this gem - https://github.com/deviantech/capistrano-conditional
Thanks Ryan, this is a really useful Gem.
FYI the ASCIIcast is missing a single quote in the "job_type :script" line of the first code block under the Creating Your Own Jobs section.
The default is to run all tasks in "production' mode. How would you make it run in the default environment?
if your using rvm, you can't just:
you need to state the gemset too, thus having an accepted .rvmrc that states the gemset is important.
seems to work also. if you don't then you get log messages that are along the lines of: "cannot load such file -- bundler/setup"
at least that was my experience with using the above version of ruby and rails.
also, the whenever gem would default to production. so if your running whenever in development you want to also state:
Hello everybody,
I need to remove all searches from my database every 10 minutes...
So this would be the path
http://nameofmysite.heroku.com/search/1
How would you write the right code to do it?
Thanks
He gave the example of what this would look like with Capistrano. What if I just want to run it locally to test it out? Also, how do you deploy it to the server if you are not using Capistrano?
yeah I would love to find that out too. So far I have been able to test with a rake task by itself but would love to just test everything locally before I deploy
Just for those who want their program to create the cron
0 9 1 * *
which executes on the first day of each month at 9AM, you can do this.This is the same as
:at => "9am"
but is more humanized.Thanks!
You mention that this is not suitable for tasks that run frequently but give no indication as to why it is not suitable or even what might be suitable.
I have a need to poll for changes to the database every 2 minutes, if changes are found then add add to sidekiq worker jobs for processing.
I have a rake task that looks for the changes and handles adding to the worker queue. I would be interested to know what might be a suitable solution for running a rake task every couple of minutes in this type of frequently polling scenario
Update:
Capistrano 3 has moved out bundler integration into a gem, so
require 'bundler/capistrano'
won't work anymore, you need to have'capistrano/bundler'
in your Gemfile and require it and everything will be fine. (See http://stackoverflow.com/a/19412601/1216245.)