Class: DBLock::Adapter::Postgres

Inherits:
Base
  • Object
show all
Defined in:
lib/db_lock/adapter/postgres.rb

Instance Method Summary collapse

Methods inherited from Base

#execute, #select_one, #select_value

Instance Method Details

#lock(name, timeout = 0) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/db_lock/adapter/postgres.rb', line 8

def lock(name, timeout = 0)
  pid = connection_pid(connection)
  Timeout.timeout(timeout, LockTimeout) do
    execute lock_query(name)
    # Sadly this returns void in postgres
    true
  end
rescue LockTimeout
  logger&.info 'DBLock: Recovering from expired lock query'
  recover_from_timeout pid, name
end

#release(name) ⇒ Object



20
21
22
23
# File 'lib/db_lock/adapter/postgres.rb', line 20

def release(name)
  res = select_value 'SELECT pg_advisory_unlock(hashtext(?))', name
  res == true
end