Class: Bsdkrun::ShellSession

Inherits:
Object
  • Object
show all
Defined in:
lib/bsdkrun/shell_session.rb

Overview

A live interactive session opened by Client#shell.

Output arrives on the WsClient's background reader thread, so #on_output / #on_exit callbacks run on that thread, not the caller's — the same tradeoff web/src/lib/graphql.ts makes with its onNext handlers, just single-threaded there because JS has no threads to worry about.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#idString (readonly)

Returns the session id (+openShell+'s return value).

Returns:

  • (String)

    the session id (+openShell+'s return value).



15
16
17
# File 'lib/bsdkrun/shell_session.rb', line 15

def id
  @id
end

Instance Method Details

#closevoid

This method returns an undefined value.

Unsubscribe and close the session. Idempotent on the wire (+closeShell+ is safe to call on an already-closed session) — a request failure here (already gone, machine removed, etc.) is swallowed rather than raised, matching the other SDKs' close/closeShell handling.



104
105
106
107
108
109
110
111
112
# File 'lib/bsdkrun/shell_session.rb', line 104

def close
  @unsubscribe&.call
  begin
    @client.request("mutation($s:String!){ closeShell(sessionId:$s) }", { s: @id })
  rescue GraphQLError
    nil
  end
  nil
end

#on_exit {|exit_code| ... } ⇒ void

This method returns an undefined value.

Register a callback for the session's exit code. Fires once — right away if the session had already exited before this was called.

Yield Parameters:

  • exit_code (Integer, nil)

    nil if the session ended without ever reporting one (e.g. the connection dropped).



68
69
70
71
72
73
74
# File 'lib/bsdkrun/shell_session.rb', line 68

def on_exit(&block)
  already_exited, code = @callback_mutex.synchronize do
    @on_exit = block
    [@exit_delivered, @buffered_exit]
  end
  block.call(code) if already_exited
end

#on_output {|bytes| ... } ⇒ void

This method returns an undefined value.

Register a callback for output chunks (already base64-decoded). Any output that arrived before this was called is replayed immediately.

Yield Parameters:

  • bytes (String)

    binary-safe.



53
54
55
56
57
58
59
60
61
# File 'lib/bsdkrun/shell_session.rb', line 53

def on_output(&block)
  buffered = @callback_mutex.synchronize do
    @on_output = block
    buf = @buffered_output
    @buffered_output = []
    buf
  end
  buffered.each { |bytes| block.call(bytes) }
end

#resize(rows, cols) ⇒ void

This method returns an undefined value.

Resize the pseudo-terminal.

Parameters:

  • rows (Integer)
  • cols (Integer)


91
92
93
94
95
96
97
# File 'lib/bsdkrun/shell_session.rb', line 91

def resize(rows, cols)
  @client.request(
    "mutation($s:String!,$r:Int!,$c:Int!){ resizeShell(sessionId:$s, rows:$r, cols:$c) }",
    { s: @id, r: rows, c: cols }
  )
  nil
end

#write(data) ⇒ void

This method returns an undefined value.

Send keystrokes / input bytes.

Parameters:

  • data (String)


79
80
81
82
83
84
85
# File 'lib/bsdkrun/shell_session.rb', line 79

def write(data)
  @client.request(
    "mutation($s:String!,$d:String!){ sendShellInput(sessionId:$s, dataBase64:$d) }",
    { s: @id, d: Base64.strict_encode64(data.to_s.b) }
  )
  nil
end