Module: Osbourne::Locks::Base

Included in:
Memory, NOOP, Redis
Defined in:
lib/osbourne/locks/base.rb

Constant Summary collapse

DEFAULT_SOFT_TTL =

5 minutes

(5 * 60).freeze
DEFAULT_HARD_TTL =

24 hours

(24 * 60 * 60).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#hard_ttlObject

Returns the value of attribute hard_ttl.



11
12
13
# File 'lib/osbourne/locks/base.rb', line 11

def hard_ttl
  @hard_ttl
end

#soft_ttlObject

Returns the value of attribute soft_ttl.



11
12
13
# File 'lib/osbourne/locks/base.rb', line 11

def soft_ttl
  @soft_ttl
end

Instance Method Details

#hard_lock(id) ⇒ Object



22
23
24
# File 'lib/osbourne/locks/base.rb', line 22

def hard_lock(id)
  lock!(lock_key(id), hard_ttl)
end

#initialize(options = {}) ⇒ Object



13
14
15
16
# File 'lib/osbourne/locks/base.rb', line 13

def initialize(options={})
  self.soft_ttl = options.fetch(:soft_ttl, DEFAULT_SOFT_TTL)
  self.hard_ttl = options.fetch(:hard_ttl, DEFAULT_HARD_TTL)
end

#soft_lock(id) ⇒ Object



18
19
20
# File 'lib/osbourne/locks/base.rb', line 18

def soft_lock(id)
  lock(lock_key(id), soft_ttl)
end

#try_with_lock(id, hard_lock: false) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/osbourne/locks/base.rb', line 30

def try_with_lock(id, hard_lock: false)
  if lock(lock_key(id), soft_ttl)
    begin
      yield
      unlock(id)
    rescue => e # rubocop:disable Style/RescueStandardError
      unlock(id)
      raise e
    end

    hard_lock(id) if hard_lock
    true
  else
    false
  end
end

#unlock(id) ⇒ Object



26
27
28
# File 'lib/osbourne/locks/base.rb', line 26

def unlock(id)
  unlock!(lock_key(id))
end