Class: Mutagem::Mutex
Overview
File based mutex
Instance Attribute Summary
Attributes inherited from Lockfile
Instance Method Summary collapse
-
#execute(&block) ⇒ Boolean
Protect a block.
-
#initialize(lockfile = 'mutagem.lck') ⇒ Mutex
constructor
Creates a new Mutex.
Methods inherited from Lockfile
Constructor Details
#initialize(lockfile = 'mutagem.lck') ⇒ Mutex
Creates a new Mutex
11 12 13 |
# File 'lib/mutagem/mutex.rb', line 11 def initialize(lockfile='mutagem.lck') super lockfile end |
Instance Method Details
#execute(&block) ⇒ Boolean
Protect a block
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/mutagem/mutex.rb', line 29 def execute(&block) result = false raise ArgumentError, "missing block" unless block_given? begin open(lockfile, 'w') do |f| # exclusive non-blocking lock result = lock(f, File::LOCK_EX | File::LOCK_NB) do |f| yield end end ensure # clean up but only if we have a positive result meaning we wrote the lockfile FileUtils.rm(lockfile) if (result && File.exists?(lockfile)) end result end |