Module: FlockSynchronize
- Defined in:
- lib/flock_synchronize.rb,
lib/flock_synchronize/version.rb
Constant Summary collapse
- VERSION =
"1.1.0"
Class Method Summary collapse
-
.flock_synchronize(key, locking_constant = File::LOCK_EX) ⇒ Object
flock_synchronize wraps a block with a flock-based mutex.
Class Method Details
.flock_synchronize(key, locking_constant = File::LOCK_EX) ⇒ Object
flock_synchronize wraps a block with a flock-based mutex.
Any other process calling flock_synchronize with the same key will wait for the previous block to exit before executing.
NOTE: This only works on a per-process basis and is not compatible with Ruby threads. For that, see the built-in Mutex library
FlockSynchronize.flock_synchronize("my operation") do
some.code_that_needs(synchronizing)
end
The optional locking_constant parameter allows you to specify a different constant than LOCK_EX. See the File.flock documentation for more info.
20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/flock_synchronize.rb', line 20 def self.flock_synchronize(key, locking_constant=File::LOCK_EX) filename = File.join(Dir.tmpdir, "#{key}.flock") begin File.open(filename, 'w') do |f| f.flock(locking_constant) yield end ensure File.unlink filename if File.exists? filename end end |