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
Instance Attribute Summary collapse
-
#args ⇒ Object
Returns the value of attribute args.
-
#connection ⇒ Object
Returns the value of attribute connection.
Instance Method Summary collapse
- #active? ⇒ Boolean
- #aquired? ⇒ Boolean
-
#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.
5 6 7 8 |
# File 'lib/pg_lock/locket.rb', line 5 def initialize(connection, lock_args) self.connection = connection self.args = lock_args end |
Instance Attribute Details
#args ⇒ Object
Returns the value of attribute args.
4 5 6 |
# File 'lib/pg_lock/locket.rb', line 4 def args @args end |
#connection ⇒ Object
Returns the value of attribute connection.
4 5 6 |
# File 'lib/pg_lock/locket.rb', line 4 def connection @connection end |
Instance Method Details
#active? ⇒ Boolean
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/pg_lock/locket.rb', line 26 def active? connection.exec(<<-eos, args).getvalue(0,0) == "t" SELECT granted FROM pg_locks WHERE locktype = 'advisory' AND pid = pg_backend_pid() AND mode = 'ExclusiveLock' AND classid = $1 AND objid = $2 eos end |
#aquired? ⇒ Boolean
20 21 22 23 24 |
# File 'lib/pg_lock/locket.rb', line 20 def aquired? @lock[0]["pg_try_advisory_lock"] == "t" rescue false end |
#lock ⇒ Object
10 11 12 13 |
# File 'lib/pg_lock/locket.rb', line 10 def lock @lock = connection.exec("select pg_try_advisory_lock($1,$2)", args) return aquired? end |
#unlock ⇒ Object
15 16 17 18 |
# File 'lib/pg_lock/locket.rb', line 15 def unlock connection.exec("select pg_advisory_unlock($1,$2)", args) @lock = false end |