# Best Practice For Background Processes

**URL:** <https://www.ruby-forum.com/t/best-practice-for-background-processes/136332>\
**Category:** Rails\
**Created:** [April 30, 2008, 10:04pm UTC](https://www.ruby-forum.com/t/best-practice-for-background-processes/136332 "2008-04-30T22:04:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [April 30, 2008, 10:04pm UTC](https://www.ruby-forum.com/t/best-practice-for-background-processes/136332/1 "2008-04-30T22:04:06Z")

</div>

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  
job.  
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  
copying  
data from one table to another.

What are the best practices to run these types of periodic tasks?

Thanks before hand for your feedback!

Your friend,

John

–  
John K.  
removed\_email\_address@domain.invalid

Blog: [http://www.kopanas.com](http://www.kopanas.com)  
Conference: [http://www.cusec.net](http://www.cusec.net)  
Twits: [http://www.twitter.com/kopanas](http://www.twitter.com/kopanas)

---

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [April 30, 2008, 10:51pm UTC](https://www.ruby-forum.com/t/best-practice-for-background-processes/136332/2 "2008-04-30T22:51:35Z")

</div>

Here is what I do:

I have a file lib/offline/model\_loader.rb that looks like this:

module ModelLoader

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** ) +  
model\_path)  
require “yaml”  
require “rubygems”  
gem “activerecord”  
require File.dirname( **FILE** ) + ‘/…/…/config/environment’  
conf = YAML::load(File.open(File.dirname( **FILE** ) +  
database\_config))  
ActiveRecord::Base.establish\_connection(conf[environment])  
end

end

Then in whatever script you wanna run say script/my\_site\_name/  
some\_script\_for\_cron.rb

#!/usr/bin/env ruby

rootd = File.dirname( **FILE** ) + ‘/…/…/’  
require rootd + ‘lib/offline/model\_loader’  
include ModelLoader  
ModelLoader::load\_model(nil, nil, ‘development’)

# now you have access to all your models

MyModel.find(:all).each do |instance|  
…  
…  
end

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  
you need.

Hope that helps, good luck!  
Tim

---

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [May 1, 2008, 1:36am UTC](https://www.ruby-forum.com/t/best-practice-for-background-processes/136332/3 "2008-05-01T01:36:56Z")

</div>

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  
of:

15 2 \* \* \* /usr/local/bin/rake -f /var/www/site/Rakefile do:something  
RAILS\_ENV=production

and in lib/tasks/something.rake:

namespace :do do  
desc “does something”  
task :something =\> :environment do  
`
end
end`
