Manage cron from rails

I needed to setup some caching and database updating that will run once a day on our ruby on rails application.

I found this link, which gave three different approaches - all of which I didn't like.

A quick and dirty yet tried and true method is to use cron to call wget on a controller script/runner (thanks David) that does everything we need. All I needed to do was write the crontab entries - but that means later deployments will require someone to do that. My approach was to use environment.rb to programmatically edit the crontab. I just added the following to the end of environment.rb:

current_cron = `crontab -l`
rails_cron_jobs =< # m h dom mon dow command
0 18 * * mon-fri /var/www/bart/script/runner -e production 'Report.cache'
0 22 * * mon-fri /var/www/bart/script/runner -e production 'Patient.update_defaulters'
EOF
unless current_cron.match(/Report\.cache/)
puts "Adding #{rails_cron_jobs} to current cron: #{current_cron}"
puts `echo "#{current_cron}\n#{rails_cron_jobs}" | crontab -`
end


It is a total hack, but I like it. We read in the current crontab - see if we have already edited it - if not we edit it. Plain and simple stuff. Now, as we say in Malawi - somebody school me as to why this is evil bad and wrong.

0 Response to Manage cron from rails

Post a Comment