Class: Microsandbox::PullSession

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

Overview

Note:

Single-pass, forward-only, single-consumer. each drains a one-shot native progress channel — not rewindable, iterate once from a single thread. #sandbox works whether or not you iterated (it awaits the create either way), so you can skip each entirely; but don't expect a second each to replay the events.

A streaming image-pull + create session, from Sandbox.create_with_progress. Iterate it (it is Enumerable) to consume progress-event Hashes as the image pulls, then call #sandbox to get the booted Sandbox. Each event Hash has a "kind" key (e.g. "resolving", "resolved", "layer_download_progress", "layer_materialize_progress", "complete") plus kind-specific fields.

Examples:

session = Microsandbox::Sandbox.create_with_progress("box", image: "python")
session.each { |ev| puts "#{ev["kind"]} #{ev["downloaded_bytes"]}" }
sb = session.sandbox
begin
  sb.exec("python", ["-V"])
ensure
  sb.stop
end

Instance Method Summary collapse

Constructor Details

#initialize(native) ⇒ PullSession

Returns a new instance of PullSession.



1136
1137
1138
# File 'lib/microsandbox/sandbox.rb', line 1136

def initialize(native)
  @native = native
end

Instance Method Details

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

Yield each progress-event Hash until the pull finishes. Returns an Enumerator when called without a block.

Yield Parameters:

  • event (Hash)

Returns:

  • (self, Enumerator)


1144
1145
1146
1147
1148
1149
1150
1151
# File 'lib/microsandbox/sandbox.rb', line 1144

def each
  return enum_for(:each) unless block_given?

  while (event = @native.recv)
    yield event
  end
  self
end

#sandboxSandbox

The booted sandbox. Joins the create task (draining any remaining pull progress first), so call it after iterating progress. The returned Sandbox is live — stop it when done. Memoized; callable once.

Returns:



1157
1158
1159
# File 'lib/microsandbox/sandbox.rb', line 1157

def sandbox
  @sandbox ||= Sandbox.new(@native.result)
end