Created
September 26, 2016 14:26
-
-
Save wjessop/68cc2df707188d646f95ff436529e3e6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
with_exclusive_lock("my_operation") do | |
# Something that can only happen one at a time | |
end | |
def with_exclusive_lock(lock_name, &block) | |
lock_file_name = "/tmp/#{lock_name}.lock" | |
FileUtils.touch(lock_file_name) | |
lock_file = File.new(lock_file_name) | |
lock_file.flock(File::LOCK_EX) | |
yield | |
lock_file.flock(File::LOCK_UN) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice :)