Module: Sensu::Redis::Commands

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

Overview

Sensu Module for requesting Redis commands, intended to be included by Sensu::Redis::Client.

You can read about RESP @ redis.io/topics/protocol

Instance Method Summary collapse

Instance Method Details

#auth_and_select_db(password = nil, db = nil) ⇒ Object

Authenticate to Redis and select the correct DB, when applicable. The auth and select Redis commands must be the first commands (& callbacks) to run.

Parameters:

  • password (String) (defaults to: nil)
  • db (Integer) (defaults to: nil)


55
56
57
58
59
60
61
# File 'lib/sensu/redis/commands.rb', line 55

def auth_and_select_db(password=nil, db=nil)
  callbacks = @callbacks || []
  @callbacks = []
  send_command(AUTH_COMMAND, password) if password
  send_command(SELECT_COMMAND, db) if db
  callbacks.each { |block| callback(&block) }
end

#create_command_methods!Object

Create Redis command methods. Command methods just wrap ‘send_command()` and enqueue a response callback. This method MUST be called in the connection object’s ‘initialize()`.



39
40
41
42
43
44
45
46
47
# File 'lib/sensu/redis/commands.rb', line 39

def create_command_methods!
  @response_callbacks ||= []
  REDIS_COMMANDS.each do |command|
    self.class.send(:define_method, command.to_sym) do |*arguments, &block|
      send_command(command, *arguments)
      @response_callbacks << [RESPONSE_PROCESSORS[command], block]
    end
  end
end

#get_size(string) ⇒ Integer

Determine the byte size of a string.

Parameters:

  • string (String)

Returns:

  • (Integer)

    string byte size.



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

def get_size(string)
  string.respond_to?(:bytesize) ? string.bytesize : string.size
end

#send_command(*arguments) ⇒ Object

Send a Redis command using RESP multi bulk. This method is called by the Redis command methods, which are created by ‘create_command_methods()`, it simply implements RESP and sends commands to Redis via EM connection `send_data()`.



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/sensu/redis/commands.rb', line 23

def send_command(*arguments)
  command = "*#{arguments.size}#{DELIM}"
  arguments.each do |value|
    value = value.to_s
    command << "$#{get_size(value)}#{DELIM}#{value}#{DELIM}"
  end
  if @deferred_status
    send_data(command)
  else
    callback { send_data(command) }
  end
end

#subscribe(channel, &block) ⇒ Object

Subscribe to a Redis PubSub channel.

Parameters:

  • channel (String)


66
67
68
69
70
# File 'lib/sensu/redis/commands.rb', line 66

def subscribe(channel, &block)
  @pubsub_callbacks ||= Hash.new([])
  @pubsub_callbacks[channel] << block
  send_command(SUBSCRIBE_COMMAND, channel, &block)
end

#unsubscribe(channel = nil, &block) ⇒ Object

Unsubscribe to one or more Redis PubSub channels. If a channel is provided, this method will unsubscribe from it. If a channel is not provided, this method will unsubscribe from all Redis PubSub channels.

Parameters:

  • channel (String) (defaults to: nil)


78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/sensu/redis/commands.rb', line 78

def unsubscribe(channel=nil, &block)
  @pubsub_callbacks ||= Hash.new([])
  arguments = [UNSUBSCRIBE_COMMAND]
  if channel
    @pubsub_callbacks[channel] = [block]
    arguments << channel
  else
    @pubsub_callbacks.each_key do |key|
      @pubsub_callbacks[key] = [block]
    end
  end
  send_command(arguments)
end