Class: TransactionEventStoreMongoid::Locker

Inherits:
Object
  • Object
show all
Defined in:
lib/transaction_event_store_mongoid/locker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(timeout: 10, retry_interval: 0.1, adapter: ::TransactionEventStoreMongoid::Lock.new) ⇒ Locker

Returns a new instance of Locker.



7
8
9
10
11
# File 'lib/transaction_event_store_mongoid/locker.rb', line 7

def initialize(timeout: 10, retry_interval: 0.1, adapter: ::TransactionEventStoreMongoid::Lock.new)
  @timeout = timeout
  @retry_interval = retry_interval
  @adapter = adapter
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



5
6
7
# File 'lib/transaction_event_store_mongoid/locker.rb', line 5

def adapter
  @adapter
end

#retry_intervalObject (readonly)

Returns the value of attribute retry_interval.



5
6
7
# File 'lib/transaction_event_store_mongoid/locker.rb', line 5

def retry_interval
  @retry_interval
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



5
6
7
# File 'lib/transaction_event_store_mongoid/locker.rb', line 5

def timeout
  @timeout
end

Instance Method Details

#with_lock(stream, &block) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/transaction_event_store_mongoid/locker.rb', line 13

def with_lock(stream, &block)
  begin
    start = Time.now
    adapter.with_lock(stream, &block)
  rescue TransactionEventStore::CannotObtainLock
    if (Time.now - start) < timeout
      sleep(retry_interval)
      retry
    else
      raise
    end
  end
end