Class: Lockistics::Meter

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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, options={})
  @key     = key
  @options = {:pass_through => Lockistics.configuration.pass_through}.merge(options)
  @lock_timeouts = 0
end

Instance Attribute Details

#keyObject

Returns the value of attribute key.



12
13
14
# File 'lib/lockistics/meter.rb', line 12

def key
  @key
end

#optionsObject

Returns the value of attribute options.



12
13
14
# File 'lib/lockistics/meter.rb', line 12

def options
  @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)

Examples:

Lockistics.meter do |meter|
  foo = FooGenerator.new
  foo.perform
  meter.incr 'failed-foo-generations' unless foo.success?
end


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

Examples:

Lockistics.meter do |meter|
  foo = FooGenerator.new
  foo.perform
  meter.incrby 'foos-generated', foo.count
end


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 options[: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 options[: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