Module: TrxExt::Retry

Defined in:
lib/trx_ext/retry.rb

Class Method Summary collapse

Class Method Details

.retry_until_serialized(connection) ⇒ Object

Retries block execution until serialization errors are no longer raised



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/trx_ext/retry.rb', line 25

def retry_until_serialized(connection)
  retries_count = 0
  begin
    yield
  rescue ActiveRecord::SerializationFailure, ActiveRecord::Deadlocked => error
    if connection.open_transactions == 0
      TrxExt.log("Detected transaction rollback condition. Reason - #{error.inspect}. Retrying...")
      retry
    end
    raise
  rescue ActiveRecord::RecordNotUnique => error
    raise unless retry_query?(connection, retries_count)

    retries_count += 1
    TrxExt.log("Detected transaction rollback condition. Reason - #{error.inspect}. Retrying...")
    retry
  end
end

.with_retry_until_serialized(klass, method) ⇒ Object

Wraps specified method in a TrxExt::Retry.retry_until_serialized loop.

Parameters:

  • klass (Class)

    class a method belongs to

  • method (Symbol)

    instance method that needs to be wrapped into TrxExt::Retry.retry_until_serialized



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/trx_ext/retry.rb', line 10

def with_retry_until_serialized(klass, method)
  module_to_prepend = Module.new do
    klass.class_eval <<-RUBY, __FILE__, __LINE__ + 1
      def #{method}(...)
        ::TrxExt::Retry.retry_until_serialized(self) do
          super
        end
      end
    RUBY
  end
  prepend module_to_prepend
  method
end