Class: Microsandbox::LogStream

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/microsandbox/streams.rb

Overview

Note:

Single-pass, forward-only, single-consumer. each drains a one-shot native channel, so it is not rewindable: a second each — or any Enumerable combinator after a partial drain (to_a twice, count then each, first(n) then each) — silently yields nothing. Iterate exactly once, from one thread. With follow: true, each blocks the calling thread uninterruptibly until the sandbox stops (see DESIGN.md on GVL release).

A live stream of LogEntrys, returned by Sandbox#log_stream. Enumerable: iterate to consume entries as they are appended. With follow: true the iteration blocks for new entries until the sandbox stops; otherwise it ends once the historical log is drained.

Examples:

sb.log_stream(follow: true).each { |entry| print entry.text }

Instance Method Summary collapse

Constructor Details

#initialize(native) ⇒ LogStream

Returns a new instance of LogStream.



22
23
24
# File 'lib/microsandbox/streams.rb', line 22

def initialize(native)
  @native = native
end

Instance Method Details

#each {|entry| ... } ⇒ self, Enumerator

Yield Parameters:

Returns:

  • (self, Enumerator)


28
29
30
31
32
33
34
35
# File 'lib/microsandbox/streams.rb', line 28

def each
  return enum_for(:each) unless block_given?

  while (entry = @native.recv)
    yield LogEntry.new(entry)
  end
  self
end