Class: Modal::Sandbox
- Inherits:
-
Object
- Object
- Modal::Sandbox
- Defined in:
- lib/modal/sandbox.rb
Instance Attribute Summary collapse
-
#sandbox_id ⇒ Object
readonly
Returns the value of attribute sandbox_id.
-
#stderr ⇒ Object
readonly
Returns the value of attribute stderr.
-
#stdin ⇒ Object
readonly
Returns the value of attribute stdin.
-
#stdout ⇒ Object
readonly
Returns the value of attribute stdout.
-
#task_id ⇒ Object
Returns the value of attribute task_id.
Instance Method Summary collapse
- #exec(command, options = {}) ⇒ Object
-
#initialize(sandbox_id) ⇒ Sandbox
constructor
A new instance of Sandbox.
- #open(path, mode) ⇒ Object
- #terminate ⇒ Object
- #wait ⇒ Object
Constructor Details
#initialize(sandbox_id) ⇒ Sandbox
Returns a new instance of Sandbox.
10 11 12 13 14 15 16 17 |
# File 'lib/modal/sandbox.rb', line 10 def initialize(sandbox_id) @sandbox_id = sandbox_id @task_id = nil @stdin = ModalWriteStream.new(SandboxInputStream.new(sandbox_id)) @stdout = ModalReadStream.new(SandboxOutputStream.new(sandbox_id, Modal::Client::FileDescriptor::FILE_DESCRIPTOR_STDOUT)) @stderr = ModalReadStream.new(SandboxOutputStream.new(sandbox_id, Modal::Client::FileDescriptor::FILE_DESCRIPTOR_STDERR)) end |
Instance Attribute Details
#sandbox_id ⇒ Object (readonly)
Returns the value of attribute sandbox_id.
7 8 9 |
# File 'lib/modal/sandbox.rb', line 7 def sandbox_id @sandbox_id end |
#stderr ⇒ Object (readonly)
Returns the value of attribute stderr.
7 8 9 |
# File 'lib/modal/sandbox.rb', line 7 def stderr @stderr end |
#stdin ⇒ Object (readonly)
Returns the value of attribute stdin.
7 8 9 |
# File 'lib/modal/sandbox.rb', line 7 def stdin @stdin end |
#stdout ⇒ Object (readonly)
Returns the value of attribute stdout.
7 8 9 |
# File 'lib/modal/sandbox.rb', line 7 def stdout @stdout end |
#task_id ⇒ Object
Returns the value of attribute task_id.
8 9 10 |
# File 'lib/modal/sandbox.rb', line 8 def task_id @task_id end |
Instance Method Details
#exec(command, options = {}) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/modal/sandbox.rb', line 19 def exec(command, = {}) ensure_task_id workdir = [:workdir] timeout_secs = [:timeout] ? [:timeout] / 1000 : 0 request = Modal::Client::ContainerExecRequest.new( task_id: @task_id, command: command, workdir: workdir, timeout_secs: timeout_secs ) resp = Modal.client.call(:container_exec, request) ContainerProcess.new(resp.exec_id, "text") end |
#open(path, mode) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/modal/sandbox.rb', line 36 def open(path, mode) ensure_task_id request = Modal::Client::ContainerFilesystemExecRequest.new( file_open_request: Modal::Client::ContainerFileOpenRequest.new( path: path, mode: mode ), task_id: @task_id ) resp = run_filesystem_exec(request) SandboxFile.new(resp.response.file_open_response.file_descriptor, @task_id) end |
#terminate ⇒ Object
50 51 52 53 |
# File 'lib/modal/sandbox.rb', line 50 def terminate request = Modal::Client::SandboxTerminateRequest.new(sandbox_id: @sandbox_id) Modal.client.call(:sandbox_terminate, request) end |
#wait ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/modal/sandbox.rb', line 55 def wait loop do request = Modal::Client::SandboxWaitRequest.new( sandbox_id: @sandbox_id, timeout: 55 # seconds ) resp = Modal.client.call(:sandbox_wait, request) if resp.completed return resp.exit_code || 0 end sleep(1) # Poll every second end end |