Class: Bash::Session
- Inherits:
-
Object
- Object
- Bash::Session
- Defined in:
- lib/bash/session.rb,
lib/bash/session/version.rb
Defined Under Namespace
Classes: TimeoutError
Constant Summary collapse
- VERSION =
"0.0.3"
Instance Attribute Summary collapse
-
#default_timeout ⇒ Object
readonly
Returns the value of attribute default_timeout.
Instance Method Summary collapse
- #close ⇒ Object
- #execute(command, options = {}, &callback) ⇒ Object
-
#initialize(default_timeout = nil) ⇒ Session
constructor
A new instance of Session.
Constructor Details
#initialize(default_timeout = nil) ⇒ Session
Returns a new instance of Session.
11 12 13 14 15 |
# File 'lib/bash/session.rb', line 11 def initialize(default_timeout=nil) start_session @default_timeout = default_timeout @separator = SecureRandom.hex end |
Instance Attribute Details
#default_timeout ⇒ Object (readonly)
Returns the value of attribute default_timeout.
9 10 11 |
# File 'lib/bash/session.rb', line 9 def default_timeout @default_timeout end |
Instance Method Details
#close ⇒ Object
51 52 53 54 55 56 57 58 |
# File 'lib/bash/session.rb', line 51 def close return unless @wait_thr.alive? return if (Process.kill('TERM', @wait_thr.pid) rescue nil) return unless @wait_thr.alive? return if (Process.kill('KILL', @wait_thr.pid) rescue nil) return unless @wait_thr.alive? raise "Could not kill process(PID #{@wait_thr.pid})" end |
#execute(command, options = {}, &callback) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/bash/session.rb', line 17 def execute(command, ={}, &callback) exit_status = nil out = [:out] timeout = [:timeout] || @default_timeout cmd = command.dup cmd << ";" if cmd !~ /[;&]$/ cmd << "\n" if cmd =~ /#/ cmd << %Q{DONTEVERUSETHIS=$?; echo "\n#{@separator} $DONTEVERUSETHIS"; echo "exit $DONTEVERUSETHIS"|sh} @stdin.puts(cmd) until exit_status do begin data = @outstr.read_nonblock(160000) if data.strip =~ /^#{@separator} (\d+)\s*/ exit_status = $1 data.gsub!(/\n^#{@separator} (\d+)\s*$/, '') end callback.call(data) if callback out.write data if out rescue IO::WaitReadable ready = IO.select([@outstr], nil, nil, timeout) unless ready raise TimeoutError.new("No output received for the last #{timeout} seconds. Timing out...") else retry end end end exit_status.to_i end |