Hello, This may be more of a Ruby question but I''m using Ruby on Rails so I thought I would post it here. Hopefully someone can provide a suggestion on how to approach this. I have a method that uploads a selection of files via FTP. For each file the method gets the file''s associated ftp host/domain, user name and password, connects, uploads the file, updates the status of the file and saves the updated information. I''m now setting up error handling to email the file owner if the response_code is not the expected successful code. If a file has incorrect ftp information associated with it, I want to send an email to the file owner to notify them, shouldn''t be a problem. So, if there is incorrect connection info, Net::FTP will retry connecting until it times out which could be 3 to 5 minutes or more depending on the FTP server''s configuration. I want to keep track of elapsed time and if the connection is not established within 30 seconds, I want to cancel the connection and send an email about the error to the file owner and move on to the next file. So basically I want to make my own timeout, which times out faster than the external FTP server. [code] start_timer_if_elapsed_time>=_30_seconds_cancel_connection #open ftp connection Net::FTP.open(ftp_link, ftp_username, ftp_password) # <-- this line shouldn''t attempt to connect longer than 30 seconds do |ftp| ## send the file to the ftp ## update the status ## save the file end [/code] Any suggestions appreciated -- 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 -~----------~----~----~----~------~----~------~--~---
Hi Brian! I don''t know about Net::FTP, but as a suggestion: what if you spawned a separate thread right before you open the connection and that thread does a: sleep(30) then checks the FTP connection and forces it to close if need be. Of course, if there isn''t a way to check the connection separately, that may not work. The flip-side may make more sense, while I think of it: spawn a thread to do the FTP connection and have your main thread sleep 30 seconds. Then have the main thread check the status and if need be kill the FTP connection or the child thread altogether. Just a thought. :-) -Danimal On Mar 31, 8:14 am, Brian Zzzzzz <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hello, > > This may be more of a Ruby question but I''m using Ruby on Rails so I > thought I would post it here. > > Hopefully someone can provide a suggestion on how to approach this. > > I have a method that uploads a selection of files via FTP. For each file > the method gets the file''s associated ftp host/domain, user name and > password, connects, uploads the file, updates the status of the file and > saves the updated information. > > I''m now setting up error handling to email the file owner if the > response_code is not the expected successful code. > > If a file has incorrect ftp information associated with it, I want to > send an email to the file owner to notify them, shouldn''t be a problem. > > So, if there is incorrect connection info, Net::FTP will retry > connecting until it times out which could be 3 to 5 minutes or more > depending on the FTP server''s configuration. > > I want to keep track of elapsed time and if the connection is not > established within 30 seconds, I want to cancel the connection and send > an email about the error to the file owner and move on to the next file. > > So basically I want to make my own timeout, which times out faster than > the external FTP server. > > [code] > start_timer_if_elapsed_time>=_30_seconds_cancel_connection > #open ftp connection > Net::FTP.open(ftp_link, ftp_username, ftp_password) # <-- this line > shouldn''t attempt to connect longer than 30 seconds > > do |ftp| > > ## send the file to the ftp > ## update the status > ## save the file > end > [/code] > > Any suggestions appreciated > -- > Posted viahttp://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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi Brian, On Tue, 2009-03-31 at 16:14 +0200, Brian Zzzzzz wrote:> I have a method that uploads a selection of files via FTP. For each file > the method gets the file''s associated ftp host/domain, user name and > password, connects, uploads the file, updates the status of the file and > saves the updated information.You should check out DRb, BackgrounDRb, and script/runner for options. You need to handle long-running tasks like this ''outside'' your Rails app via a distributed processing mechanism. Sounds like you may also need access to your Models which these can also provide. HTH, Bill --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On Mar 31, 3:41 pm, Danimal <fightonfightw...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> The flip-side may make more sense, while I think of it: spawn a thread > to do the FTP connection and have your main thread sleep 30 seconds. > Then have the main thread check the status and if need be kill the FTP > connection or the child thread altogether. >which is basically what http://www.ruby-doc.org/stdlib/libdoc/timeout/rdoc/index.html does Fred> Just a thought. :-) > > -Danimal > > On Mar 31, 8:14 am, Brian Zzzzzz <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: > > > Hello, > > > This may be more of a Ruby question but I''m using Ruby on Rails so I > > thought I would post it here. > > > Hopefully someone can provide a suggestion on how to approach this. > > > I have a method that uploads a selection of files via FTP. For each file > > the method gets the file''s associated ftp host/domain, user name and > > password, connects, uploads the file, updates the status of the file and > > saves the updated information. > > > I''m now setting up error handling to email the file owner if the > > response_code is not the expected successful code. > > > If a file has incorrect ftp information associated with it, I want to > > send an email to the file owner to notify them, shouldn''t be a problem. > > > So, if there is incorrect connection info, Net::FTP will retry > > connecting until it times out which could be 3 to 5 minutes or more > > depending on the FTP server''s configuration. > > > I want to keep track of elapsed time and if the connection is not > > established within 30 seconds, I want to cancel the connection and send > > an email about the error to the file owner and move on to the next file. > > > So basically I want to make my own timeout, which times out faster than > > the external FTP server. > > > [code] > > start_timer_if_elapsed_time>=_30_seconds_cancel_connection > > #open ftp connection > > Net::FTP.open(ftp_link, ftp_username, ftp_password) # <-- this line > > shouldn''t attempt to connect longer than 30 seconds > > > do |ftp| > > > ## send the file to the ftp > > ## update the status > > ## save the file > > end > > [/code] > > > Any suggestions appreciated > > -- > > Posted viahttp://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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung wrote:> On Mar 31, 3:41�pm, Danimal <fightonfightw...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> The flip-side may make more sense, while I think of it: spawn a thread >> to do the FTP connection and have your main thread sleep 30 seconds. >> Then have the main thread check the status and if need be kill the FTP >> connection or the child thread altogether. >> > which is basically what > http://www.ruby-doc.org/stdlib/libdoc/timeout/rdoc/index.html > does > > FredThanks for all the quick informative responses" I got the Timeout class working it appears to solve the problem nicely. I am doing this task ''outside'' my rails app using the "background plugin" it seems to be working well so far. -- 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---