class Anschel::Stats

Attributes

stats[R]

Public Class Methods

new(interval, logger) click to toggle source
# File lib/anschel/stats.rb, line 8
def initialize interval, logger
  @logger   = logger
  @interval = interval || 30
  @stats    = Hash.new
  @lock     = Mutex.new
  @logger.info event: 'stats-loaded'

  @thread = Thread.new do
    loop do
      sleep @interval
      @logger.info \
        event: 'stats-report',
        version: Anschel::VERSION,
        stats: read
    end
  end
end

Public Instance Methods

create(name, init=0) click to toggle source
# File lib/anschel/stats.rb, line 28
def create name, init=0
  with_lock do
    stats[name] = init
    stats
  end
end
dec(name, by=1) click to toggle source
# File lib/anschel/stats.rb, line 47
def dec name, by=1
  with_lock do
    stats[name] -= by
  end
end
delete(name) click to toggle source
# File lib/anschel/stats.rb, line 35
def delete name
  with_lock do
    stats.delete name
  end
end
get(name) click to toggle source
# File lib/anschel/stats.rb, line 59
def get name
  with_lock do
    stats[name]
  end
end
inc(name, by=1) click to toggle source
# File lib/anschel/stats.rb, line 41
def inc name, by=1
  with_lock do
    stats[name] += by
  end
end
read() click to toggle source
# File lib/anschel/stats.rb, line 26
def read ; stats end
set(name, to) click to toggle source
# File lib/anschel/stats.rb, line 53
def set name, to
  with_lock do
    stats[name] = to
  end
end

Private Instance Methods

with_lock() { || ... } click to toggle source
# File lib/anschel/stats.rb, line 69
def with_lock &block
  @lock.synchronize do
    yield
  end
end