Class: Modal::SandboxFile

Inherits:
Object
  • Object
show all
Defined in:
lib/modal/sandbox_filesystem.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_descriptor, task_id) ⇒ SandboxFile

Returns a new instance of SandboxFile.



5
6
7
8
# File 'lib/modal/sandbox_filesystem.rb', line 5

def initialize(file_descriptor, task_id)
  @file_descriptor = file_descriptor
  @task_id = task_id
end

Instance Attribute Details

#file_descriptorObject (readonly)

Returns the value of attribute file_descriptor.



3
4
5
# File 'lib/modal/sandbox_filesystem.rb', line 3

def file_descriptor
  @file_descriptor
end

#task_idObject (readonly)

Returns the value of attribute task_id.



3
4
5
# File 'lib/modal/sandbox_filesystem.rb', line 3

def task_id
  @task_id
end

Instance Method Details

#closeObject



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/modal/sandbox_filesystem.rb', line 179

def close
  request = Modal::Client::ContainerFilesystemExecRequest.new(
    file_close_request: Modal::Client::ContainerFileCloseRequest.new(
      file_descriptor: @file_descriptor
    ),
    task_id: @task_id
  )

  resp = Modal.client.call(:container_filesystem_exec, request)
  exec_id = resp.exec_id

  retries = 10

  while retries > 0
    begin
      output_request = Modal::Client::ContainerFilesystemExecGetOutputRequest.new(
        exec_id: exec_id,
        timeout: 5
      )

      stream = Modal.client.call(:container_filesystem_exec_get_output, output_request)
      stream.each do |batch|
        if batch.respond_to?(:error) && batch.error
          raise SandboxFilesystemError.new("Close failed: #{batch.error.error_message}")
        end
        if batch.respond_to?(:eof) && batch.eof
          return
        end
      end

      retries -= 1
      sleep(0.1)
    rescue GRPC::BadStatus => e
      if e.code == GRPC::Core::StatusCodes::DEADLINE_EXCEEDED
        retries -= 1
        next
      else
        raise e
      end
    end
  end
end

#flushObject



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/modal/sandbox_filesystem.rb', line 136

def flush
  request = Modal::Client::ContainerFilesystemExecRequest.new(
    file_flush_request: Modal::Client::ContainerFileFlushRequest.new(
      file_descriptor: @file_descriptor
    ),
    task_id: @task_id
  )

  resp = Modal.client.call(:container_filesystem_exec, request)
  exec_id = resp.exec_id

  retries = 10

  while retries > 0
    begin
      output_request = Modal::Client::ContainerFilesystemExecGetOutputRequest.new(
        exec_id: exec_id,
        timeout: 5
      )

      stream = Modal.client.call(:container_filesystem_exec_get_output, output_request)
      stream.each do |batch|
        if batch.respond_to?(:error) && batch.error
          raise SandboxFilesystemError.new("Flush failed: #{batch.error.error_message}")
        end
        if batch.respond_to?(:eof) && batch.eof
          return
        end
      end

      retries -= 1
      sleep(0.1)
    rescue GRPC::BadStatus => e
      if e.code == GRPC::Core::StatusCodes::DEADLINE_EXCEEDED
        retries -= 1
        next
      else
        raise e
      end
    end
  end
end

#readObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/modal/sandbox_filesystem.rb', line 10

def read
  request = Modal::Client::ContainerFilesystemExecRequest.new(
    file_read_request: Modal::Client::ContainerFileReadRequest.new(
      file_descriptor: @file_descriptor
    ),
    task_id: @task_id
  )

  resp = Modal.client.call(:container_filesystem_exec, request)
  exec_id = resp.exec_id

  data = ""
  completed = false
  retries = 20

  while !completed && retries > 0
    begin
      output_request = Modal::Client::ContainerFilesystemExecGetOutputRequest.new(
        exec_id: exec_id,
        timeout: 10
      )

      stream = Modal.client.call(:container_filesystem_exec_get_output, output_request)

      stream.each do |batch|
        if batch.respond_to?(:output) && batch.output && batch.output.any?
          chunk = if batch.output.first.is_a?(String)
            batch.output.join("")
          elsif batch.output.first.is_a?(Integer)
            batch.output.pack("C*")
          else
            batch.output.map(&:to_s).join("")
          end

          data += chunk.force_encoding("UTF-8")
        end

        if batch.respond_to?(:error) && batch.error
          raise SandboxFilesystemError.new("Read failed: #{batch.error.error_message}")
        end

        if batch.respond_to?(:eof) && batch.eof
          completed = true
          break
        end
      end

      unless completed
        retries -= 1
        sleep(0.1)
      end
    rescue GRPC::BadStatus => e
      if e.code == GRPC::Core::StatusCodes::DEADLINE_EXCEEDED
        retries -= 1
        sleep(0.1)
        next
      else
        raise e
      end
    end
  end

  data
end

#write(data) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/modal/sandbox_filesystem.rb', line 75

def write(data)
  binary_data = if data.is_a?(String)
    data.force_encoding("BINARY")
  else
    data.to_s.force_encoding("BINARY")
  end

  request = Modal::Client::ContainerFilesystemExecRequest.new(
    file_write_request: Modal::Client::ContainerFileWriteRequest.new(
      file_descriptor: @file_descriptor,
      data: binary_data
    ),
    task_id: @task_id
  )

  resp = Modal.client.call(:container_filesystem_exec, request)
  exec_id = resp.exec_id

  completed = false
  retries = 20

  while !completed && retries > 0
    begin
      output_request = Modal::Client::ContainerFilesystemExecGetOutputRequest.new(
        exec_id: exec_id,
        timeout: 10
      )

      stream = Modal.client.call(:container_filesystem_exec_get_output, output_request)

      stream.each do |batch|
        if batch.respond_to?(:error) && batch.error
          raise SandboxFilesystemError.new("Write failed: #{batch.error.error_message}")
        end
        if batch.respond_to?(:eof) && batch.eof
          completed = true
          break
        end
      end

      unless completed
        retries -= 1
        sleep(0.1)
      end
    rescue GRPC::BadStatus => e
      if e.code == GRPC::Core::StatusCodes::DEADLINE_EXCEEDED
        retries -= 1
        sleep(0.1)
        next
      else
        raise e
      end
    end
  end

  unless completed
  end

  data.length
end