Module: WithPgLock
- Included in:
- ApplicationRecord
- Defined in:
- app/models/concerns/with_pg_lock.rb
Instance Method Summary collapse
-
#with_pg_lock(callback, criterion = proc { true }) ⇒ Object
Lock PG table, reload model and execute callback if criterion is met.
Instance Method Details
#with_pg_lock(callback, criterion = proc { true }) ⇒ Object
Lock PG table, reload model and execute callback if criterion is met
6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'app/models/concerns/with_pg_lock.rb', line 6 def with_pg_lock(callback, criterion = proc { true }) # Some notes: # # * nowait is a postgre specific option and may not work with other databases # * nowait will raise an exception if the lock can not be acquired # * we are using a double check lock pattern to reduce lock acquisition with_lock('for update nowait') do reload callback.call if criterion.call end if criterion.call rescue nil end |