Module: Mongoid::Locker

Defined in:
lib/mongoid/locker.rb,
lib/mongoid/locker/version.rb,
lib/mongoid/locker/wrapper4.rb,
lib/mongoid/locker/wrapper5.rb,
lib/mongoid/locker/wrapper6.rb

Defined Under Namespace

Modules: ClassMethods, Wrapper Classes: LockError

Constant Summary collapse

VERSION =
'1.0.1'.freeze

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.locked_at_fieldObject

Returns the value of attribute locked_at_field.



62
63
64
# File 'lib/mongoid/locker.rb', line 62

def locked_at_field
  @locked_at_field
end

.locked_until_fieldObject

Returns the value of attribute locked_until_field.



62
63
64
# File 'lib/mongoid/locker.rb', line 62

def locked_until_field
  @locked_until_field
end

Class Method Details

.configure {|_self| ... } ⇒ Object

Sets configuration using a block

Mongoid::Locker.configure do |config|

config.locked_at_field = :mongoid_locker_locked_at
config.locked_until_field = :mongoid_locker_locked_until

end

Yields:

  • (_self)

Yield Parameters:



81
82
83
# File 'lib/mongoid/locker.rb', line 81

def configure
  yield(self) if block_given?
end

.included(mod) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



65
66
67
68
69
70
71
72
73
# File 'lib/mongoid/locker.rb', line 65

def included(mod)
  mod.extend ClassMethods

  mod.class_variable_set(:@@locked_at_field, locked_at_field)
  mod.class_variable_set(:@@locked_until_field, locked_until_field)

  mod.send(:define_method, :locked_at_field) { mod.class_variable_get(:@@locked_at_field) }
  mod.send(:define_method, :locked_until_field) { mod.class_variable_get(:@@locked_until_field) }
end

.reset!Object

Resets to default configuration.



86
87
88
89
90
# File 'lib/mongoid/locker.rb', line 86

def reset!
  # The field names used by default.
  @locked_at_field     = :locked_at
  @locked_until_field  = :locked_until
end

Instance Method Details

#has_lock?Boolean

Returns whether the current instance has the lock or not.

Returns:

  • (Boolean)

    true if locked, false otherwise



103
104
105
# File 'lib/mongoid/locker.rb', line 103

def has_lock?
  !!(@has_lock && locked?)
end

#locked?Boolean

Returns whether the document is currently locked or not.

Returns:

  • (Boolean)

    true if locked, false otherwise



96
97
98
# File 'lib/mongoid/locker.rb', line 96

def locked?
  !!(self[locked_until_field] && self[locked_until_field] > Time.now.utc)
end

#with_lock(opts = {}) ⇒ void

This method returns an undefined value.

Primary method of plugin: execute the provided code once the document has been successfully locked.

Parameters:

  • opts (Hash) (defaults to: {})

    for the locking mechanism

Options Hash (opts):

  • :timeout (Fixnum)

    The number of seconds until the lock is considered “expired” - defaults to the Mongoid::Locker::ClassMethods#lock_timeout

  • :retries (Fixnum)

    If the document is currently locked, the number of times to retry - defaults to 0

  • :retry_sleep (Float)

    How long to sleep between attempts to acquire lock - defaults to time left until lock is available

  • :wait (Boolean) — default: deprecated

    If the document is currently locked, wait until the lock expires and try again - defaults to false. If set, :retries will be ignored

  • :reload (Boolean)

    After acquiring the lock, reload the document - defaults to true



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/mongoid/locker.rb', line 116

def with_lock(opts = {})
  unless !persisted? || (had_lock = has_lock?)
    if opts[:wait]
      opts[:retries] = 1
      warn 'WARN: `:wait` option for Mongoid::Locker is deprecated - use `retries: 1` instead.'
    end

    lock(opts)
  end

  begin
    yield
  ensure
    unlock if !had_lock && locked?
  end
end