Module: Nucleus::Adapters::HttpTailClient

Included in:
BaseAdapter
Defined in:
lib/nucleus/core/adapter_extensions/http_tail_client.rb

Instance Method Summary collapse

Instance Method Details

#tail_http_response(url, api_stream, http_method = :get) ⇒ Object

Executes a request to the given URL and expects a streaming response.
Each new chunk (usually lines) will be forwarded to the client via the api_stream.

Parameters:

  • url (String)

    url to call

  • api_stream (Nucleus::API::StreamCallback)

    stream to which new chunks will be forwarded

  • http_method (Symbol) (defaults to: :get)

    HTTP method to use



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/nucleus/core/adapter_extensions/http_tail_client.rb', line 10

def tail_http_response(url, api_stream, http_method = :get)
  http_connection = EventMachine::HttpRequest.new(url, inactivity_timeout: 0)
  http_client = http_connection.send(http_method, keepalive: true)

  # close stream on error
  http_client.on_error do
    log.debug('HttpTailClient detected an error, close stream...')
    api_stream.close
  end
  # tail and immediately push the results to the stream
  http_client.stream { |chunk| api_stream.send_message(chunk) }
  # return object that responds to :stop and cancels the tailing request
  TailStopper.new(http_connection, :close)
end