Class: MegaMutex::DistributedMutex

Inherits:
Object
  • Object
show all
Defined in:
lib/mega_mutex/distributed_mutex.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, timeout = nil) ⇒ DistributedMutex

Returns a new instance of DistributedMutex.



16
17
18
19
# File 'lib/mega_mutex/distributed_mutex.rb', line 16

def initialize(key, timeout = nil)
  @key = key
  @timeout = timeout
end

Class Method Details

.cacheObject



11
12
13
# File 'lib/mega_mutex/distributed_mutex.rb', line 11

def cache
  @cache ||= Dalli::Client.new MegaMutex.configuration.memcache_servers, :namespace => MegaMutex.configuration.namespace
end

Instance Method Details

#current_lockObject



38
39
40
# File 'lib/mega_mutex/distributed_mutex.rb', line 38

def current_lock
  cache.get(@key)
end

#loggerObject



21
22
23
# File 'lib/mega_mutex/distributed_mutex.rb', line 21

def logger
  Logging::Logger[self]
end

#run(&block) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/mega_mutex/distributed_mutex.rb', line 25

def run(&block)
  @start_time = Time.now
  log "Attempting to lock mutex..."
  lock!
  log "Locked. Running critical section..."
  result = yield
  log "Critical section complete. Unlocking..."
  result
ensure
  unlock!
  log "Unlocking Mutex."
end