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
# File 'lib/process-helper.rb', line 97

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

Instance Method Details

#drainObject



135
136
137
138
139
140
141
# File 'lib/process-helper.rb', line 135

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

#startObject



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

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
  end
  self
end

#to_aObject



131
132
133
# File 'lib/process-helper.rb', line 131

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

#to_sObject



143
144
145
# File 'lib/process-helper.rb', line 143

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

#waitObject



125
126
127
128
129
# File 'lib/process-helper.rb', line 125

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

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



115
116
117
118
119
120
121
122
123
# File 'lib/process-helper.rb', line 115

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
  end
end