Class: Logster::RedisStore

Inherits:
BaseStore show all
Defined in:
lib/logster/redis_store.rb

Instance Attribute Summary collapse

Attributes inherited from BaseStore

#ignore, #level, #max_retention, #skip_empty

Instance Method Summary collapse

Methods inherited from BaseStore

#report

Constructor Details

#initialize(redis = nil) ⇒ RedisStore

Returns a new instance of RedisStore.


9
10
11
12
13
# File 'lib/logster/redis_store.rb', line 9

def initialize(redis = nil)
  super()
  @redis = redis || Redis.new
  @max_backlog = 1000
end

Instance Attribute Details

#max_backlogObject

Returns the value of attribute max_backlog


7
8
9
# File 'lib/logster/redis_store.rb', line 7

def max_backlog
  @max_backlog
end

#redisObject

Returns the value of attribute redis


7
8
9
# File 'lib/logster/redis_store.rb', line 7

def redis
  @redis
end

Instance Method Details

#clearObject


81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/logster/redis_store.rb', line 81

def clear
  @redis.del(list_key)
  keys = @redis.smembers(protected_key) || []
  if keys.empty?
    @redis.del(hash_key)
  else
    protected = @redis.mapped_hmget(hash_key, *keys)
    @redis.del(hash_key)
    @redis.mapped_hmset(hash_key, protected)

    sorted = protected
      .values
      .map { |string| Message.from_json(string) }
      .sort
      .map(&:key)

    @redis.pipelined do
      sorted.each do |message_key|
        @redis.rpush(list_key, message_key)
      end
    end
  end
end

#clear_allObject

Delete everything, included protected messages (use in tests)


107
108
109
110
111
# File 'lib/logster/redis_store.rb', line 107

def clear_all
  @redis.del(list_key)
  @redis.del(protected_key)
  @redis.del(hash_key)
end

#countObject


31
32
33
# File 'lib/logster/redis_store.rb', line 31

def count
  @redis.llen(list_key)
end

#get(message_key) ⇒ Object


113
114
115
116
117
118
119
120
# File 'lib/logster/redis_store.rb', line 113

def get(message_key)
  json = @redis.hget(hash_key, message_key)
  return nil unless json

  message = Message.from_json(json)
  message.protected = @redis.sismember(protected_key, message_key)
  message
end

#latest(opts = {}) ⇒ Object


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/logster/redis_store.rb', line 35

def latest(opts={})
  limit = opts[:limit] || 50
  severity = opts[:severity]
  before = opts[:before]
  after = opts[:after]
  search = opts[:search]

  start, finish = find_location(before, after, limit)

  return [] unless start && finish

  results = []

  direction = after ? 1 : -1

  begin
    keys = @redis.lrange(list_key, start, finish) || []
    break unless keys and keys.count > 0
    rows = @redis.hmget(hash_key, keys)

    temp = []

    rows.each do |s|
      row = Message.from_json(s)
      break if before && before == row.key
      row = nil if severity && !severity.include?(row.severity)

      row = filter_search(row, search)
      temp << row if row
    end

    if direction == -1
      results = temp + results
    else
      results += temp
    end

    start += limit * direction
    finish += limit * direction

    finish = -1 if finish > -1
  end while rows.length > 0 && results.length < limit && start < 0

  results
end

#protect(message_key) ⇒ Object


122
123
124
125
126
127
128
129
130
# File 'lib/logster/redis_store.rb', line 122

def protect(message_key)
  json = @redis.hget(hash_key, message_key)
  # Message already lost
  return false unless json

  @redis.sadd(protected_key, message_key)

  true
end

#save(message) ⇒ Object


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/logster/redis_store.rb', line 15

def save(message)
  # multi for integrity
  @redis.multi do
    @redis.hset(hash_key, message.key, message.to_json)
    @redis.rpush(list_key, message.key)
  end

  # TODO make it atomic
  if @redis.llen(list_key) > max_backlog
    removed_key = @redis.lpop(list_key)
    if removed_key && !@redis.sismember(protected_key, removed_key)
      @redis.hdel(hash_key, removed_key)
    end
  end
end

#unprotect(message_key) ⇒ Object


132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/logster/redis_store.rb', line 132

def unprotect(message_key)
  value = @redis.hget(hash_key, message_key)
  # this is a failure of retention
  raise "Message already deleted?" unless value

  @redis.srem(protected_key, message_key)

  index = find_message(list_key, message_key)
  if index == nil
    # Message fell off list - delete
    @redis.hdel(hash_key, message_key)
  end

  true
end