Class: ProcessHelper::ProcessLog

Inherits:
Object
  • Object
show all
Defined in:
lib/process-helper.rb

Instance Method Summary collapse

Constructor Details

#initialize(io, opts, prefill = []) ⇒ ProcessLog

Returns a new instance of ProcessLog.



97
98
99
100
101
102
103
# File 'lib/process-helper.rb', line 97

def initialize(io, opts, prefill = [])
  @io = io
  @lines = prefill.dup
  @mutex = Mutex.new
  @opts = opts
  @eof = false
end

Instance Method Details

#drainObject



142
143
144
145
146
147
148
# File 'lib/process-helper.rb', line 142

def drain
  @mutex.synchronize do
    r = @lines.dup
    @lines.clear
    r
  end
end

#eofObject



117
118
119
# File 'lib/process-helper.rb', line 117

def eof
  @mutex.synchronize { !!@eof }
end

#startObject



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/process-helper.rb', line 105

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_aObject



138
139
140
# File 'lib/process-helper.rb', line 138

def to_a
  @mutex.synchronize { @lines.dup }
end

#to_sObject



150
151
152
# File 'lib/process-helper.rb', line 150

def to_s
  @mutex.synchronize { @lines.join '' }
end

#waitObject



132
133
134
135
136
# File 'lib/process-helper.rb', line 132

def wait
  @thread.join
  @thread = nil
  self
end

#wait_for_output(regex, opts = {}) ⇒ Object



121
122
123
124
125
126
127
128
129
130
# File 'lib/process-helper.rb', line 121

def wait_for_output(regex, opts = {})
  opts = { :poll_rate => 0.25 }.merge(opts)
  opts[:timeout] ||= 30
  cutoff = DateTime.now + Rational(opts[:timeout].to_i, 86_400)
  until _any_line_matches(regex)
    sleep(opts[:poll_rate])
    fail "Timeout of #{opts[:timeout]} seconds exceeded while waiting for output that matches '#{regex}'" if DateTime.now > cutoff
    fail "EOF encountered while waiting for output that matches '#{regex}'" if eof and !_any_line_matches(regex)
  end
end