Skip to content

Commit

Permalink
Add logging total lock time
Browse files Browse the repository at this point in the history
It is useful to know the total time that a lock was acquired for.
  • Loading branch information
jborrey committed Jun 15, 2017
1 parent 9dfd28d commit 7e7e0b3
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions lib/master_lock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require 'logger'
require 'socket'
require 'json'

# MasterLock is a system for interprocess locking. Resources can be locked by a
# string identifier such that only one thread may have the lock at a time. Lock
Expand Down Expand Up @@ -56,6 +57,8 @@ class << self
# @raise [LockNotAcquiredError] if the lock cannot be acquired before the
# timeout
def synchronize(key, options = {})
start_time = Time.now

check_configured
raise NotStartedError unless @registry

Expand Down Expand Up @@ -88,10 +91,16 @@ def synchronize(key, options = {})
yield
ensure
@registry.unregister(registration)

info = {
key: key,
lock_time: (Time.now - start_time).to_f
}

if lock.release
logger.debug("Released lock #{key}")
logger.debug(info.merge(message: "Released lock.").to_json)
else
logger.warn("Failed to release lock #{key}")
logger.warn(info.merge(message: "Failed to release lock.").to_json)
end
end
end
Expand Down

0 comments on commit 7e7e0b3

Please sign in to comment.