Class: Stoplight::DataStore::Redis

Inherits:
Base
  • Object
show all
Defined in:
lib/stoplight/data_store/redis.rb

Overview

See Also:

Constant Summary collapse

KEY_PREFIX =
'stoplight'.freeze
KEY_SEPARATOR =
':'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(redis) ⇒ Redis

Returns a new instance of Redis

Parameters:

  • redis (::Redis)

11
12
13
# File 'lib/stoplight/data_store/redis.rb', line 11

def initialize(redis)
  @redis = redis
end

Instance Method Details

#clear_failures(light) ⇒ Object


52
53
54
55
56
57
58
59
# 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) ⇒ Object


70
71
72
73
74
75
76
77
# 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) ⇒ Object


27
28
29
30
31
32
33
34
35
36
37
# 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) ⇒ Object


39
40
41
# 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) ⇒ Object


61
62
63
# File 'lib/stoplight/data_store/redis.rb', line 61

def get_state(light)
  query_state(light) || State::UNLOCKED
end

#namesObject


15
16
17
18
19
20
21
22
23
24
25
# 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) ⇒ Object


43
44
45
46
47
48
49
50
# 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) ⇒ Object


65
66
67
68
# File 'lib/stoplight/data_store/redis.rb', line 65

def set_state(light, state)
  @redis.hset(states_key, light.name, state)
  state
end