Class: Marloss::Locker

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(store, name) ⇒ Locker

Returns a new instance of Locker.



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

def initialize(store, name)
  @store = store
  @name = name
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#storeObject (readonly)

Returns the value of attribute store.



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

def store
  @store
end

Instance Method Details

#obtain_lockObject



12
13
14
# File 'lib/marloss/locker.rb', line 12

def obtain_lock
  store.create_lock(name)
end

#refresh_lockObject



16
17
18
# File 'lib/marloss/locker.rb', line 16

def refresh_lock
  store.refresh_lock(name)
end

#release_lockObject



20
21
22
# File 'lib/marloss/locker.rb', line 20

def release_lock
  store.delete_lock(name)
end

#wait_until_lock_obtained(sleep_seconds: 3, retries: nil) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/marloss/locker.rb', line 24

def wait_until_lock_obtained(sleep_seconds: 3, retries: nil)
  store.create_lock(name)
rescue LockNotObtainedError
  sleep(sleep_seconds)

  unless retries.nil?
    retries -= 1
    raise if retries.zero?
  end

  retry
end