Class: Microsandbox::MetricsStream

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

Overview

Note:

Single-pass, forward-only, single-consumer. Like LogStream, each drains a one-shot native channel — not rewindable, iterate once from a single thread; a second pass or a post-drain combinator yields nothing.

A live stream of Metrics snapshots, returned by Sandbox#metrics_stream. Enumerable: iteration yields one snapshot per interval tick until the sandbox stops.

Examples:

sb.metrics_stream(interval: 0.5).each { |m| puts m.cpu_percent }

Instance Method Summary collapse

Constructor Details

#initialize(native) ⇒ MetricsStream

Returns a new instance of MetricsStream.



52
53
54
# File 'lib/microsandbox/streams.rb', line 52

def initialize(native)
  @native = native
end

Instance Method Details

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

Yield Parameters:

Returns:

  • (self, Enumerator)


58
59
60
61
62
63
64
65
# File 'lib/microsandbox/streams.rb', line 58

def each
  return enum_for(:each) unless block_given?

  while (snapshot = @native.recv)
    yield Metrics.new(snapshot)
  end
  self
end