Class: MegaMutex::DistributedMutex

Inherits:
Object
  • Object
show all
Includes:
Retryable
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.



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

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

Class Method Details

.cacheObject



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

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

Instance Method Details

#current_lockObject



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

def current_lock
  cache.get(@key)
end

#loggerObject



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

def logger
  Logging::Logger[self]
end

#run(&block) ⇒ Object



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

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