Class: Daytona::Process
- Inherits:
-
Object
- Object
- Daytona::Process
- Includes:
- Instrumentation
- Defined in:
- lib/daytona/process.rb
Overview
rubocop:disable Metrics/ClassLength
Instance Attribute Summary collapse
-
#get_preview_link ⇒ Proc
readonly
Function to get preview link for a port.
-
#language ⇒ String
readonly
The language for code execution (e.g. 'python', 'typescript', 'javascript').
-
#sandbox_id ⇒ String
readonly
The ID of the Sandbox.
-
#toolbox_api ⇒ DaytonaToolboxApiClient::ProcessApi
readonly
API client for Sandbox operations.
Instance Method Summary collapse
-
#code_run(code:, params: nil, timeout: nil) ⇒ ExecuteResponse
Execute code in the Sandbox using the appropriate language runtime.
-
#connect_pty_session(session_id) ⇒ PtyHandle
Connects to an existing PTY session in the Sandbox.
-
#create_pty_session(id:, cwd: nil, envs: nil, pty_size: nil) ⇒ PtyHandle
Creates a new PTY (pseudo-terminal) session in the Sandbox.
-
#create_session(session_id) ⇒ void
Creates a new long-running background session in the Sandbox.
-
#delete_pty_session(session_id) ⇒ void
Deletes a PTY session, terminating the associated process.
-
#delete_session(session_id) ⇒ Object
Terminates and removes a session from the Sandbox, cleaning up any resources associated with it.
-
#exec(command:, cwd: nil, env: nil, timeout: nil) ⇒ ExecuteResponse
Execute a shell command in the Sandbox.
-
#execute_session_command(session_id:, req:) ⇒ Daytona::SessionExecuteResponse
Executes a command in the session.
-
#get_entrypoint_logs ⇒ Daytona::SessionCommandLogsResponse
Get the sandbox entrypoint logs.
-
#get_entrypoint_logs_async(on_stdout:, on_stderr:) ⇒ WebSocket::Client::Simple::Client
Asynchronously retrieves and processes the sandbox entrypoint logs as they become available.
-
#get_entrypoint_session ⇒ DaytonaApiClient::Session
Gets the Sandbox entrypoint session.
-
#get_pty_session_info(session_id) ⇒ DaytonaApiClient::PtySessionInfo
Gets detailed information about a specific PTY session.
-
#get_session(session_id) ⇒ DaytonaApiClient::Session
Gets a session in the Sandbox.
-
#get_session_command(session_id:, command_id:) ⇒ DaytonaApiClient::Command
Gets information about a specific command executed in a session.
-
#get_session_command_logs(session_id:, command_id:) ⇒ Daytona::SessionCommandLogsResponse
Get the logs for a command executed in a session.
-
#get_session_command_logs_async(session_id:, command_id:, on_stdout:, on_stderr:) ⇒ WebSocket::Client::Simple::Client
Asynchronously retrieves and processes the logs for a command executed in a session as they become available.
-
#initialize(sandbox_id:, toolbox_api:, get_preview_link:, language: 'python', otel_state: nil) ⇒ Process
constructor
Initialize a new Process instance.
-
#list_pty_sessions ⇒ Array<DaytonaApiClient::PtySessionInfo>
Lists all PTY sessions in the Sandbox.
-
#list_sessions ⇒ Array<DaytonaApiClient::Session>
List of all sessions in the Sandbox.
-
#resize_pty_session(session_id, pty_size) ⇒ DaytonaApiClient::PtySessionInfo
Resizes a PTY session to the specified dimensions.
-
#send_session_command_input(session_id:, command_id:, data:) ⇒ void
Sends input data to a command executed in a session.
Methods included from Instrumentation
Constructor Details
#initialize(sandbox_id:, toolbox_api:, get_preview_link:, language: 'python', otel_state: nil) ⇒ Process
Initialize a new Process instance
33 34 35 36 37 38 39 |
# File 'lib/daytona/process.rb', line 33 def initialize(sandbox_id:, toolbox_api:, get_preview_link:, language: 'python', otel_state: nil) @sandbox_id = sandbox_id @toolbox_api = toolbox_api @get_preview_link = get_preview_link @language = language @otel_state = otel_state end |
Instance Attribute Details
#get_preview_link ⇒ Proc (readonly)
Returns Function to get preview link for a port.
21 22 23 |
# File 'lib/daytona/process.rb', line 21 def get_preview_link @get_preview_link end |
#language ⇒ String (readonly)
Returns The language for code execution (e.g. 'python', 'typescript', 'javascript').
24 25 26 |
# File 'lib/daytona/process.rb', line 24 def language @language end |
#sandbox_id ⇒ String (readonly)
Returns The ID of the Sandbox.
15 16 17 |
# File 'lib/daytona/process.rb', line 15 def sandbox_id @sandbox_id end |
#toolbox_api ⇒ DaytonaToolboxApiClient::ProcessApi (readonly)
Returns API client for Sandbox operations.
18 19 20 |
# File 'lib/daytona/process.rb', line 18 def toolbox_api @toolbox_api end |
Instance Method Details
#code_run(code:, params: nil, timeout: nil) ⇒ ExecuteResponse
Execute code in the Sandbox using the appropriate language runtime
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/daytona/process.rb', line 90 def code_run(code:, params: nil, timeout: nil) response = toolbox_api.code_run( DaytonaToolboxApiClient::CodeRunRequest.new( code:, language:, argv: params&.argv, envs: params&.env, timeout: ) ) ExecuteResponse.new( exit_code: response.exit_code, result: response.result, artifacts: ExecutionArtifacts.new(response.result, (response.artifacts&.charts || []).map do |c| Charts.parse_chart(c) end) ) rescue *Sdk::API_ERROR_CLASSES => e raise Sdk.wrap_error(e, 'Failed to run code') end |
#connect_pty_session(session_id) ⇒ PtyHandle
Connects to an existing PTY session in the Sandbox.
Establishes a WebSocket connection to an existing PTY session, allowing you to interact with a previously created terminal session.
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 |
# File 'lib/daytona/process.rb', line 492 def connect_pty_session(session_id) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength preview_link = get_preview_link.call(WS_PORT) url = URI.parse(preview_link.url) url.scheme = url.scheme == 'https' ? 'wss' : 'ws' url.path = "/process/pty/#{session_id}/connect" headers = toolbox_api.api_client.default_headers.dup.merge( 'X-Daytona-Preview-Token' => preview_link.token ) headers['Sec-WebSocket-Protocol'] = [headers['Sec-WebSocket-Protocol'], PTY_EXIT_CONTROL_SUBPROTOCOL].compact.join(', ') handle = nil WebSocket::Client::Simple.connect(url.to_s, headers:) do |client| handle = PtyHandle.new( client, session_id:, handle_resize: ->(pty_size) { resize_pty_session(session_id, pty_size) }, handle_kill: -> { delete_pty_session(session_id) } ) end handle.tap(&:wait_for_connection) end |
#create_pty_session(id:, cwd: nil, envs: nil, pty_size: nil) ⇒ PtyHandle
Creates a new PTY (pseudo-terminal) session in the Sandbox.
Creates an interactive terminal session that can execute commands and handle user input. The PTY session behaves like a real terminal, supporting features like command history.
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 |
# File 'lib/daytona/process.rb', line 447 def create_pty_session(id:, cwd: nil, envs: nil, pty_size: nil) # rubocop:disable Metrics/MethodLength,Metrics/AbcSize cols = pty_size&.cols || 80 rows = pty_size&.rows || 24 preview_link = get_preview_link.call(WS_PORT) url = URI.parse(preview_link.url) url.scheme = url.scheme == 'https' ? 'wss' : 'ws' url.path = '/process/pty/create-connect' params = { id:, cols:, rows: } params[:cwd] = cwd if cwd url.query = URI.encode_www_form(params) headers = toolbox_api.api_client.default_headers.dup.merge( 'X-Daytona-Preview-Token' => preview_link.token ) protocols = [headers['Sec-WebSocket-Protocol'], PTY_EXIT_CONTROL_SUBPROTOCOL].compact protocols << "X-Daytona-Pty-Envs~#{Base64.urlsafe_encode64(envs.to_json, padding: false)}" if envs headers['Sec-WebSocket-Protocol'] = protocols.join(', ') PtyHandle.new( WebSocket::Client::Simple.connect(url.to_s, headers:), session_id: id, handle_resize: ->(pty_size_arg) { resize_pty_session(id, pty_size_arg) }, handle_kill: -> { delete_pty_session(id) } ).tap(&:wait_for_connection) end |
#create_session(session_id) ⇒ void
This method returns an undefined value.
Creates a new long-running background session in the Sandbox
Sessions are background processes that maintain state between commands, making them ideal for scenarios requiring multiple related commands or persistent environment setup.
123 124 125 126 127 |
# File 'lib/daytona/process.rb', line 123 def create_session(session_id) toolbox_api.create_session(DaytonaToolboxApiClient::CreateSessionRequest.new(session_id:)) rescue *Sdk::API_ERROR_CLASSES => e raise Sdk.wrap_error(e, 'Failed to create session') end |
#delete_pty_session(session_id) ⇒ void
This method returns an undefined value.
Deletes a PTY session, terminating the associated process
545 546 547 548 549 |
# File 'lib/daytona/process.rb', line 545 def delete_pty_session(session_id) toolbox_api.delete_pty_session(session_id) rescue *Sdk::API_ERROR_CLASSES => e raise Sdk.wrap_error(e, 'Failed to delete PTY session') end |
#delete_session(session_id) ⇒ Object
Terminates and removes a session from the Sandbox, cleaning up any resources associated with it
408 409 410 411 412 |
# File 'lib/daytona/process.rb', line 408 def delete_session(session_id) toolbox_api.delete_session(session_id) rescue *Sdk::API_ERROR_CLASSES => e raise Sdk.wrap_error(e, 'Failed to delete session') end |
#exec(command:, cwd: nil, env: nil, timeout: nil) ⇒ ExecuteResponse
Execute a shell command in the Sandbox
60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/daytona/process.rb', line 60 def exec(command:, cwd: nil, env: nil, timeout: nil) envs = env&.empty? ? nil : env response = toolbox_api.execute_command(DaytonaToolboxApiClient::ExecuteRequest.new(command:, cwd:, envs:, timeout:)) result = response.result || '' ExecuteResponse.new( exit_code: response.exit_code, result:, artifacts: ExecutionArtifacts.new(result, []) ) rescue *Sdk::API_ERROR_CLASSES => e raise Sdk.wrap_error(e, 'Failed to execute command') end |
#execute_session_command(session_id:, req:) ⇒ Daytona::SessionExecuteResponse
Executes a command in the session
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/daytona/process.rb', line 200 def execute_session_command(session_id:, req:) # rubocop:disable Metrics/MethodLength response = toolbox_api.session_execute_command( session_id, DaytonaToolboxApiClient::SessionExecuteRequest.new(command: req.command, run_async: req.run_async, suppress_input_echo: req.suppress_input_echo) ) SessionExecuteResponse.new( cmd_id: response.cmd_id, output: response.output, stdout: response.stdout || '', stderr: response.stderr || '', exit_code: response.exit_code, additional_properties: {} ) rescue *Sdk::API_ERROR_CLASSES => e raise Sdk.wrap_error(e, 'Failed to execute session command') end |
#get_entrypoint_logs ⇒ Daytona::SessionCommandLogsResponse
Get the sandbox entrypoint logs
302 303 304 305 306 307 |
# File 'lib/daytona/process.rb', line 302 def get_entrypoint_logs response = toolbox_api.get_entrypoint_logs SessionCommandLogsResponse.new(output: response.output, stdout: response.stdout, stderr: response.stderr) rescue *Sdk::API_ERROR_CLASSES => e raise Sdk.wrap_error(e, 'Failed to get entrypoint logs') end |
#get_entrypoint_logs_async(on_stdout:, on_stderr:) ⇒ WebSocket::Client::Simple::Client
Asynchronously retrieves and processes the sandbox entrypoint logs as they become available
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 |
# File 'lib/daytona/process.rb', line 320 def get_entrypoint_logs_async(on_stdout:, on_stderr:) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength preview_link = get_preview_link.call(WS_PORT) url = URI.parse(preview_link.url) url.scheme = url.scheme == 'https' ? 'wss' : 'ws' url.path = '/process/session/entrypoint/logs' url.query = 'follow=true' completion_queue = Queue.new WebSocket::Client::Simple.connect( url.to_s, headers: toolbox_api.api_client.default_headers.dup.merge( 'X-Daytona-Preview-Token' => preview_link.token, 'Content-Type' => 'text/plain', 'Accept' => 'text/plain' ) ) do |client| client.on(:message) do || if .type == :close client.close completion_queue.push(:close) else stdout, stderr = Util.demux(.data.to_s) on_stdout.call(stdout) unless stdout.empty? on_stderr.call(stderr) unless stderr.empty? end end client.on(:close) do completion_queue.push(:close) end client.on(:error) do |e| completion_queue.push(:error) raise Sdk::Error, "WebSocket error: #{e.}" end end # Wait for completion completion_queue.pop end |
#get_entrypoint_session ⇒ DaytonaApiClient::Session
Gets the Sandbox entrypoint session
154 155 156 157 158 |
# File 'lib/daytona/process.rb', line 154 def get_entrypoint_session toolbox_api.get_entrypoint_session rescue *Sdk::API_ERROR_CLASSES => e raise Sdk.wrap_error(e, 'Failed to get entrypoint session') end |
#get_pty_session_info(session_id) ⇒ DaytonaApiClient::PtySessionInfo
Gets detailed information about a specific PTY session
Retrieves comprehensive information about a PTY session including its current state, configuration, and metadata.
585 586 587 588 589 |
# File 'lib/daytona/process.rb', line 585 def get_pty_session_info(session_id) toolbox_api.get_pty_session(session_id) rescue *Sdk::API_ERROR_CLASSES => e raise Sdk.wrap_error(e, 'Failed to get PTY session info') end |
#get_session(session_id) ⇒ DaytonaApiClient::Session
Gets a session in the Sandbox
139 140 141 142 143 |
# File 'lib/daytona/process.rb', line 139 def get_session(session_id) toolbox_api.get_session(session_id) rescue *Sdk::API_ERROR_CLASSES => e raise Sdk.wrap_error(e, 'Failed to get session') end |
#get_session_command(session_id:, command_id:) ⇒ DaytonaApiClient::Command
Gets information about a specific command executed in a session
171 172 173 174 175 |
# File 'lib/daytona/process.rb', line 171 def get_session_command(session_id:, command_id:) toolbox_api.get_session_command(session_id, command_id) rescue *Sdk::API_ERROR_CLASSES => e raise Sdk.wrap_error(e, 'Failed to get session command') end |
#get_session_command_logs(session_id:, command_id:) ⇒ Daytona::SessionCommandLogsResponse
Get the logs for a command executed in a session
229 230 231 232 233 234 |
# File 'lib/daytona/process.rb', line 229 def get_session_command_logs(session_id:, command_id:) response = toolbox_api.get_session_command_logs(session_id, command_id) SessionCommandLogsResponse.new(output: response.output, stdout: response.stdout, stderr: response.stderr) rescue *Sdk::API_ERROR_CLASSES => e raise Sdk.wrap_error(e, 'Failed to get session command logs') end |
#get_session_command_logs_async(session_id:, command_id:, on_stdout:, on_stderr:) ⇒ WebSocket::Client::Simple::Client
Asynchronously retrieves and processes the logs for a command executed in a session as they become available
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 |
# File 'lib/daytona/process.rb', line 251 def get_session_command_logs_async(session_id:, command_id:, on_stdout:, on_stderr:) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength preview_link = get_preview_link.call(WS_PORT) url = URI.parse(preview_link.url) url.scheme = url.scheme == 'https' ? 'wss' : 'ws' url.path = "/process/session/#{session_id}/command/#{command_id}/logs" url.query = 'follow=true' completion_queue = Queue.new WebSocket::Client::Simple.connect( url.to_s, headers: toolbox_api.api_client.default_headers.dup.merge( 'X-Daytona-Preview-Token' => preview_link.token, 'Content-Type' => 'text/plain', 'Accept' => 'text/plain' ) ) do |client| client.on(:message) do || if .type == :close client.close completion_queue.push(:close) else stdout, stderr = Util.demux(.data.to_s) on_stdout.call(stdout) unless stdout.empty? on_stderr.call(stderr) unless stderr.empty? end end client.on(:close) do completion_queue.push(:close) end client.on(:error) do |e| completion_queue.push(:error) raise Sdk::Error, "WebSocket error: #{e.}" end end # Wait for completion completion_queue.pop end |
#list_pty_sessions ⇒ Array<DaytonaApiClient::PtySessionInfo>
Lists all PTY sessions in the Sandbox
560 561 562 563 564 565 566 567 |
# File 'lib/daytona/process.rb', line 560 def list_pty_sessions response = toolbox_api.list_pty_sessions return [] if response.nil? response.respond_to?(:sessions) ? (response.sessions || []) : response rescue *Sdk::API_ERROR_CLASSES => e raise Sdk.wrap_error(e, 'Failed to list PTY sessions') end |
#list_sessions ⇒ Array<DaytonaApiClient::Session>
Returns List of all sessions in the Sandbox.
391 392 393 394 395 |
# File 'lib/daytona/process.rb', line 391 def list_sessions toolbox_api.list_sessions rescue *Sdk::API_ERROR_CLASSES => e raise Sdk.wrap_error(e, 'Failed to list sessions') end |
#resize_pty_session(session_id, pty_size) ⇒ DaytonaApiClient::PtySessionInfo
Resizes a PTY session to the specified dimensions
526 527 528 529 530 531 532 533 534 535 536 |
# File 'lib/daytona/process.rb', line 526 def resize_pty_session(session_id, pty_size) toolbox_api.resize_pty_session( session_id, DaytonaToolboxApiClient::PtyResizeRequest.new( cols: pty_size.cols, rows: pty_size.rows ) ) rescue *Sdk::API_ERROR_CLASSES => e raise Sdk.wrap_error(e, 'Failed to resize PTY session') end |
#send_session_command_input(session_id:, command_id:, data:) ⇒ void
This method returns an undefined value.
Sends input data to a command executed in a session
This method allows you to send input to an interactive command running in a session, such as responding to prompts or providing data to stdin.
372 373 374 375 376 377 378 379 380 |
# File 'lib/daytona/process.rb', line 372 def send_session_command_input(session_id:, command_id:, data:) toolbox_api.send_input( session_id, command_id, DaytonaToolboxApiClient::SessionSendInputRequest.new(data:) ) rescue *Sdk::API_ERROR_CLASSES => e raise Sdk.wrap_error(e, 'Failed to send session command input') end |