Class: Franz::Stats

Inherits:
Object
  • Object
show all
Defined in:
lib/franz/stats.rb

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Stats

Returns a new instance of Stats.



8
9
10
11
12
13
14
15
16
17
18
19
20
# 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

Instance Method Details

#create(name, default = nil) ⇒ Object



32
33
34
35
36
# 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) ⇒ Object



50
51
52
53
54
# File 'lib/franz/stats.rb', line 50

def dec name, by=1
  with_lock do
    stats[name][:val] -= by
  end
end

#delete(name) ⇒ Object



38
39
40
41
42
# File 'lib/franz/stats.rb', line 38

def delete name
  with_lock do
    stats.delete name
  end
end

#get(name) ⇒ Object



62
63
64
65
66
# File 'lib/franz/stats.rb', line 62

def get name
  with_lock do
    stats[name][:val]
  end
end

#inc(name, by = 1) ⇒ Object



44
45
46
47
48
# File 'lib/franz/stats.rb', line 44

def inc name, by=1
  with_lock do
    stats[name][:val] += by
  end
end

#set(name, to) ⇒ Object



56
57
58
59
60
# File 'lib/franz/stats.rb', line 56

def set name, to
  with_lock do
    stats[name][:val] = to
  end
end

#stopObject



23
24
25
26
27
28
29
# 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