Class: Idempo::ActiveRecordBackend::PostgresLock

Inherits:
Object
  • Object
show all
Defined in:
lib/idempo/active_record_backend.rb

Instance Method Summary collapse

Instance Method Details

#acquire(conn, based_on_str) ⇒ Object



29
30
31
32
# File 'lib/idempo/active_record_backend.rb', line 29

def acquire(conn, based_on_str)
  acquisition_result = conn.select_value("SELECT pg_try_advisory_lock(%d)" % derive_lock_key(based_on_str))
  [true, "t"].include?(acquisition_result)
end

#derive_lock_key(from_str) ⇒ Object



38
39
40
41
42
# File 'lib/idempo/active_record_backend.rb', line 38

def derive_lock_key(from_str)
  # The key must be a single bigint (signed long)
  hash_bytes = Digest::SHA1.digest(from_str)
  hash_bytes[0...8].unpack1("l_")
end

#release(conn, based_on_str) ⇒ Object



34
35
36
# File 'lib/idempo/active_record_backend.rb', line 34

def release(conn, based_on_str)
  conn.select_value("SELECT pg_advisory_unlock(%d)" % derive_lock_key(based_on_str))
end