I have simple connection object
require 'net/sftp'
@con = Net::SFTP.start(@@host, @@username, :password => @@password
...
@con.upload!(filepath, destination)
I have this in a script that listens for files being dropped into a folder. The files are collected, and uploaded to a remote sfpt server. It all works fine, but it seems after the script is left running for long enough, it will break and throw this error:
exception while processing events: Write to the server failed
I can only assume this happens because the idle connection is disconnecting after a certain amount of time. Is there away to keep the connection open indefinitely? or is that a limitation?
If thats not possible, is there away I can manage the error? Something like:
begin
@con.upload!(filepath, destination)
rescue Net::SFTPStatusException => e
if e.code == 7 //connection lost code
@con.{reconnect}//Whatever this would look like
end
else
raise
end
end
Not sure if 7 is the right error code, or how I could reconnect the connection object.
Since this uses SFTP, i.e. it is proxied over SSH, it may be possible to send a 'ping' every X seconds to keep the connection open. You can do this with
ServerAliveIntervalin~/.ssh/configor/etc/ssh/sshd_config, for example:Also note that any options passed to
Net::SFTP.startare passed to theNet::SSH.startso you can use options such asverbose: :debugto get more information about what is happening.Another option would be to do something like list files in some directory within the loop which waits for a file to be uploaded in order to keep the connection active. It might also be possible to do this in a separate thread if SSH connections to the same host are shared.