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! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Unless I''m reading it wrong or misunderstanding the question, the README file created by "rails <appname>", recommends the lib directory. On 9/15/07, Sy Ys <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org > 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! > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
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. 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! >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
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. Philip Hallstrom wrote:>> 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 >> > > 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'' > > > > >> 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. >> >> 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! >>> >>> > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
> 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_thingIf 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''> > 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. > > 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! >> > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Excellent reply. Thanks. Matthew Rudy wrote:>> stuff-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---