Class: Daytona::Process
- Inherits:
-
Object
- Object
- Daytona::Process
- Defined in:
- lib/daytona/process.rb
Overview
rubocop:disable Metrics/ClassLength
Defined Under Namespace
Modules: ArtifactType
Instance Attribute Summary collapse
-
#code_toolbox ⇒ Daytona::SandboxPythonCodeToolbox,
readonly
Daytona::SandboxPythonCodeToolbox,.
-
#get_preview_link ⇒ Proc
readonly
Function to get preview link for a port.
-
#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_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(code_toolbox:, sandbox_id:, toolbox_api:, get_preview_link:) ⇒ 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.
Constructor Details
#initialize(code_toolbox:, sandbox_id:, toolbox_api:, get_preview_link:) ⇒ Process
Initialize a new Process instance
27 28 29 30 31 32 |
# File 'lib/daytona/process.rb', line 27 def initialize(code_toolbox:, sandbox_id:, toolbox_api:, get_preview_link:) @code_toolbox = code_toolbox @sandbox_id = sandbox_id @toolbox_api = toolbox_api @get_preview_link = get_preview_link end |
Instance Attribute Details
#code_toolbox ⇒ Daytona::SandboxPythonCodeToolbox, (readonly)
Returns Daytona::SandboxPythonCodeToolbox,.
10 11 12 |
# File 'lib/daytona/process.rb', line 10 def code_toolbox @code_toolbox end |
#get_preview_link ⇒ Proc (readonly)
Returns Function to get preview link for a port.
19 20 21 |
# File 'lib/daytona/process.rb', line 19 def get_preview_link @get_preview_link end |
#sandbox_id ⇒ String (readonly)
Returns The ID of the Sandbox.
13 14 15 |
# File 'lib/daytona/process.rb', line 13 def sandbox_id @sandbox_id end |
#toolbox_api ⇒ DaytonaToolboxApiClient::ProcessApi (readonly)
Returns API client for Sandbox operations.
16 17 18 |
# File 'lib/daytona/process.rb', line 16 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
92 93 94 |
# File 'lib/daytona/process.rb', line 92 def code_run(code:, params: nil, timeout: nil) exec(command: code_toolbox.get_run_command(code, params), env: params&.env, timeout:) 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.
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 |
# File 'lib/daytona/process.rb', line 350 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" PtyHandle.new( WebSocket::Client::Simple.connect( url.to_s, headers: toolbox_api.api_client.default_headers.dup.merge( 'X-Daytona-Preview-Token' => preview_link.token ) ), session_id:, handle_resize: ->(pty_size) { resize_pty_session(session_id, pty_size) }, handle_kill: -> { delete_pty_session(session_id) } ).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.
318 319 320 321 322 323 324 325 326 327 328 329 330 331 |
# File 'lib/daytona/process.rb', line 318 def create_pty_session(id:, cwd: nil, envs: nil, pty_size: nil) # rubocop:disable Metrics/MethodLength response = toolbox_api.create_pty_session( DaytonaToolboxApiClient::PtyCreateRequest.new( id:, cwd:, envs:, cols: pty_size&.cols, rows: pty_size&.rows, lazy_start: true ) ) connect_pty_session(response.session_id) 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.
111 112 113 |
# File 'lib/daytona/process.rb', line 111 def create_session(session_id) toolbox_api.create_session(DaytonaToolboxApiClient::CreateSessionRequest.new(session_id:)) end |
#delete_pty_session(session_id) ⇒ void
This method returns an undefined value.
Deletes a PTY session, terminating the associated process
397 398 399 |
# File 'lib/daytona/process.rb', line 397 def delete_pty_session(session_id) toolbox_api.delete_pty_session(session_id) end |
#delete_session(session_id) ⇒ Object
Terminates and removes a session from the Sandbox, cleaning up any resources associated with it
283 |
# File 'lib/daytona/process.rb', line 283 def delete_session(session_id) = toolbox_api.delete_session(session_id) |
#exec(command:, cwd: nil, env: nil, timeout: nil) ⇒ ExecuteResponse
Execute a shell command in the Sandbox
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/daytona/process.rb', line 53 def exec(command:, cwd: nil, env: nil, timeout: nil) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength command = "echo '#{Base64.encode64(command)}' | base64 -d | sh" if env && !env.empty? safe_env_exports = env.map do |key, value| "export #{key}=$(echo '#{Base64.encode64(value)}' | base64 -d)" end.join(';') command = "#{safe_env_exports}; #{command}" end command = "sh -c \"#{command}\"" response = toolbox_api.execute_command(DaytonaToolboxApiClient::ExecuteRequest.new(command:, cwd:, timeout:)) # Post-process the output to extract ExecutionArtifacts artifacts = parse_output(response.result.split("\n")) # Create new response with processed output and charts ExecuteResponse.new( exit_code: response.exit_code, result: artifacts.stdout, artifacts: artifacts ) end |
#execute_session_command(session_id:, req:) ⇒ Daytona::SessionExecuteResponse
Executes a command in the session
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/daytona/process.rb', line 165 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) ) stdout, stderr = Util.demux(response.output || '') SessionExecuteResponse.new( cmd_id: response.cmd_id, output: response.output, stdout:, stderr:, exit_code: response.exit_code, # TODO: DaytonaApiClient::SessionExecuteResponse doesn't have additional_properties attribute additional_properties: {} ) 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.
430 431 432 |
# File 'lib/daytona/process.rb', line 430 def get_pty_session_info(session_id) toolbox_api.get_pty_session(session_id) end |
#get_session(session_id) ⇒ DaytonaApiClient::Session
Gets a session in the Sandbox
125 |
# File 'lib/daytona/process.rb', line 125 def get_session(session_id) = toolbox_api.get_session(session_id) |
#get_session_command(session_id:, command_id:) ⇒ DaytonaApiClient::Command
Gets information about a specific command executed in a session
138 139 140 |
# File 'lib/daytona/process.rb', line 138 def get_session_command(session_id:, command_id:) toolbox_api.get_session_command(session_id, command_id) end |
#get_session_command_logs(session_id:, command_id:) ⇒ Daytona::SessionCommandLogsResponse
Get the logs for a command executed in a session
194 195 196 197 198 199 200 201 |
# File 'lib/daytona/process.rb', line 194 def get_session_command_logs(session_id:, command_id:) parse_session_command_logs( toolbox_api.get_session_command_logs( session_id, command_id ) ) 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
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'lib/daytona/process.rb', line 218 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 ws = 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' ) ) ws.on(:message) do || if .type == :close ws.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 ws.on(:close) do completion_queue.push(:close) end ws.on(:error) do |e| completion_queue.push(:error) raise Sdk::Error, "WebSocket error: #{e.}" end # Wait for completion completion_queue.pop end |
#list_pty_sessions ⇒ Array<DaytonaApiClient::PtySessionInfo>
Lists all PTY sessions in the Sandbox
410 411 412 |
# File 'lib/daytona/process.rb', line 410 def list_pty_sessions toolbox_api.list_pty_sessions end |
#list_sessions ⇒ Array<DaytonaApiClient::Session>
Returns List of all sessions in the Sandbox.
270 |
# File 'lib/daytona/process.rb', line 270 def list_sessions = toolbox_api.list_sessions |
#resize_pty_session(session_id, pty_size) ⇒ DaytonaApiClient::PtySessionInfo
Resizes a PTY session to the specified dimensions
380 381 382 383 384 385 386 387 388 |
# File 'lib/daytona/process.rb', line 380 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 ) ) end |