Module: LockMethod::ClassMethods

Defined in:
lib/lock_method.rb

Overview

All Classes (but not instances), get the .lock_method method.

Instance Method Summary collapse

Instance Method Details

#lock_method(*args) ⇒ Object

Lock a method.

Options:

  • :ttl TTL in seconds, defaults to whatever’s in LockMethod.config.default_ttl

  • :spin Whether to wait indefinitely for another lock to expire

Note 2: Check out LockMethod.config.default_ttl… the default is 24 hours!

Example:

class Blog
  # [...]
  def get_latest_entries
    sleep 5
  end
  # [...]
  lock_method :get_latest_entries
  # if you wanted a different ttl...
  # lock_method :get_latest_entries, 800 #seconds
end


96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/lock_method.rb', line 96

def lock_method(*args)
  options = args.extract_options!.symbolize_keys
  spin = options[:spin]
  ttl = options[:ttl]
  method_id = args.first
  if args.last.is_a?(::Numeric)
    ttl ||= args.last
  end
  alias_method LockMethod.original_method_id(method_id), method_id
  define_method method_id do |*my_args, &blk|
    lock = ::LockMethod::Lock.new(self, method_id, my_args, ttl, spin, &blk)
    lock.call_and_lock
  end
end