class Franz::Stats

Attributes

stats[R]

Public Class Methods

new(opts={}) click to toggle source
# File lib/franz/stats.rb, line 8
def initialize opts={}
  @logger = opts[:logger] || Logger.new(STDOUT)
  @interval = opts[:interval] || 300
  @stats = Hash.new
  @lock = Mutex.new
  @t = Thread.new do
    loop do
      sleep @interval
      report
      reset
    end
  end
end

Public Instance Methods

create(name, default=nil) click to toggle source
# File lib/franz/stats.rb, line 32
def create name, default=nil
  with_lock do
    stats[name] = Hash.new { |h,k| h[k] = default }
  end
end
dec(name, by=1) click to toggle source
# File lib/franz/stats.rb, line 50
def dec name, by=1
  with_lock do
    stats[name][:val] -= by
  end
end
delete(name) click to toggle source
# File lib/franz/stats.rb, line 38
def delete name
  with_lock do
    stats.delete name
  end
end
get(name) click to toggle source
# File lib/franz/stats.rb, line 62
def get name
  with_lock do
    stats[name][:val]
  end
end
inc(name, by=1) click to toggle source
# File lib/franz/stats.rb, line 44
def inc name, by=1
  with_lock do
    stats[name][:val] += by
  end
end
set(name, to) click to toggle source
# File lib/franz/stats.rb, line 56
def set name, to
  with_lock do
    stats[name][:val] = to
  end
end
stop() click to toggle source
# File lib/franz/stats.rb, line 23
def stop
  return state if @stop
  @stop = true
  @t.stop
  log.info event: 'stats stopped'
  return nil
end

Private Instance Methods

log() click to toggle source
# File lib/franz/stats.rb, line 71
def log ; @logger end
report() click to toggle source
# File lib/franz/stats.rb, line 79
def report
  ready_stats = with_lock do
    stats.map { |k,vhash| [ k, vhash[:val] ] }
  end

  raise 'No stats?!' if ready_stats.size < 2 # HACK! Need a more elegant solution...

  log.info \
    event: 'stats',
    interval: @interval,
    stats: Hash[ready_stats]
end
reset() click to toggle source
# File lib/franz/stats.rb, line 92
def reset
  with_lock do
    stats.keys.each do |k|
      stats[k].delete :val
    end
  end
end
with_lock() { || ... } click to toggle source
# File lib/franz/stats.rb, line 73
def with_lock &block
  @lock.synchronize do
    yield
  end
end