Class: Modal::SandboxOutputStream

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

Instance Method Summary collapse

Constructor Details

#initialize(sandbox_id, file_descriptor) ⇒ SandboxOutputStream

Returns a new instance of SandboxOutputStream.



196
197
198
199
200
201
202
# File 'lib/modal/sandbox.rb', line 196

def initialize(sandbox_id, file_descriptor)
  @sandbox_id = sandbox_id
  @file_descriptor = file_descriptor
  @last_entry_id = ""
  @data_collected = []
  @finished = false
end

Instance Method Details

#eachObject



204
205
206
207
208
209
210
211
212
213
214
215
216
217
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
# File 'lib/modal/sandbox.rb', line 204

def each
  return enum_for(:each) unless block_given?

  return if @finished

  # make one call and collect all data until EOF
  request = Modal::Client::SandboxGetLogsRequest.new(
    sandbox_id: @sandbox_id,
    file_descriptor: @file_descriptor,
    timeout: 10, # Give it more time to get all the data
    last_entry_id: @last_entry_id
  )

  begin
    resp = Modal.client.call(:sandbox_get_logs, request)

    # Process the entire streaming response
    resp.each do |batch|
      # Update last_entry_id
      if batch.respond_to?(:entry_id) && batch.entry_id && !batch.entry_id.empty?
        @last_entry_id = batch.entry_id
      end

      # Collect data from this batch
      if batch.respond_to?(:items) && batch.items
        batch.items.each do |item|
          if item.respond_to?(:data) && item.data && !item.data.empty?
            @data_collected << item.data
          end
        end
      end

      # Check for EOF
      if batch.respond_to?(:eof) && batch.eof
        @finished = true
        break
      end
    end
  rescue GRPC::BadStatus => e
    if e.code == GRPC::Core::StatusCodes::DEADLINE_EXCEEDED
      @finished = true
    else
      raise e
    end
  end

  # Yield all collected data
  @data_collected.each { |data| yield data }
end