Module: Sensu::Redis::Processor

Included in:
Client
Defined in:
lib/sensu/redis/processor.rb

Overview

Sensu Module for processing Redis responses. This module calls methods provided by other Sensu Redis modules:

Sensu::Redis::Connection.error()

Instance Method Summary collapse

Instance Method Details

#begin_multibulk(multibulk_count) ⇒ Object

Begin a multi bulk response array for an expected number of responses. Using this method causes ‘dispatch_response()` to wait until all of the expected responses have been added to the array, before the Redis command reponse callback is called.

Parameters:

  • multibulk_count (Integer)

    number of expected responses.



27
28
29
30
# File 'lib/sensu/redis/processor.rb', line 27

def begin_multibulk(multibulk_count)
  @multibulk_count = multibulk_count
  @multibulk_values = []
end

#dispatch_error(code) ⇒ Object

Dispatch a Redis error, dropping the associated Redis command response callback, and passing a Redis error object to the error callback (if set).

Parameters:

  • code (String)

    Redis error code.



37
38
39
40
# File 'lib/sensu/redis/processor.rb', line 37

def dispatch_error(code)
  fetch_response_callback
  error(CommandError, code)
end

#dispatch_response(value) ⇒ Object

Dispatch a response. If a multi bulk response has begun, this method will build the completed response array before the associated Redis command response callback is called. If one or more pubsub callbacks are defined, the approprate pubsub callbacks are called, provided with the pubsub response. Redis command response callbacks may have an optional processor block, responsible for producing a value with the correct type, e.g. “1” -> true (boolean).

Parameters:

  • value (Object)


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/sensu/redis/processor.rb', line 52

def dispatch_response(value)
  if @multibulk_count
    @multibulk_values << value
    @multibulk_count -= 1
    if @multibulk_count == 0
      value = @multibulk_values
      @multibulk_count = false
    else
      return
    end
  end
  if @pubsub_callbacks && value.is_a?(Array)
    if PUBSUB_RESPONSES.include?(value[0])
      @pubsub_callbacks[value[1]].each do |block|
        block.call(*value) if block
      end
      return
    end
  end
  processor, block = fetch_response_callback
  if block
    value = processor.call(value) if processor
    block.call(value)
  end
end

#fetch_response_callbackArray

Fetch the next Redis command response callback. Response callbacks may include an optional response processor block, i.e. “1” -> true.

Returns:

  • (Array)

    processor, callback.



15
16
17
18
# File 'lib/sensu/redis/processor.rb', line 15

def fetch_response_callback
  @response_callbacks ||= []
  @response_callbacks.shift
end