Place for special scripts in the framework?

Hey there,

I have a script which scrapes a website. Since it takes nearly an hour
to complete, making it part of the controller doesn’t make sense because
it would simply time out.

However, this script needs access to my models (it adds to my database).

How can I do this? I am sure there is something simple I am missing.

Thanks!

Sy Ys wrote:

Hey there,

I have a script which scrapes a website. Since it takes nearly an hour
to complete, making it part of the controller doesn’t make sense because
it would simply time out.

However, this script needs access to my models (it adds to my database).

How can I do this? I am sure there is something simple I am missing.

Thanks!

It depends exactly how it’s working.
Often I end up defining a module and sticking it in /lib,
but, equally, any associated model code should live in the model.

Then, in order to run it,
either create a /bin directory
or stick it in /script

inside your script, make sure you require “environment.rb” like this
require File.expand_path(File.dirname(FILE) +
“/…/config/environment”)
then when you run your script (on cron or whatever), it’ll first load up
the rails stack, giving you access to the db, models, and all that.

Excellent reply. Thanks.

Matthew R. wrote:

stuff

If you just want to manually run this, or run it from cron, you can use
/script/runner and pass it the path to a script. It will execute the
script within the rails environment so everything will be available to
you. The script can be really simple if the methods are already defined
in models or libraries. like:

In a model:

class Foo < ActiveRecord::Base
def self.do_long_foo_thing
do_something_long
end
end

Then in a script /tmp/my_script:

Foo.do_long_foo_thing

then /path/to/rails/app/script/runner /tmp/my_script

I created a site_scripts directory under my scripts directory to hold
these, but you can put them wherever. I use them for cleaning sessions
and various other tasks.

However, if this is something that needs to be initiated from a web
page, but you want to background it, check out BackgroundDRB
http://backgroundrb.rubyforge.org/.

Hope this helps.

Unless I’m reading it wrong or misunderstanding the question, the README
file created by "rails ", recommends the lib directory.

Exactly, I just assumed he had more to do than that. I was simply
demonstrating that most of the time the script needs not includes, etc.
This was obviously an example.

  do_something_long

end
end

Then in a script /tmp/my_script:

Foo.do_long_foo_thing

If that’s all you’re doing there’s no reason to make /tmp/my_script.
Just
do like this:

script/runner ‘Foo.do_long_foo_thing’

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs