class PgLock::Locket

Holds the logic to aquire a lock and parse if locking was successful

Constants

TRUE_VALUES

Attributes

args[RW]
connection[RW]

Public Class Methods

new(connection, lock_args) click to toggle source
# File lib/pg_lock/locket.rb, line 7
def initialize(connection, lock_args)
  self.connection = connection
  self.args       = lock_args
end

Public Instance Methods

acquired?() click to toggle source
# File lib/pg_lock/locket.rb, line 22
def acquired?
  TRUE_VALUES.include?(@lock[0]["pg_try_advisory_lock"])
rescue
  false
end
active?() click to toggle source
# File lib/pg_lock/locket.rb, line 33
    def active?
      query = <<-eos
        SELECT granted
        FROM pg_locks
        WHERE locktype = 'advisory' AND
        pid = pg_backend_pid() AND
        mode = 'ExclusiveLock' AND
        classid = $1 AND
        objid = $2
      eos

      result = connection.exec(query, args)
      return false if result.ntuples == 0

      active = result.getvalue(0,0)
      TRUE_VALUES.include?(active)
    end
aquired?() click to toggle source

Left the misspelled version of this method for backwards compatibility

# File lib/pg_lock/locket.rb, line 29
def aquired?
  acquired?
end
lock() click to toggle source
# File lib/pg_lock/locket.rb, line 12
def lock
  @lock = connection.exec("select pg_try_advisory_lock($1,$2)", args)
  return acquired?
end
unlock() click to toggle source
# File lib/pg_lock/locket.rb, line 17
def unlock
  connection.exec("select pg_advisory_unlock($1,$2)", args)
  @lock = false
end