Class: GlobalErrorHandler::Redis

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

Overview

:nodoc:

Constant Summary collapse

CURRENT_ID_KEY =
'global_error_handler:current_id'
EXCEPTIONS_REDIS_KEY =
'global_error_handler:exceptions'
EXCEPTION_KEY_PREFIX =
'global_error_handler:exception'
FILTER_KEY_PREFIX =
'global_error_handler:filter'
FILTER_FIELDS =
%w(error_class error_message)
FILTER_MAX_CHARS =
60
REDIS_TTL =

4 weeks

4 * 7 * 24 * 60 * 60

Class Method Summary collapse

Class Method Details

.cleanup_database_dependencies!Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/global_error_handler/redis.rb', line 118

def cleanup_database_dependencies!
  total_exceptions_count = exceptions_count
  total_exception_keys_count = redis.keys(exception_key('*')).size
  if total_exceptions_count > total_exception_keys_count
    puts '==> Database dependency is broken. Need to fix it!'
    start = 0
    per_page = 500
    exception_keys_to_be_cleaned_up = []
    valid_chunks_count = 0
    cleanup_count = exception_keys_to_be_cleaned_up.size
    while total_exceptions_count > start
      exception_keys(start, per_page).each do |redis_key|
        exception_keys_to_be_cleaned_up.push redis_key unless redis.exists(redis_key)
      end
      if cleanup_count == (cleanup_count = exception_keys_to_be_cleaned_up.size)
        valid_chunks_count += 1
      end
      break if valid_chunks_count > 3 # if three ranges in a row are consistent, treat database consistency and finish looping
      start += per_page
    end

    puts "*** found #{exception_keys_to_be_cleaned_up.count} broken dependency keys."
    exception_keys_to_be_cleaned_up.each do |redis_key|
      begin
        delete_dependencies(redis_key)
      rescue
        next
      end
    end
  else
    puts '==> Database dependency is OK. No need to fix it!'
  end
end

.clear_filters(key) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/global_error_handler/redis.rb', line 103

def clear_filters(key)
  FILTER_FIELDS.each do |field|
    retry_count = 0
    field_value = build_filter_value(redis.hget key, field)
    begin
      filter_keys_for(field, field_value).each do |filter_key|
        redis.lrem filter_key, 1, key
      end
    rescue
      field_value = ''
      retry if (retry_count += 1) < 2
    end
  end
end

.current_idObject



35
36
37
# File 'lib/global_error_handler/redis.rb', line 35

def current_id
  redis.get(CURRENT_ID_KEY)
end

.delete(key) ⇒ Object



71
72
73
74
# File 'lib/global_error_handler/redis.rb', line 71

def delete(key)
  delete_dependencies key
  redis.del key
end

.delete_all(keys) ⇒ Object



76
77
78
79
80
81
82
83
84
# File 'lib/global_error_handler/redis.rb', line 76

def delete_all(keys)
  keys.each do |key|
    begin
      delete(key)
    rescue
      next
    end
  end
end

.delete_dependencies(key) ⇒ Object



98
99
100
101
# File 'lib/global_error_handler/redis.rb', line 98

def delete_dependencies(key)
  redis.lrem EXCEPTIONS_REDIS_KEY, 1, key
  clear_filters key
end

.exception_key(id = current_id) ⇒ Object



90
91
92
# File 'lib/global_error_handler/redis.rb', line 90

def exception_key(id = current_id)
  "#{EXCEPTION_KEY_PREFIX}:#{id}"
end

.exception_keys(start = 0, per_page = 10) ⇒ Object



51
52
53
# File 'lib/global_error_handler/redis.rb', line 51

def exception_keys(start = 0, per_page = 10)
  redis.lrange EXCEPTIONS_REDIS_KEY, start.to_i, per_page.to_i + start.to_i - 1
end

.exceptions_countObject

def sort(field, direction = ‘ASC’, page = 0, per_page = 1000)

redis.sort(EXCEPTIONS_REDIS_KEY, by: "#{EXCEPTION_KEY_PREFIX}_*->#{field}_*", order: "#{direction}", limit: [page, per_page])

end



43
44
45
# File 'lib/global_error_handler/redis.rb', line 43

def exceptions_count
  redis.llen EXCEPTIONS_REDIS_KEY
end

.filter_exception_keys(start = 0, field = nil, filter = nil, per_page = 10) ⇒ Object



55
56
57
# File 'lib/global_error_handler/redis.rb', line 55

def filter_exception_keys(start = 0, field = nil, filter = nil, per_page = 10)
  redis.lrange filter_key(field, filter), start.to_i, per_page.to_i + start.to_i - 1
end

.filter_key(field, filter) ⇒ Object



94
95
96
# File 'lib/global_error_handler/redis.rb', line 94

def filter_key(field, filter)
  "#{FILTER_KEY_PREFIX}:#{field}:#{filter}"
end

.filter_keys_for(field, filter = '') ⇒ Object



59
60
61
# File 'lib/global_error_handler/redis.rb', line 59

def filter_keys_for(field, filter = '')
  redis.keys filter_key(field, "#{filter}*")
end

.filtered_exceptions_count(field, filter) ⇒ Object



47
48
49
# File 'lib/global_error_handler/redis.rb', line 47

def filtered_exceptions_count(field, filter)
  redis.llen filter_key(field, filter)
end

.find(key) ⇒ Object



63
64
65
# File 'lib/global_error_handler/redis.rb', line 63

def find(key)
  Hashie::Mash.new redis.hgetall(key)
end

.find_all(keys) ⇒ Object



67
68
69
# File 'lib/global_error_handler/redis.rb', line 67

def find_all(keys)
  keys.map { |key| find(key) }.compact
end

.initialize_redis_from_configObject



23
24
25
26
# File 'lib/global_error_handler/redis.rb', line 23

def initialize_redis_from_config
  redis_config = YAML.load_file(File.join(current_root, 'config', 'redis.yml'))[current_env]
  ::Redis.new(redis_config['global_exception_handler'])
end

.redisObject



28
29
30
31
32
33
# File 'lib/global_error_handler/redis.rb', line 28

def redis
  @redis ||= begin
               $redis_global_exception_handler = initialize_redis_from_config unless $redis_global_exception_handler.is_a? ::Redis
               $redis_global_exception_handler
             end
end

.store(info_hash) ⇒ Object



12
13
14
15
16
17
18
19
20
21
# File 'lib/global_error_handler/redis.rb', line 12

def store(info_hash)
  return if info_hash.blank?
  redis_key = exception_key(next_id!)
  redis.hmset redis_key, info_hash.merge(id: current_id).to_a.flatten
  redis.rpush EXCEPTIONS_REDIS_KEY, redis_key
  FILTER_FIELDS.each do |field|
    redis.rpush filter_key(field, build_filter_value(info_hash[field])), redis_key
  end
  redis.expire redis_key, REDIS_TTL
end

.truncate!Object



86
87
88
# File 'lib/global_error_handler/redis.rb', line 86

def truncate!
  redis.flushdb
end