module RetryableRecord

Retries an operation on an ActiveRecord until no StaleObjectError is being raised.

Example

class User < ActiveRecord::Base
  include RetryableRecord
end

user = User.first

user.retryable do
  user.username = "foo"
  user.save!
end

Example using attempts

class User < ActiveRecord::Base
  include RetryableRecord
end

user = User.first

user.retryable(:attempts => 2) do
  user.username = "foo"
  user.save!
end

Constants

VERSION

Public Class Methods

retry(record, opts = {}) { || ... } click to toggle source

Retryable operations on an ActiveRecord record.

Example

RetryableRecord.retry(user) do
  user.username = "foo"
  user.save!
end
# File lib/retryable_record.rb, line 42
def retry(record, opts = {})
  attempts = opts[:attempts]
  begin
    yield
  rescue ActiveRecord::StaleObjectError
    unless attempts.nil?
      raise unless attempts > 0
      attempts -= 1
    end
    record.reload
    retry
  end
end

Public Instance Methods

retryable(opts = {}, &block) click to toggle source

Retries operations on an ActiveRecord.

# File lib/retryable_record.rb, line 58
def retryable(opts = {}, &block)
  RetryableRecord.retry(self, opts, &block)
end

Private Instance Methods

retry(record, opts = {}) { || ... } click to toggle source

Retryable operations on an ActiveRecord record.

Example

RetryableRecord.retry(user) do
  user.username = "foo"
  user.save!
end
# File lib/retryable_record.rb, line 42
def retry(record, opts = {})
  attempts = opts[:attempts]
  begin
    yield
  rescue ActiveRecord::StaleObjectError
    unless attempts.nil?
      raise unless attempts > 0
      attempts -= 1
    end
    record.reload
    retry
  end
end