Class: Microsandbox::ExecHandle

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

Overview

A live, streaming command execution, returned by Sandbox#exec_stream and Sandbox#shell_stream.

Iterate it (it is Enumerable) to consume ExecEvents as they arrive, or call #collect to drain it into an ExecOutput.

Examples:

handle = sb.exec_stream("python", ["-u", "script.py"])
handle.each do |event|
  print event.text if event.stdout? || event.stderr?
end

Instance Method Summary collapse

Constructor Details

#initialize(native) ⇒ ExecHandle

Returns a new instance of ExecHandle.



90
91
92
# File 'lib/microsandbox/exec_handle.rb', line 90

def initialize(native)
  @native = native
end

Instance Method Details

#collectExecOutput

Drain the stream and collect all output.

Returns:



120
121
122
# File 'lib/microsandbox/exec_handle.rb', line 120

def collect
  ExecOutput.new(@native.collect)
end

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

Yield each Microsandbox::ExecEvent until the stream ends. Returns an Enumerator when called without a block.

Yield Parameters:

Returns:

  • (self, Enumerator)


103
104
105
106
107
108
109
110
# File 'lib/microsandbox/exec_handle.rb', line 103

def each
  return enum_for(:each) unless block_given?

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

#idString

Returns the correlation id for this execution.

Returns:

  • (String)

    the correlation id for this execution



95
96
97
# File 'lib/microsandbox/exec_handle.rb', line 95

def id
  @native.id
end

#killnil

Kill the running process (SIGKILL).

Returns:

  • (nil)


133
134
135
136
# File 'lib/microsandbox/exec_handle.rb', line 133

def kill
  @native.kill
  nil
end

#resize(rows, cols) ⇒ nil

Resize the pseudo-terminal (only meaningful when started with tty: true).

Returns:

  • (nil)


140
141
142
143
# File 'lib/microsandbox/exec_handle.rb', line 140

def resize(rows, cols)
  @native.resize(Integer(rows), Integer(cols))
  nil
end

#signal(sig) ⇒ nil

Send a signal (integer) to the running process.

Returns:

  • (nil)


126
127
128
129
# File 'lib/microsandbox/exec_handle.rb', line 126

def signal(sig)
  @native.signal(Integer(sig))
  nil
end

#stdinExecStdin?

The stdin writer, or nil if stdin was not piped. Returned only once.

Returns:



147
148
149
150
151
152
# File 'lib/microsandbox/exec_handle.rb', line 147

def stdin
  return @stdin if defined?(@stdin)

  native_sink = @native.take_stdin
  @stdin = native_sink && ExecStdin.new(native_sink)
end

#waitExitStatus

Block until the process exits.

Returns:



114
115
116
# File 'lib/microsandbox/exec_handle.rb', line 114

def wait
  ExitStatus.new(@native.wait)
end