Class: Redmon::Worker

Inherits:
Object
  • Object
show all
Includes:
Redis
Defined in:
lib/redmon/worker.rb

Constant Summary

Constants included from Redis

Redis::UNSUPPORTED

Instance Method Summary collapse

Methods included from Redis

#config, #connection_refused, #empty_result, #ns, #redis, #redis_host, #redis_url, #stats_key, #supported?, #unknown, #unquoted, #wrong_number_of_arguments_for

Instance Method Details

#cleanup_old_statsObject



16
17
18
19
20
# File 'lib/redmon/worker.rb', line 16

def cleanup_old_stats
  # When indexing from the end of a sorted set, we start at -1, so we need to add 1 here or we'll be keeping one
  # fewer samples than expected
  redis.zremrangebyscore stats_key,  '-inf', '(' + oldest_data_to_keep.to_s
end

#data_lifespanObject



49
50
51
# File 'lib/redmon/worker.rb', line 49

def data_lifespan
  Redmon.config.data_lifespan
end

#entries(slowlog) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/redmon/worker.rb', line 30

def entries(slowlog)
  sort(slowlog).map do |entry|
    {
      :id           => entry.shift,
      :timestamp    => entry.shift * 1000,
      :process_time => entry.shift,
      :command      => entry.shift.join(' ')
    }
  end
end

#intervalObject



45
46
47
# File 'lib/redmon/worker.rb', line 45

def interval
  Redmon.config.poll_interval
end

#oldest_data_to_keepObject



53
54
55
56
57
# File 'lib/redmon/worker.rb', line 53

def oldest_data_to_keep
  lifespan_seconds = data_lifespan * 60
  oldest_time_to_keep = Time.now - lifespan_seconds
  oldest_time_to_keep.to_i * 1000
end

#record_statsObject



12
13
14
# File 'lib/redmon/worker.rb', line 12

def record_stats
  redis.zadd stats_key, *stats
end

#run!Object



5
6
7
8
9
10
# File 'lib/redmon/worker.rb', line 5

def run!
  EM::PeriodicTimer.new(interval) {
    record_stats
    cleanup_old_stats
  }
end

#sort(slowlog) ⇒ Object



41
42
43
# File 'lib/redmon/worker.rb', line 41

def sort(slowlog)
  slowlog.sort_by{|a| a[2]}.reverse!
end

#statsObject



22
23
24
25
26
27
28
# File 'lib/redmon/worker.rb', line 22

def stats
  stats = redis.info.merge! \
    :dbsize  => redis.dbsize,
    :time    => ts = Time.now.to_i * 1000,
    :slowlog => entries(redis.slowlog :get)
  [ts, stats.to_json]
end