Class: PgLock::Locket
- Inherits:
-
Object
- Object
- PgLock::Locket
- Defined in:
- lib/pg_lock/locket.rb
Overview
Holds the logic to aquire a lock and parse if locking was successful
Constant Summary collapse
- TRUE_VALUES =
[true, "t"].freeze
Instance Attribute Summary collapse
-
#args ⇒ Object
Returns the value of attribute args.
-
#connection ⇒ Object
Returns the value of attribute connection.
Instance Method Summary collapse
- #acquired? ⇒ Boolean
- #active? ⇒ Boolean
-
#aquired? ⇒ Boolean
Left the misspelled version of this method for backwards compatibility.
-
#initialize(connection, lock_args) ⇒ Locket
constructor
A new instance of Locket.
- #lock ⇒ Object
- #unlock ⇒ Object
Constructor Details
#initialize(connection, lock_args) ⇒ Locket
Returns a new instance of Locket.
7 8 9 10 |
# File 'lib/pg_lock/locket.rb', line 7 def initialize(connection, lock_args) self.connection = connection self.args = lock_args end |
Instance Attribute Details
#args ⇒ Object
Returns the value of attribute args.
6 7 8 |
# File 'lib/pg_lock/locket.rb', line 6 def args @args end |
#connection ⇒ Object
Returns the value of attribute connection.
6 7 8 |
# File 'lib/pg_lock/locket.rb', line 6 def connection @connection end |
Instance Method Details
#acquired? ⇒ Boolean
22 23 24 25 26 |
# File 'lib/pg_lock/locket.rb', line 22 def acquired? TRUE_VALUES.include?(@lock[0]["pg_try_advisory_lock"]) rescue false end |
#active? ⇒ Boolean
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/pg_lock/locket.rb', line 33 def active? query = " SELECT granted\n FROM pg_locks\n WHERE locktype = 'advisory' AND\n pid = pg_backend_pid() AND\n mode = 'ExclusiveLock' AND\n classid = $1 AND\n objid = $2\n eos\n\n result = connection.exec(query, args)\n return false if result.ntuples == 0\n\n active = result.getvalue(0,0)\n TRUE_VALUES.include?(active)\nend\n" |
#aquired? ⇒ Boolean
Left the misspelled version of this method for backwards compatibility
29 30 31 |
# File 'lib/pg_lock/locket.rb', line 29 def aquired? acquired? end |
#lock ⇒ Object
12 13 14 15 |
# 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 ⇒ Object
17 18 19 20 |
# File 'lib/pg_lock/locket.rb', line 17 def unlock connection.exec("select pg_advisory_unlock($1,$2)", args) @lock = false end |