Created
October 2, 2011 20:38
-
-
Save brainopia/1257911 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
module Lock | |
extend self | |
delegate :connection, :to => ActiveRecord::Base | |
def sync(*args) | |
lock = Zlib.crc32 args.join | |
obtain lock | |
yield | |
ensure | |
release lock | |
end | |
private | |
def obtain(key) | |
connection.execute "SELECT pg_advisory_lock(#{key})" | |
end | |
def release(key) | |
connection.execute "SELECT pg_advisory_unlock(#{key})" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment