Method: MysqlFramework::Scripts::LockManager#request_lock
- Defined in:
- lib/mysql_framework/scripts/lock_manager.rb
#request_lock(key:, ttl: default_ttl, max_attempts: default_max_retries, retry_delay: default_retry_delay) ⇒ Object
This method is called to request a lock (Default 5 minutes)
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/mysql_framework/scripts/lock_manager.rb', line 13 def request_lock(key:, ttl: default_ttl, max_attempts: default_max_retries, retry_delay: default_retry_delay) MysqlFramework.logger.info { "[#{self.class}] - Requesting lock: #{key}." } lock = false count = 0 loop do # request a lock lock = with_client { |client| client.lock(key, ttl) } # if lock was received break out of the loop break if lock # lock was not received so increment request count count += 1 MysqlFramework.logger.debug do "[#{self.class}] - Key is currently locked, waiting for lock: #{key} | Wait count: #{count}." end # check if lock requests have exceeded max request attempts raise "Resource is already locked. Lock key: #{key}. Max attempt exceeded." if count == max_attempts # sleep and try requesting the lock again sleep(retry_delay) end lock end |