Class: Redis::Retry

Inherits:
Object
  • Object
show all
Defined in:
lib/redis/retry.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Retry

Returns a new instance of Retry.



12
13
14
15
16
# File 'lib/redis/retry.rb', line 12

def initialize(options = {})
  @tries = options[:tries] || 3
  @wait  = options[:wait]  || 2
  @redis = options[:redis]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(command, *args, &block) ⇒ Object

Raises:

  • (Errno::ECONNREFUSED)


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/redis/retry.rb', line 24

def method_missing(command, *args, &block)
  try = 1
  while try <= @tries
    begin
      # Dispatch the command to Redis
      return @redis.send(command, *args, &block)
    rescue Errno::ECONNREFUSED
      try += 1
      sleep @wait
    end
  end

  # Ran out of retries
  raise Errno::ECONNREFUSED
end

Instance Attribute Details

#triesObject

The number of times a command will be retried if a connection cannot be made to Redis. If zero, retry forever.



7
8
9
# File 'lib/redis/retry.rb', line 7

def tries
  @tries
end

#waitObject

The number of seconds to wait before retrying a command.



10
11
12
# File 'lib/redis/retry.rb', line 10

def wait
  @wait
end

Instance Method Details

#type(key) ⇒ Object

Ruby defines a now deprecated type method so we need to override it here since it will never hit method_missing.



20
21
22
# File 'lib/redis/retry.rb', line 20

def type(key)
  method_missing(:type, key)
end