Class: Lockistics::Meter
- Inherits:
-
Object
- Object
- Lockistics::Meter
- Defined in:
- lib/lockistics/meter.rb
Constant Summary collapse
- LUA_SETMAX =
<<-EOB.gsub("\n", " ").gsub(/\s+/, " ") if redis.call("hexists", KEYS[1], KEYS[2]) == 0 or tonumber(redis.call("hget", KEYS[1], KEYS[2])) < tonumber(ARGV[1]) then return redis.call("hset", KEYS[1], KEYS[2], ARGV[1]) else return 0 end EOB
- LUA_SETMIN =
<<-EOB.gsub("\n", " ").gsub(/\s+/, " ") if redis.call("hexists", KEYS[1], KEYS[2]) == 0 or tonumber(redis.call("hget", KEYS[1], KEYS[2])) > tonumber(ARGV[1]) then return redis.call("hset", KEYS[1], KEYS[2], ARGV[1]) else return 0 end EOB
Instance Attribute Summary collapse
-
#key ⇒ Object
Returns the value of attribute key.
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
-
#incr(key) ⇒ Object
You can add custom metrics during runtime with this.
-
#incrby(key, value) ⇒ Object
You can add custom metrics during runtime.
-
#initialize(key, options = {}) ⇒ Meter
constructor
A new instance of Meter.
- #perform(&block) ⇒ Object
- #set_minmax(key, value) ⇒ Object
- #with_lock(&block) ⇒ Object
Constructor Details
#initialize(key, options = {}) ⇒ Meter
Returns a new instance of Meter.
34 35 36 37 38 |
# File 'lib/lockistics/meter.rb', line 34 def initialize(key, ={}) @key = key @options = {:pass_through => Lockistics.configuration.pass_through}.merge() @lock_timeouts = 0 end |
Instance Attribute Details
#key ⇒ Object
Returns the value of attribute key.
12 13 14 |
# File 'lib/lockistics/meter.rb', line 12 def key @key end |
#options ⇒ Object
Returns the value of attribute options.
12 13 14 |
# File 'lib/lockistics/meter.rb', line 12 def @options end |
Instance Method Details
#incr(key) ⇒ Object
You can add custom metrics during runtime with this.
This is a shortcut to incrby(key, 1)
90 91 92 |
# File 'lib/lockistics/meter.rb', line 90 def incr(key) incrby(key, 1) end |
#incrby(key, value) ⇒ Object
You can add custom metrics during runtime
72 73 74 75 76 77 |
# File 'lib/lockistics/meter.rb', line 72 def incrby(key, value) return nil if value == 0 [:hourly, :daily, :total].each do |period| redis.hincrby namespaced_hash(period), key, value end end |
#perform(&block) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/lockistics/meter.rb', line 49 def perform(&block) raise ArgumentError, "perform called without block" unless block_given? if [:pass_through] yield DummyMeter.new else before_perform lock ? with_lock(&block) : yield(self) end rescue Lockistics::LockTimeout @lock_timeouts = 1 raise ensure after_perform unless [:pass_through] end |
#set_minmax(key, value) ⇒ Object
94 95 96 97 98 99 |
# File 'lib/lockistics/meter.rb', line 94 def set_minmax(key, value) [:hourly, :daily, :total].each do |period| redis_hsetmax(namespaced_hash(period), "max.#{key}", value) redis_hsetmin(namespaced_hash(period), "min.#{key}", value) end end |
#with_lock(&block) ⇒ Object
40 41 42 43 44 45 46 47 |
# File 'lib/lockistics/meter.rb', line 40 def with_lock(&block) raise ArgumentError, "with_lock called without block" unless block_given? raise ArgumentError, "lock not defined" if lock.nil? lock.acquire_lock yield self ensure lock.release_lock end |