Class: PgAdvisoryLock::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/pg_advisory_lock/base.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, transaction:, shared:, id:, wait:) ⇒ 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

Raises:

  • (ArgumentError)


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.

Yields:

    • call block when lock is acquired block must be passed if transaction argument is false.

Raises:



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.

Yields:

    • call block when lock is acquired block must be passed if transaction argument is false.

Raises:

  • (ArgumentError)

    when lock name is invalid.



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.

Yields:

    • call block when lock is acquired block must be passed if transaction argument is false.

Raises:



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