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


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/lock_method.rb', line 58

def lock_method(*args)
  options = args.extract_options!
  options = options.symbolize_keys
  method_id = args.first
  if args.last.is_a?(::Numeric)
    options[:ttl] ||= args.last
  end
  original_method_id = "_unlocked_#{method_id}"
  alias_method original_method_id, method_id
  define_method method_id do |*args1|
    options = options.merge(:args => args1)
    lock = ::LockMethod::Lock.new self, method_id, options
    lock.call_and_lock(*([original_method_id]+args1))
  end
end