Forum: Ruby on Rails Handle Net::FTP connection timeout

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Brian Z. (Guest)
on 2009-03-31 18:14
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
Danimal (Guest)
on 2009-03-31 18:42
(Received via mailing list)
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 Z. <removed_email_address@domain.invalid>
bill walton (Guest)
on 2009-03-31 18:48
(Received via mailing list)
Hi Brian,

On Tue, 2009-03-31 at 16:14 +0200, Brian Z. 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
Frederick C. (Guest)
on 2009-03-31 19:18
(Received via mailing list)
On Mar 31, 3:41 pm, Danimal <removed_email_address@domain.invalid> 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...
does

Fred
Brian Z. (Guest)
on 2009-03-31 19:58
Frederick C. wrote:
> On Mar 31, 3:41�pm, Danimal <removed_email_address@domain.invalid> 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...
> does
>
> Fred

Thanks 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.
This topic is locked and can not be replied to.