Forum: Ruby on Rails Asynchronous SSH tasks

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.
31bb0c6ac470d40572c054ce8a495fa6?d=identicon&s=25 Paul Denize (pdenize)
on 2009-04-28 01:45
I have managed to get copies working well between Windows and Linux
but I noticed that the libraries allow Asynchronous operation.  I can
send Async commands and the loop till they complete but copies seem to
stay busy forever.  Maybe I am just not cleaning up right (it does not
seem so)

ssh_session = Net::SSH.start(...etc...)


# ssh_session.exec("sleep 10")
ssh_session.sftp.connect.upload(src_file, tgt_file)


ssh_session.loop(1) {
   puts "I=#{interrupted} B="+@ssh_session.busy?.inspect
   not interrupted and @ssh_session.busy?
 }


If the sleep is called (instead of the upload) it waits 10 secs and
then is not busy and wakes.
But the copy will not wake.  Since I have hundreds of files to copy
I'd like to set them all off, do some other work, and then wait for
them to complete (rather than wait for them one at a time with
upload!) and then do the other work.


Any help would be appreciated... should I cleanup the "sftp.connect"?
How?  When (if it is asynchronous)?


Thanks
31bb0c6ac470d40572c054ce8a495fa6?d=identicon&s=25 Paul Denize (pdenize)
on 2009-04-28 03:21
Found the solution

ssh_session = Net::SSH.start(...etc...)

sftp_conn = ssh_session.sftp.connect!   # needed to block connect here
(!) or the upload seems to hang
sftp_conn.upload(src_file, tgt_file)

interrupted = false
trap('INT') { interrupted = true }
sftp_conn.loop() {
    not interrupted and sftp_conn.pending_requests.any?   # check for
pending requests
}
sftp_conn.close_channel()
ssh_session.loop(1) {
   not interrupted and ssh_session.busy?
 }
sftp_conn = ssh_session.sftp.connect!


One word of warning if sending lots of files (>300) fast then you may
run out of open file handles (Windows).
I avoid the error by doing this call each now and then when starting
another upload

if (sftp_conn.pending_requests.length>=100)
  interrupted = false
  trap('INT') { interrupted = true }
  sftp_conn.loop() {
    not interrupted and sftp_conn.pending_requests.any?
  }
end

I guess I could have exited when the queue was smaller not empty also.

Hope this helps someone else.
This topic is locked and can not be replied to.