Class: Bsdkrun::ShellSession
- Inherits:
-
Object
- Object
- Bsdkrun::ShellSession
- 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
-
#id ⇒ String
readonly
The session id (+openShell+'s return value).
Instance Method Summary collapse
-
#close ⇒ void
Unsubscribe and close the session.
-
#on_exit {|exit_code| ... } ⇒ void
Register a callback for the session's exit code.
-
#on_output {|bytes| ... } ⇒ void
Register a callback for output chunks (already base64-decoded).
-
#resize(rows, cols) ⇒ void
Resize the pseudo-terminal.
-
#write(data) ⇒ void
Send keystrokes / input bytes.
Instance Attribute Details
#id ⇒ String (readonly)
Returns 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
#close ⇒ void
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.
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.
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.
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.
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 |