Class: Gitlab::ExclusiveLeaseHelpers::SleepingLock
- Inherits:
-
Object
- Object
- Gitlab::ExclusiveLeaseHelpers::SleepingLock
- Defined in:
- lib/gitlab/exclusive_lease_helpers/sleeping_lock.rb
Overview
Wrapper around ExclusiveLease that adds retry logic
Instance Method Summary collapse
-
#initialize(key, timeout:, delay:) ⇒ SleepingLock
constructor
A new instance of SleepingLock.
- #obtain(max_attempts) ⇒ Object
- #retried? ⇒ Boolean
Constructor Details
#initialize(key, timeout:, delay:) ⇒ SleepingLock
Returns a new instance of SleepingLock.
9 10 11 12 13 |
# File 'lib/gitlab/exclusive_lease_helpers/sleeping_lock.rb', line 9 def initialize(key, timeout:, delay:) @lease = ::Gitlab::ExclusiveLease.new(key, timeout: timeout) @delay = delay @attempts = 0 end |
Instance Method Details
#obtain(max_attempts) ⇒ Object
15 16 17 18 19 20 21 22 |
# File 'lib/gitlab/exclusive_lease_helpers/sleeping_lock.rb', line 15 def obtain(max_attempts) until held? raise FailedToObtainLockError, 'Failed to obtain a lock' if attempts >= max_attempts sleep(sleep_sec) unless first_attempt? try_obtain end end |
#retried? ⇒ Boolean
24 25 26 |
# File 'lib/gitlab/exclusive_lease_helpers/sleeping_lock.rb', line 24 def retried? attempts > 1 end |