Class: ProcessHelper::ProcessLog
- Inherits:
-
Object
- Object
- ProcessHelper::ProcessLog
- Defined in:
- lib/process-helper.rb
Instance Method Summary collapse
- #drain ⇒ Object
- #eof ⇒ Object
-
#initialize(io, opts, prefill = []) ⇒ ProcessLog
constructor
A new instance of ProcessLog.
- #start ⇒ Object
- #to_a ⇒ Object
- #to_s ⇒ Object
- #wait ⇒ Object
- #wait_for_output(regex, opts = {}) ⇒ Object
Constructor Details
#initialize(io, opts, prefill = []) ⇒ ProcessLog
Returns a new instance of ProcessLog.
99 100 101 102 103 104 105 |
# File 'lib/process-helper.rb', line 99 def initialize(io, opts, prefill = []) @io = io @lines = prefill.dup @mutex = Mutex.new @opts = opts @eof = false end |
Instance Method Details
#drain ⇒ Object
144 145 146 147 148 149 150 |
# File 'lib/process-helper.rb', line 144 def drain @mutex.synchronize do r = @lines.dup @lines.clear r end end |
#eof ⇒ Object
119 120 121 |
# File 'lib/process-helper.rb', line 119 def eof @mutex.synchronize { !!@eof } end |
#start ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/process-helper.rb', line 107 def start @thread = Thread.new do @io.each_line do |l| l = TimestampedString.new(l) STDOUT.puts l if @opts[:print_lines] @mutex.synchronize { @lines.push l } end @mutex.synchronize { @eof = true } end self end |
#to_a ⇒ Object
140 141 142 |
# File 'lib/process-helper.rb', line 140 def to_a @mutex.synchronize { @lines.dup } end |
#to_s ⇒ Object
152 153 154 |
# File 'lib/process-helper.rb', line 152 def to_s @mutex.synchronize { @lines.join '' } end |
#wait ⇒ Object
134 135 136 137 138 |
# File 'lib/process-helper.rb', line 134 def wait @thread.join @thread = nil self end |
#wait_for_output(regex, opts = {}) ⇒ Object
123 124 125 126 127 128 129 130 131 132 |
# File 'lib/process-helper.rb', line 123 def wait_for_output(regex, opts = {}) opts = { :poll_rate => 0.25 }.merge(opts) opts[:timeout] ||= 30 cutoff = Time.now + opts[:timeout].to_i until _any_line_matches(regex) sleep(opts[:poll_rate]) fail "Timeout of #{opts[:timeout]} seconds exceeded while waiting for output that matches '#{regex}'" if Time.now > cutoff fail "EOF encountered while waiting for output that matches '#{regex}'" if eof and !_any_line_matches(regex) end end |