Class: PgAdvisoryLock::Base
- Inherits:
-
Object
- Object
- PgAdvisoryLock::Base
- Defined in:
- lib/pg_advisory_lock/base.rb
Class Method Summary collapse
- .inherited(subclass) ⇒ Object
- .register_lock(name, value) ⇒ Object
- .sql_caller_class(klass) ⇒ Object
-
.try_lock(name, transaction: true, shared: false, id: nil) { ... } ⇒ Object
Tries to lock specific advisory lock.
-
.with_lock(name, transaction: true, shared: false, id: nil) { ... } ⇒ Object
Locks specific advisory lock.
Instance Method Summary collapse
-
#initialize(name, transaction:, shared:, id:, wait:) ⇒ Base
constructor
A new instance of Base.
-
#lock { ... } ⇒ Object
Yield.
Constructor Details
#initialize(name, transaction:, shared:, id:, wait:) ⇒ Base
Returns a new instance of Base.
84 85 86 87 88 89 90 |
# File 'lib/pg_advisory_lock/base.rb', line 84 def initialize(name, transaction:, shared:, id:, wait:) @name = name.to_sym @transaction = transaction @shared = shared @id = id @wait = wait end |
Class Method Details
.inherited(subclass) ⇒ Object
18 19 20 21 |
# File 'lib/pg_advisory_lock/base.rb', line 18 def inherited(subclass) subclass._lock_names = {} super end |
.register_lock(name, value) ⇒ Object
25 26 27 28 29 |
# File 'lib/pg_advisory_lock/base.rb', line 25 def register_lock(name, value) raise ArgumentError, 'value must be integer or array of integers' unless int_or_array_of_ints?(value) _lock_names[name.to_sym] = value end |
.sql_caller_class(klass) ⇒ Object
32 33 34 |
# File 'lib/pg_advisory_lock/base.rb', line 32 def sql_caller_class(klass) self._sql_caller_class = klass end |
.try_lock(name, transaction: true, shared: false, id: nil) { ... } ⇒ Object
Tries to lock specific advisory lock. If it’s already locked raises exception.
63 64 65 |
# File 'lib/pg_advisory_lock/base.rb', line 63 def try_lock(name, transaction: true, shared: false, id: nil, &block) new(name, transaction: transaction, shared: shared, id: id, wait: false).lock(&block) end |
.with_lock(name, transaction: true, shared: false, id: nil) { ... } ⇒ Object
Locks specific advisory lock. If it’s already locked just waits.
47 48 49 |
# File 'lib/pg_advisory_lock/base.rb', line 47 def with_lock(name, transaction: true, shared: false, id: nil, &block) new(name, transaction: transaction, shared: shared, id: id, wait: true).lock(&block) end |
Instance Method Details
#lock { ... } ⇒ Object
Returns yield.
97 98 99 100 101 102 |
# File 'lib/pg_advisory_lock/base.rb', line 97 def lock(&block) with_logger do lock_args = build_lock_args advisory_lock(lock_args, &block) end end |