Class: Daytona::PtyHandle
- Inherits:
-
Object
- Object
- Daytona::PtyHandle
- Includes:
- Observable
- Defined in:
- lib/daytona/common/pty.rb
Overview
rubocop:disable Metrics/ClassLength
Defined Under Namespace
Modules: Status, WebSocketControlStatus, WebSocketMessageType
Instance Attribute Summary collapse
-
#error ⇒ String?
readonly
Error message if the PTY failed.
-
#exit_code ⇒ Integer?
readonly
Exit code of the PTY process (if terminated).
-
#session_id ⇒ String
readonly
Session ID of the PTY session.
Instance Method Summary collapse
-
#connected? ⇒ Boolean
Check if connected to the PTY session.
-
#disconnect ⇒ void
Disconnect from the PTY session.
- #each {|| ... } ⇒ void
-
#initialize(websocket, session_id:, handle_resize: nil, handle_kill: nil) ⇒ PtyHandle
constructor
Initialize the PTY handle.
-
#kill ⇒ void
Delete the PTY session.
-
#resize(pty_size) ⇒ DaytonaApiClient::PtySessionInfo
Resize the PTY terminal.
-
#send_input(input) ⇒ void
Send input to the PTY session.
-
#wait(timeout: nil, &on_data) ⇒ Daytona::PtyResult
Wait for the PTY session to complete.
-
#wait_for_connection(timeout: DEFAULT_TIMEOUT) ⇒ void
Wait for the PTY connection to be established.
Constructor Details
#initialize(websocket, session_id:, handle_resize: nil, handle_kill: nil) ⇒ PtyHandle
Initialize the PTY handle.
61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/daytona/common/pty.rb', line 61 def initialize(websocket, session_id:, handle_resize: nil, handle_kill: nil) @websocket = websocket @session_id = session_id @handle_resize = handle_resize @handle_kill = handle_kill @exit_code = nil @error = nil @logger = Sdk.logger @status = Status::INIT subscribe end |
Instance Attribute Details
#error ⇒ String? (readonly)
Returns Error message if the PTY failed.
53 54 55 |
# File 'lib/daytona/common/pty.rb', line 53 def error @error end |
#exit_code ⇒ Integer? (readonly)
Returns Exit code of the PTY process (if terminated).
50 51 52 |
# File 'lib/daytona/common/pty.rb', line 50 def exit_code @exit_code end |
#session_id ⇒ String (readonly)
Returns Session ID of the PTY session.
47 48 49 |
# File 'lib/daytona/common/pty.rb', line 47 def session_id @session_id end |
Instance Method Details
#connected? ⇒ Boolean
Check if connected to the PTY session
77 |
# File 'lib/daytona/common/pty.rb', line 77 def connected? = websocket.open? |
#disconnect ⇒ void
This method returns an undefined value.
Disconnect from the PTY session
160 |
# File 'lib/daytona/common/pty.rb', line 160 def disconnect = websocket.close |
#each {|| ... } ⇒ void
This method returns an undefined value.
143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/daytona/common/pty.rb', line 143 def each(&) return unless block_given? queue = Queue.new add_observer(proc { queue << _1 }, :call) while websocket.open? drain(queue, &) sleep(SLEEP_INTERVAL) end drain(queue, &) end |
#kill ⇒ void
This method returns an undefined value.
Delete the PTY session
117 118 119 120 121 |
# File 'lib/daytona/common/pty.rb', line 117 def kill raise Sdk::Error, 'No kill handler available' unless handle_kill handle_kill.call end |
#resize(pty_size) ⇒ DaytonaApiClient::PtySessionInfo
Resize the PTY terminal
108 109 110 111 112 |
# File 'lib/daytona/common/pty.rb', line 108 def resize(pty_size) raise Sdk::Error, 'No resize handler available' unless handle_resize handle_resize.call(pty_size) end |
#send_input(input) ⇒ void
This method returns an undefined value.
Send input to the PTY session
98 99 100 101 102 |
# File 'lib/daytona/common/pty.rb', line 98 def send_input(input) raise Sdk::Error, 'PTY session not connected' unless websocket.open? websocket.send(input) end |
#wait(timeout: nil, &on_data) ⇒ Daytona::PtyResult
Wait for the PTY session to complete
127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/daytona/common/pty.rb', line 127 def wait(timeout: nil, &on_data) timeout ||= Float::INFINITY return unless status == Status::CONNECTED start_time = Time.now add_observer(on_data, :call) if on_data sleep(SLEEP_INTERVAL) while status == Status::CONNECTED && (Time.now - start_time) <= timeout PtyResult.new(exit_code:, error:) ensure delete_observer(on_data) if on_data end |
#wait_for_connection(timeout: DEFAULT_TIMEOUT) ⇒ void
This method returns an undefined value.
Wait for the PTY connection to be established
84 85 86 87 88 89 90 91 92 |
# File 'lib/daytona/common/pty.rb', line 84 def wait_for_connection(timeout: DEFAULT_TIMEOUT) return if status == Status::CONNECTED start_time = Time.now sleep(SLEEP_INTERVAL) until status == Status::CONNECTED || (Time.now - start_time) > timeout raise Sdk::Error, 'PTY connection timeout' unless status == Status::CONNECTED end |