Module: Sensu::Redis::Connection

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

Overview

Sensu Module connecting to Redis.

Instance Method Summary collapse

Instance Method Details

#after_reconnect(&block) ⇒ Object

Set the after reconnect callback.



36
37
38
# File 'lib/sensu/redis/connection.rb', line 36

def after_reconnect(&block)
  @reconnect_callbacks[:after] = block
end

#before_reconnect(&block) ⇒ Object

Set the before reconnect callback.



31
32
33
# File 'lib/sensu/redis/connection.rb', line 31

def before_reconnect(&block)
  @reconnect_callbacks[:before] = block
end

#closeObject

Close the Redis connection after writing the current command/data.



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

def close
  @closing = true
  close_connection_after_writing
end

#connected?Boolean

Determine if connected to Redis.

Returns:

  • (Boolean)


50
51
52
# File 'lib/sensu/redis/connection.rb', line 50

def connected?
  @connected || false
end

#connection_completedObject

Called by EM when the connection is established.



102
103
104
105
106
107
108
109
# File 'lib/sensu/redis/connection.rb', line 102

def connection_completed
  @connected = true
  auth_and_select_db(@password, @db)
  validate_connection!
  @reconnect_callbacks[:after].call if @reconnecting
  @reconnecting = false
  succeed
end

#error(klass, message) ⇒ Object

Create an error and pass it to the error callback.

Parameters:

  • klass (Class)
  • message (String)


44
45
46
47
# File 'lib/sensu/redis/connection.rb', line 44

def error(klass, message)
  redis_error = klass.new(message)
  @error_callback.call(redis_error)
end

#initialize(options = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/sensu/redis/connection.rb', line 8

def initialize(options={})
  create_command_methods!
  @host = options[:host]
  @port = options[:port]
  @db = (options[:db] || 0).to_i
  @password = options[:password]
  @auto_reconnect = options.fetch(:auto_reconnect, true)
  @reconnect_on_error = options.fetch(:reconnect_on_error, true)
  @error_callback = lambda do |error|
    raise(error)
  end
  @reconnect_callbacks = {
    :before => lambda{},
    :after  => lambda{}
  }
end

#on_error(&block) ⇒ Object

Set the on error callback.



26
27
28
# File 'lib/sensu/redis/connection.rb', line 26

def on_error(&block)
  @error_callback = block
end

#reconnect!Object

Reconnect to Redis. The before reconnect callback is first call if not already reconnecting. This method uses a 1 second delay before attempting a reconnect.



57
58
59
60
61
62
63
# File 'lib/sensu/redis/connection.rb', line 57

def reconnect!
  @reconnect_callbacks[:before].call unless @reconnecting
  @reconnecting = true
  EM.add_timer(1) do
    reconnect(@host, @port)
  end
end

#unbindObject

Called by EM when the connection closes, either intentionally or unexpectedly.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/sensu/redis/connection.rb', line 74

def unbind
  @deferred_status = nil
  @response_callbacks = []
  @multibulk_count = false
  if @closing
    @reconnecting = false
  elsif ((@connected || @reconnecting) && @auto_reconnect) || @reconnect_on_error
    reconnect!
  elsif @connected
    error(ConnectionError, "connection closed")
  else
    error(ConnectionError, "unable to connect to redis server")
  end
  @connected = false
end

#validate_connection!Object

Validate the connection, ensuring that the Redis release supports Sensu’s required Redis commands. A connection error is thrown if Redis’s version does not meet the requirement.



93
94
95
96
97
98
99
# File 'lib/sensu/redis/connection.rb', line 93

def validate_connection!
  info do |redis_info|
    if redis_info[:redis_version] < "1.3.14"
      error(ConnectionError, "redis version must be >= 2.0 RC 1")
    end
  end
end