Sha256: 6690d6b5aca4774767ddeec9ff2e0cc9fdd9ac1a9a16f749c9246d901b11d620

Contents?: true

Size: 731 Bytes

Versions: 1

Compression:

Stored size: 731 Bytes

Contents

module WithAdvisoryLock
  class PostgreSQL < Base

    # See http://www.postgresql.org/docs/9.1/static/functions-admin.html#FUNCTIONS-ADVISORY-LOCKS

    def try_lock
      # pg_try_advisory_lock will either obtain the lock immediately
      # and return true, or return false if the lock cannot be acquired immediately
      "t" == connection.select_value("SELECT pg_try_advisory_lock(#{numeric_lock})").to_s
    end

    def release_lock
      "t" == connection.select_value("SELECT pg_advisory_unlock(#{numeric_lock})").to_s
    end

    def numeric_lock
      @numeric_lock ||= begin
        if lock_name.is_a? Numeric
          lock_name.to_i
        else
          lock_name.to_s.hash
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
with_advisory_lock-0.0.7 lib/with_advisory_lock/postgresql.rb