Module: Resque::Stat

Extended by:
Helpers, Stat
Included in:
Stat
Defined in:
lib/resque/stat.rb

Overview

The stat subsystem. Used to keep track of integer counts.

Get a stat:  Stat[name]
Incr a stat: Stat.incr(name)
Decr a stat: Stat.decr(name)
Kill a stat: Stat.clear(name)

Instance Method Summary collapse

Methods included from Helpers

classify, constantize, decode, encode, mongo, mongo_queues, mongo_stats, mongo_workers

Instance Method Details

#<<(stat) ⇒ Object

Increments a stat by one.



33
34
35
# File 'lib/resque/stat.rb', line 33

def <<(stat)
  incr stat
end

#>>(stat) ⇒ Object

Decrements a stat by one.



46
47
48
# File 'lib/resque/stat.rb', line 46

def >>(stat)
  decr stat
end

#[](stat) ⇒ Object

Alias of ‘get`



20
21
22
# File 'lib/resque/stat.rb', line 20

def [](stat)
  get(stat)
end

#clear(stat) ⇒ Object

Removes a stat from Redis, effectively setting it to 0.



51
52
53
# File 'lib/resque/stat.rb', line 51

def clear(stat)
  mongo_stats.remove(:stat => stat)
end

#decr(stat, by = 1) ⇒ Object

For a string stat name, decrements the stat by one.

Can optionally accept a second int parameter. The stat is then decremented by that amount.



41
42
43
# File 'lib/resque/stat.rb', line 41

def decr(stat, by = 1)
  mongo_stats.update({:stat => stat}, {'$inc' => {:value => -by}})
end

#get(stat) ⇒ Object

Returns the int value of a stat, given a string stat name.



13
14
15
16
17
# File 'lib/resque/stat.rb', line 13

def get(stat)
  res = mongo_stats.find_one(:stat => stat)
  return 0 unless res
  res['value'].to_i
end

#incr(stat, by = 1) ⇒ Object

For a string stat name, increments the stat by one.

Can optionally accept a second int parameter. The stat is then incremented by that amount.



28
29
30
# File 'lib/resque/stat.rb', line 28

def incr(stat, by = 1)
  mongo_stats.update({:stat => stat}, {'$inc' => {:value => by}}, :upsert => true)
end