module Sensu::Redis

Constants

ASTERISK
AUTH_COMMAND

Redis commands.

BOOLEAN_PROCESSOR

Boolean Redis response value processor.

COLON
COMMANDS

Redis commands that are supported by this library.

DELIM
DOLLAR
INCOMPLETE

Redis response line parser incomplete data return value.

INFO_COMMAND
MINUS
OK

RESP (REdis Serialization Protocol) response type characters and delimiter (redis.io/topics/protocol).

PLUS
PUBSUB_RESPONSES

Redis pubsub response type values.

RESPONSE_PROCESSORS

Redis response value processors.

SELECT_COMMAND
SUBSCRIBE_COMMAND
TRUE_VALUES

Redis response boolean values.

UNSUBSCRIBE_COMMAND

Public Class Methods

connect(options={}, &block) click to toggle source

Connect to Redis using the provided connection options.

@param options [String,Hash] @yield callback to be called with the redis connection object.

# File lib/sensu/redis.rb, line 83
def connect(options={}, &block)
  case options
  when String
    options = parse_url(options)
  when nil
    options = {}
  end
  options[:host] ||= "127.0.0.1"
  options[:port] ||= 6379
  case
  when options[:sentinels].is_a?(String)
    raw_urls = options[:sentinels]
    options[:sentinels] = raw_urls.split(',').map { |url| parse_url(url) }
  when options[:sentinels].is_a?(Array) && options[:sentinels].length > 0
    connect_via_sentinel(options, &block)
  else
    connect_direct(options, &block)
  end
end
connect_direct(options, &block) click to toggle source

Connect to the current Redis master directly, using the provided connection options. This method uses `resolve_host()` to first resolve the provided host, if it's not already an IP address. Resolving the hostname upfront guards against lookup failures that would cause the Sensu process to crash. Upfront hostname resolution also allows this Redis library to work with Amazon AWS ElastiCache & where DNS is used as a failover mechanism.

@param options [Hash] @yield callback to be called with the redis connection object.

# File lib/sensu/redis.rb, line 65
def connect_direct(options, &block)
  resolve_host(options[:host]) do |ip_address|
    if ip_address.nil?
      EM::Timer.new(1) do
        connect_direct(options, &block)
      end
    else
      redis = EM.connect(ip_address, options[:port], Client, options)
      redis.logger = @logger
      block.call(redis)
    end
  end
end
connect_via_sentinel(options, &block) click to toggle source

Connect to the current Redis master via Sentinel. Sentinel will resolve the current Redis master host and port.

@param options [Hash] @yield callback to be called with the redis connection object.

# File lib/sensu/redis.rb, line 43
def connect_via_sentinel(options, &block)
  sentinel = Sentinel.new(options)
  sentinel.logger = @logger
  sentinel.resolve do |host, port|
    redis = EM.connect(host, port, Client, options)
    redis.logger = @logger
    redis.sentinel = sentinel
    block.call(redis)
  end
end
logger=(logger) click to toggle source

Set the Redis logger.

@param logger [Object] Redis logger.

# File lib/sensu/redis.rb, line 16
def logger=(logger)
  @logger = logger
end
parse_url(url) click to toggle source

Parse a Redis URL. An argument error exception is thrown if this method is unable to parse the provided URL string.

@param url [String] @return [Hash] Redis connection options.

# File lib/sensu/redis.rb, line 25
def parse_url(url)
  begin
    uri = URI.parse(url)
    {
      :host => uri.host,
      :port => uri.port,
      :password => uri.password
    }
  rescue
    raise ArgumentError, "invalid redis url"
  end
end