Class: Polyrun::WorkerOutput::LineForwarder

Inherits:
Object
  • Object
show all
Defined in:
lib/polyrun/worker_output_forwarders.rb

Instance Method Summary collapse

Constructor Details

#initialize(shard:, pid:, log_io:, prefix_live:, tty_io:) ⇒ LineForwarder

Returns a new instance of LineForwarder.



6
7
8
9
10
11
12
13
14
# File 'lib/polyrun/worker_output_forwarders.rb', line 6

def initialize(shard:, pid:, log_io:, prefix_live:, tty_io:)
  @shard = shard
  @pid = pid
  @log_io = log_io
  @prefix_live = prefix_live
  @tty_io = tty_io
  @buffer = +""
  @mutex = Mutex.new
end

Instance Method Details

#consume(chunk) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/polyrun/worker_output_forwarders.rb', line 16

def consume(chunk)
  return if chunk.nil? || chunk.empty?

  @mutex.synchronize do
    if progress_chunk?(chunk)
      write_tty(chunk)
      @log_io&.write(chunk)
      return
    end

    @buffer << chunk
    while (newline_index = @buffer.index("\n"))
      emit_line(@buffer.slice!(0, newline_index + 1))
    end
  end
end

#flushObject



33
34
35
36
37
38
39
40
# File 'lib/polyrun/worker_output_forwarders.rb', line 33

def flush
  @mutex.synchronize do
    next if @buffer.empty?

    emit_line(@buffer)
    @buffer.clear
  end
end