Class: RedisUtil::Factory

Inherits:
Object
  • Object
show all
Extended by:
MonitorMixin
Includes:
GemLogger::LoggerSupport
Defined in:
lib/redis_util/factory.rb

Overview

A factory class for creating redis connections, useful for reconnecting them after a fork

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.clientsObject

Returns the value of attribute clients.



23
24
25
# File 'lib/redis_util/factory.rb', line 23

def clients
  @clients
end

.config_fileObject

Set the path to the yml config file which defines all the redis connections. If running in rails, it will default to “#Rails.root/config/redis.yml”



17
18
19
# File 'lib/redis_util/factory.rb', line 17

def config_file
  @config_file
end

.configurationObject

Returns the value of attribute configuration.



23
24
25
# File 'lib/redis_util/factory.rb', line 23

def configuration
  @configuration
end

.envObject

Set the environment we are running in (development/test/etc). If running in rails, it will default to Rails.env



21
22
23
# File 'lib/redis_util/factory.rb', line 21

def env
  @env
end

Class Method Details

.connect(name) ⇒ Redis

Creates/retrieves a single redis client for the given named configuration

Parameters:

  • name (Symbol)

    The name of the redis configuration (config/redis.yml) to use

Returns:

  • (Redis)

    A redis client object



29
30
31
32
33
34
35
36
37
38
# File 'lib/redis_util/factory.rb', line 29

def connect(name)
  conf = lookup_config(name)
  synchronize do
    clients[name] ||= []
    if clients[name].first.nil?
      clients[name] << new_redis_client(conf)
    end
  end
  clients[name].first
end

.create(name) ⇒ Redis

Always create and return a new redis client for the given named configuration

Parameters:

  • name (Symbol)

    The name of the redis configuration (config/redis.yml) to use

Returns:

  • (Redis)

    A redis client object



44
45
46
47
48
49
50
51
# File 'lib/redis_util/factory.rb', line 44

def create(name)
  conf = lookup_config(name)
  synchronize do
    clients[name] ||= []
    clients[name] << new_redis_client(conf)
  end
  clients[name].last
end

.disconnect(key = nil) ⇒ Object

Disconnect all known redis clients

Parameters:

  • key (Symbol) (defaults to: nil)

    (optional, defaults to all) The name of the redis configuration (config/redis.yml) to disconnect



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/redis_util/factory.rb', line 56

def disconnect(key=nil)
  logger.debug "RedisUtil::Factory.disconnect start"
  synchronize do
    clients.clone.each do |name, client|
      next if key && name != key
    
      connections = clients.delete(name) || []
      connections.each do |connection|
        begin
          logger.debug "Disconnecting Redis client: #{connection}"
          connection.quit
        rescue => e
          logger.warn("Exception while disconnecting: #{e}")
        end
      end
    end
  end
  logger.debug "RedisUtil::Factory.disconnect complete"
  nil
end

.reconnect(key = nil) ⇒ Object

Reconnect all known redis clients

Parameters:

  • key (Symbol) (defaults to: nil)

    (optional, defaults to all) The name of the redis configuration (config/redis.yml) to reconnect



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/redis_util/factory.rb', line 80

def reconnect(key=nil)
  logger.debug "RedisUtil::Factory.reconnect start"
  synchronize do
    clients.each do |name, connections|
      next if key && name != key
      
      connections.each do |connection|
        logger.debug "Reconnecting Redis client: #{connection}"
        connection.client.reconnect
      end
    end
  end
  logger.debug "RedisUtil::Factory.reconnect complete"    
end