I have a couple background processes that run from anywhere between 2
seconds and 30 seconds that run periodically (some run every 15 minutes,
some every 4 hrs). To run them I call a webpage using curl in a cron
I have a funny feeling this is not the best thing to do :-).
All the tasks I have manipulate the DB. Either re-weighing users or
data from one table to another.
What are the best practices to run these types of periodic tasks?
Thanks before hand for your feedback!
Here is what I do:
I have a file lib/offline/model_loader.rb that looks like this:
def load_model(model_path=nil, database_config=nil, environment=nil)
model_path ||= “/…/…/app/models/”
database_config ||= “/…/…/config/database.yml”
environment ||= “development”
$LOAD_PATH << File.expand_path(File.dirname(FILE) +
require File.dirname(FILE) + ‘/…/…/config/environment’
conf = YAML::load(File.open(File.dirname(FILE) +
Then in whatever script you wanna run say script/my_site_name/
rootd = File.dirname(FILE) + ‘/…/…/’
require rootd + ‘lib/offline/model_loader’
ModelLoader::load_model(nil, nil, ‘development’)
now you have access to all your models
MyModel.find(:all).each do |instance|
There are other situations that may call for access to controllers/
views, there’s a solution for that as well, but I suspect this is what
Hope that helps, good luck!
this is a perfect place for rake!
i encounter situations like this all the time at work. simply take the
code from your action that you are hitting with curl and wrap them in
a rake task. then set up your cron job to do something along the lines
15 2 * * * /usr/local/bin/rake -f /var/www/site/Rakefile do:something
and in lib/tasks/something.rake:
namespace :do do
desc “does something”
task :something => :environment do