class Stoplight::DataStore::Redis

@see Base

Constants

KEY_PREFIX
KEY_SEPARATOR

Public Class Methods

new(redis) click to toggle source

@param redis [::Redis]

# File lib/stoplight/data_store/redis.rb, line 11
def initialize(redis)
  @redis = redis
end

Public Instance Methods

clear_failures(light) click to toggle source
# File lib/stoplight/data_store/redis.rb, line 52
def clear_failures(light)
  failures, = @redis.multi do
    query_failures(light)
    @redis.del(failures_key(light))
  end

  normalize_failures(failures, light.error_notifier)
end
clear_state(light) click to toggle source
# File lib/stoplight/data_store/redis.rb, line 70
def clear_state(light)
  state, = @redis.multi do
    query_state(light)
    @redis.hdel(states_key, light.name)
  end

  normalize_state(state)
end
get_all(light) click to toggle source
# File lib/stoplight/data_store/redis.rb, line 27
def get_all(light)
  failures, state = @redis.multi do
    query_failures(light)
    @redis.hget(states_key, light.name)
  end

  [
    normalize_failures(failures, light.error_notifier),
    normalize_state(state)
  ]
end
get_failures(light) click to toggle source
# File lib/stoplight/data_store/redis.rb, line 39
def get_failures(light)
  normalize_failures(query_failures(light), light.error_notifier)
end
get_state(light) click to toggle source
# File lib/stoplight/data_store/redis.rb, line 61
def get_state(light)
  query_state(light) || State::UNLOCKED
end
names() click to toggle source
# File lib/stoplight/data_store/redis.rb, line 15
def names
  state_names = @redis.hkeys(states_key)

  pattern = key('failures', '*')
  prefix_regex = /^#{key('failures', '')}/
  failure_names = @redis.scan_each(match: pattern).to_a.map do |key|
    key.sub(prefix_regex, '')
  end

  (state_names + failure_names).uniq
end
record_failure(light, failure) click to toggle source
# File lib/stoplight/data_store/redis.rb, line 43
def record_failure(light, failure)
  size, = @redis.multi do
    @redis.lpush(failures_key(light), failure.to_json)
    @redis.ltrim(failures_key(light), 0, light.threshold - 1)
  end

  size
end
set_state(light, state) click to toggle source
# File lib/stoplight/data_store/redis.rb, line 65
def set_state(light, state)
  @redis.hset(states_key, light.name, state)
  state
end

Private Instance Methods

failures_key(light) click to toggle source
# File lib/stoplight/data_store/redis.rb, line 104
def failures_key(light)
  key('failures', light.name)
end
key(*pieces) click to toggle source
# File lib/stoplight/data_store/redis.rb, line 112
def key(*pieces)
  ([KEY_PREFIX] + pieces).join(KEY_SEPARATOR)
end
normalize_failures(failures, error_notifier) click to toggle source
# File lib/stoplight/data_store/redis.rb, line 85
def normalize_failures(failures, error_notifier)
  failures.map do |json|
    begin
      Failure.from_json(json)
    rescue => error
      error_notifier.call(error)
      Failure.from_error(error)
    end
  end
end
normalize_state(state) click to toggle source
# File lib/stoplight/data_store/redis.rb, line 100
def normalize_state(state)
  state || State::UNLOCKED
end
query_failures(light) click to toggle source
# File lib/stoplight/data_store/redis.rb, line 81
def query_failures(light)
  @redis.lrange(failures_key(light), 0, -1)
end
query_state(light) click to toggle source
# File lib/stoplight/data_store/redis.rb, line 96
def query_state(light)
  @redis.hget(states_key, light.name)
end
states_key() click to toggle source
# File lib/stoplight/data_store/redis.rb, line 108
def states_key
  key('states')
end