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

Returns a new instance of Base.

Parameters:

  • name (Symbol, String)
    • lock name (will be transformed to number).

  • transaction (Boolean)
    • if true lock will be released at the end of current transaction

    otherwise it will be released at the end of block.

  • shared (Boolean)
    • is lock shared or not.

  • id (Integer)
    • number that will be used in pair with lock number to perform advisory lock.

  • wait (Boolean)
    • when locked by someone else: true - wait for lock, raise PgAdvisoryLock::LockNotObtained.



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

Parameters:

  • name (Symbol, String)
  • value (Integer, Array<(Integer, Integer)>)

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

Parameters:

  • klass (Class<PgSqlCaller::Base>, String)


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.

Parameters:

  • name (Symbol, String)
    • lock name (will be transformed to number).

  • transaction (Boolean) (defaults to: true)
    • if true lock will be released at the end of current transaction

    otherwise it will be released at the end of block.

  • shared (Boolean) (defaults to: false)
    • is lock shared or not.

  • id (Integer) (defaults to: nil)
    • number that will be used in pair with lock number to perform advisory lock.

Yields:

    • call block when lock is acquired

    block must be passed if transaction argument is false.

Returns:

  • yield

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.

Parameters:

  • name (Symbol, String)
    • lock name (will be transformed to number).

  • transaction (Boolean) (defaults to: true)
    • if true lock will be released at the end of current transaction

    otherwise it will be released at the end of block.

  • shared (Boolean) (defaults to: false)
    • is lock shared or not.

  • id (Integer) (defaults to: nil)
    • number that will be used in pair with lock number to perform advisory lock.

Yields:

    • call block when lock is acquired

    block must be passed if transaction argument is false.

Returns:

  • yield

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.

Returns:

  • yield

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