Class: Fleck::Client::Request

Inherits:
Object
  • Object
show all
Includes:
Loggable
Defined in:
lib/fleck/client/request.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Loggable

#logger

Constructor Details

#initialize(exchange, routing_key, reply_to, headers = {}, params = {}, &callback) ⇒ Request

Returns a new instance of Request.



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

def initialize(exchange, routing_key, reply_to, headers = {}, params = {}, &callback)
  @id              = SecureRandom.uuid
  logger.progname += " #{@id}"

  logger.debug "Preparing new request"

  @exchange    = exchange
  @routing_key = routing_key
  @reply_to    = reply_to
  @params      = params
  @headers     = headers
  @response    = nil
  @lock        = Mutex.new
  @condition   = ConditionVariable.new
  @callback    = callback
  @started_at  = nil
  @ended_at    = nil

  logger.debug "Request prepared"
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



6
7
8
# File 'lib/fleck/client/request.rb', line 6

def id
  @id
end

#responseObject

Returns the value of attribute response.



6
7
8
# File 'lib/fleck/client/request.rb', line 6

def response
  @response
end

Instance Method Details

#complete!Object



49
50
51
52
53
# File 'lib/fleck/client/request.rb', line 49

def complete!
  @lock.synchronize { @condition.signal }
  @ended_at = Time.now.to_f
  logger.debug "Done in #{((@ended_at - @started_at).round(5) * 1000).round(2)} ms"
end

#send!(async = false) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/fleck/client/request.rb', line 37

def send!(async = false)
  @started_at = Time.now.to_f
  data = Oj.dump({
    headers: @headers,
    params:  @params
  }, mode: :compat)
  logger.debug("Sending request with data: #{data}")

  @exchange.publish(data, routing_key: @routing_key, reply_to: @reply_to, correlation_id: @id)
  @lock.synchronize { @condition.wait(@lock) } unless async
end