Module: Mongoid::Locking::Retry

Defined in:
lib/mongoid/locking/retry.rb

Overview

Gives the ability to retry a block of code a specified number of times when a Mongoid::StaleObjectError is raised.

Since:

  • 0.1.0

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

Since:

  • 0.1.0



7
8
9
# File 'lib/mongoid/locking/retry.rb', line 7

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#with_locking(max_retries: 3) ⇒ Object

Retries the block of code a specified number of times when a Mongoid::StaleObjectError is raised.

This method will reload the document before each block execution.

Examples:

person = Person.find(existing.id)
person.with_locking do
  person.update!(name: "Person 1 updated")
end

Parameters:

  • max_retries (Integer) (defaults to: 3)

    The maximum number of times to retry

Returns:

  • (Object)

    The result of the block

Since:

  • 0.1.0



26
27
28
29
30
31
# File 'lib/mongoid/locking/retry.rb', line 26

def with_locking(max_retries: 3)
  self.class.with_locking(max_retries: max_retries) do
    reload
    yield
  end
end