Class: WithAdvisoryLock::PostgreSQL
- Defined in:
- lib/with_advisory_lock/postgresql.rb
Instance Attribute Summary
Attributes inherited from Base
#connection, #lock_name, #timeout_seconds
Instance Method Summary collapse
- #execute_successful?(pg_function) ⇒ Boolean
-
#lock_keys ⇒ Object
PostgreSQL wants 2 32bit integers as the lock key.
- #release_lock ⇒ Object
- #try_lock ⇒ Object
Methods inherited from Base
#already_locked?, #initialize, lock_stack, #lock_str, #stable_hashcode, #unique_column_name, #with_advisory_lock_if_needed, #yield_with_lock, #yield_with_lock_and_timeout
Constructor Details
This class inherits a constructor from WithAdvisoryLock::Base
Instance Method Details
#execute_successful?(pg_function) ⇒ Boolean
12 13 14 15 16 17 |
# File 'lib/with_advisory_lock/postgresql.rb', line 12 def execute_successful?(pg_function) sql = "SELECT #{pg_function}(#{lock_keys.join(',')}) AS #{unique_column_name}" result = connection.select_value(sql) # MRI returns 't', jruby returns true. YAY! (result == 't' || result == true) end |
#lock_keys ⇒ Object
PostgreSQL wants 2 32bit integers as the lock key.
20 21 22 23 24 25 26 27 |
# File 'lib/with_advisory_lock/postgresql.rb', line 20 def lock_keys @lock_keys ||= begin [stable_hashcode(lock_name), ENV['WITH_ADVISORY_LOCK_PREFIX']].map do |ea| # pg advisory args must be 31 bit ints ea.to_i & 0x7fffffff end end end |
#release_lock ⇒ Object
8 9 10 |
# File 'lib/with_advisory_lock/postgresql.rb', line 8 def release_lock execute_successful?('pg_advisory_unlock') end |
#try_lock ⇒ Object
4 5 6 |
# File 'lib/with_advisory_lock/postgresql.rb', line 4 def try_lock execute_successful?('pg_try_advisory_lock') end |