Module: Daytona::Util

Defined in:
lib/daytona/util.rb

Class Method Summary collapse

Class Method Details

.demux(line) ⇒ Object

rubocop:disable Metrics/MethodLength



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/daytona/util.rb', line 7

def self.demux(line) # rubocop:disable Metrics/MethodLength
  stdout = ''.dup
  stderr = ''.dup

  until line.empty?
    buff = line.start_with?(STDOUT_PREFIX) ? stdout : stderr
    line = line[3..]

    end_index = [
      line.index(STDOUT_PREFIX),
      line.index(STDERR_PREFIX)
    ].compact.min || line.length
    data = line[...end_index]
    buff << data

    line = line[end_index..]
  end

  [stdout, stderr]
end

.stream_async(uri:, on_chunk:, headers: nil) ⇒ Thread

Parameters:

  • uri (URI)
  • on_chunk (Proc)
  • headers (Hash<String, String>) (defaults to: nil)

Returns:

  • (Thread)


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/daytona/util.rb', line 32

def self.stream_async(uri:, on_chunk:, headers: nil) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
  Sdk.logger.debug("Starting async stream: #{uri}")
  Thread.new do
    Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
      request = Net::HTTP::Get.new(uri, headers)

      http.request(request) do |response|
        response.read_body do |chunk|
          Sdk.logger.debug("Chunked response received: #{chunk.inspect}")
          on_chunk.call(chunk)
        end
      end
    end
  rescue Net::ReadTimeout => e
    Sdk.logger.debug("Async stream (#{uri}) timeout: #{e.inspect}")
  end
end