Class: Anschel::Stats

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

Instance Method Summary collapse

Constructor Details

#initialize(interval, logger) ⇒ Stats

Returns a new instance of Stats.



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

Instance Method Details

#create(name, init = 0) ⇒ Object



28
29
30
31
32
33
# 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) ⇒ Object



47
48
49
50
51
# File 'lib/anschel/stats.rb', line 47

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

#delete(name) ⇒ Object



35
36
37
38
39
# File 'lib/anschel/stats.rb', line 35

def delete name
  with_lock do
    stats.delete name
  end
end

#get(name) ⇒ Object



59
60
61
62
63
# File 'lib/anschel/stats.rb', line 59

def get name
  with_lock do
    stats[name]
  end
end

#inc(name, by = 1) ⇒ Object



41
42
43
44
45
# File 'lib/anschel/stats.rb', line 41

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

#readObject



26
# File 'lib/anschel/stats.rb', line 26

def read ; stats end

#set(name, to) ⇒ Object



53
54
55
56
57
# File 'lib/anschel/stats.rb', line 53

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