Skip to content

Instantly share code, notes, and snippets.

@brainopia
Created October 2, 2011 20:38
Show Gist options
  • Save brainopia/1257911 to your computer and use it in GitHub Desktop.
Save brainopia/1257911 to your computer and use it in GitHub Desktop.
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