class Net::SSH::Shell::Process
Attributes
callback[R]
command[R]
exit_status[R]
manager[R]
properties[R]
state[R]
Public Class Methods
new(manager, command, properties, callback)
click to toggle source
# File lib/net/ssh/shell/process.rb, line 10 def initialize(manager, command, properties, callback) @command = command @manager = manager @callback = callback @properties = properties @on_output = nil @on_error_output = nil @on_finish = nil @state = :new end
Public Instance Methods
[](key)
click to toggle source
# File lib/net/ssh/shell/process.rb, line 21 def [](key) @properties[key] end
[]=(key, value)
click to toggle source
# File lib/net/ssh/shell/process.rb, line 25 def []=(key, value) @properties[key] = value end
busy?()
click to toggle source
# File lib/net/ssh/shell/process.rb, line 67 def busy? starting? || running? end
finished?()
click to toggle source
# File lib/net/ssh/shell/process.rb, line 63 def finished? state == :finished end
on_error_output(&callback)
click to toggle source
# File lib/net/ssh/shell/process.rb, line 80 def on_error_output(&callback) @on_error_output = callback end
on_finish(&callback)
click to toggle source
# File lib/net/ssh/shell/process.rb, line 84 def on_finish(&callback) @on_finish = callback end
on_output(&callback)
click to toggle source
# File lib/net/ssh/shell/process.rb, line 76 def on_output(&callback) @on_output = callback end
run()
click to toggle source
# File lib/net/ssh/shell/process.rb, line 33 def run if state == :new state = :starting manager.open do state = :running manager.channel.on_data(&method(:on_stdout)) manager.channel.on_extended_data(&method(:on_stderr)) manager.channel.on_close(&method(:on_close)) callback.call(self) if callback cmd = command.dup cmd << ';' if cmd !~ /[;&]$/ cmd << " DONTEVERUSETHIS=$?; echo #{manager.separator} $DONTEVERUSETHIS; echo \"exit $DONTEVERUSETHIS\"|sh" send_data(cmd + "\n") end end self end
running?()
click to toggle source
# File lib/net/ssh/shell/process.rb, line 59 def running? state == :running end
send_data(data)
click to toggle source
# File lib/net/ssh/shell/process.rb, line 29 def send_data(data) manager.channel.send_data(data) end
starting?()
click to toggle source
# File lib/net/ssh/shell/process.rb, line 55 def starting? state == :starting end
wait!()
click to toggle source
# File lib/net/ssh/shell/process.rb, line 71 def wait! manager.session.loop { busy? } self end
Protected Instance Methods
finished!(status)
click to toggle source
# File lib/net/ssh/shell/process.rb, line 113 def finished!(status) @state = :finished @exit_status = status.to_i @on_finish.call(self) if @on_finish manager.child_finished(self) end
on_close(ch)
click to toggle source
# File lib/net/ssh/shell/process.rb, line 108 def on_close(ch) manager.on_channel_close(ch) finished!(-1) end
on_stderr(_ch, _type, data)
click to toggle source
# File lib/net/ssh/shell/process.rb, line 104 def on_stderr(_ch, _type, data) @on_error_output.call(self, data) if @on_error_output end
on_stdout(_ch, data)
click to toggle source
# File lib/net/ssh/shell/process.rb, line 94 def on_stdout(_ch, data) if data.strip =~ /#{manager.separator} (\d+)$/ before = $` output!(before) unless before.empty? finished!(Regexp.last_match[1]) else output!(data) end end
output!(data)
click to toggle source
# File lib/net/ssh/shell/process.rb, line 90 def output!(data) @on_output.call(self, data) if @on_output end