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
On Apr 30, 1:03 pm, "John Kopanas"
<kopa...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> 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 Kopanas
> j...-Iau1QiYlxLpBDgjK7y7TUQ@public.gmane.org
>
> Blog:http://www.kopanas.com
> Conference:http://www.cusec.net
> Twits:http://www.twitter.com/kopanas
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---