Class: Kubeclient::Common::WatchStream

Inherits:
Object
  • Object
show all
Defined in:
lib/kubeclient/watch_stream.rb

Overview

HTTP Stream used to watch changes on entities

Instance Method Summary collapse

Constructor Details

#initialize(uri, options) ⇒ WatchStream

Returns a new instance of WatchStream.



7
8
9
10
11
# File 'lib/kubeclient/watch_stream.rb', line 7

def initialize(uri, options)
  @uri = uri
  @http = nil
  @options = options.merge(read_timeout: nil)
end

Instance Method Details

#eachObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/kubeclient/watch_stream.rb', line 13

def each
  @finished = false
  @http = Net::HTTP.start(@uri.host, @uri.port, @options)

  buffer = ''
  request = Net::HTTP::Get.new(@uri)

  @http.request(request) do |response|
    unless response.is_a? Net::HTTPSuccess
      fail KubeException.new(response.code, response.message)
    end
    response.read_body do |chunk|
      buffer << chunk
      while (line = buffer.slice!(/.+\n/))
        yield WatchNotice.new(JSON.parse(line))
      end
    end
  end
rescue Errno::EBADF
  raise unless @finished
end

#finishObject



35
36
37
38
# File 'lib/kubeclient/watch_stream.rb', line 35

def finish
  @finished = true
  @http.finish if !@http.nil? && @http.started?
end