class Redis::Claim::Actions

Attributes

config[R]

Public Class Methods

claim_db!(config) click to toggle source
# File lib/redis/claim/actions.rb, line 14
def self.claim_db!(config)
  actions = self.new(config)
  actions.verify_config!
  actions.claim_db!
end
new(config) click to toggle source
# File lib/redis/claim/actions.rb, line 10
def initialize(config)
  @config = config
end

Public Instance Methods

claim_db!() click to toggle source
# File lib/redis/claim/actions.rb, line 27
def claim_db!
  current_lock = get_db_lock
  if app_name == current_lock
    true
  else
    raise Redis::Claim::DbAlreadyClaimed, "database already claimed by #{current_lock}"
  end
end
verify_config!() click to toggle source
# File lib/redis/claim/actions.rb, line 20
def verify_config!
  check_config_values!
  check_redis_functionality!

  true
end

Protected Instance Methods

check_config_values!() click to toggle source
# File lib/redis/claim/actions.rb, line 38
def check_config_values!
  return raise_configuration_error('app name is missing') unless app_name
  return raise_configuration_error('redis is missing') unless redis

  true
end
check_redis_functionality!() click to toggle source
# File lib/redis/claim/actions.rb, line 45
def check_redis_functionality!
  return raise_configuration_error('redis should respond to get') unless redis.respond_to?(:get)
  return raise_configuration_error('redis should respond to setnx') unless redis.respond_to?(:setnx)

  true
end
get_db_lock() click to toggle source
# File lib/redis/claim/actions.rb, line 52
def get_db_lock
  return app_name if redis.setnx(lock_key, app_name)
  redis.get(lock_key)
rescue => e
  return app_name if config.ignore_connection_error
  raise e
end
raise_configuration_error(message) click to toggle source
# File lib/redis/claim/actions.rb, line 60
def raise_configuration_error(message)
  raise Redis::Claim::InvalidConfiguration, message
end