Skip to content

Instantly share code, notes, and snippets.

@wjessop
Created September 26, 2016 14:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wjessop/68cc2df707188d646f95ff436529e3e6 to your computer and use it in GitHub Desktop.
Save wjessop/68cc2df707188d646f95ff436529e3e6 to your computer and use it in GitHub Desktop.
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
@rahim
Copy link

rahim commented Sep 26, 2016

Nice :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment