Class: PactBroker::DB::AdvisoryLock

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/pact_broker/db/advisory_lock.rb

Instance Method Summary collapse

Methods included from Logging

included, #log_error, #log_with_tag, #measure_info

Constructor Details

#initialize(database_connection, name, type = :pg_try_advisory_lock) ⇒ AdvisoryLock

Returns a new instance of AdvisoryLock.



20
21
22
23
24
25
26
# File 'lib/pact_broker/db/advisory_lock.rb', line 20

def initialize(database_connection, name, type = :pg_try_advisory_lock)
  @database_connection = database_connection
  @name = name
  @type = type
  @lock_obtained = false
  register_advisory_lock if postgres?
end

Instance Method Details

#lock_obtained?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/pact_broker/db/advisory_lock.rb', line 42

def lock_obtained?
  @lock_obtained
end

#with_lockObject



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/pact_broker/db/advisory_lock.rb', line 28

def with_lock
  if postgres?
    @database_connection.with_advisory_lock(@name) do
      logger.debug("Lock #{@name} obtained")
      @lock_obtained = true
      yield
    end
  else
    logger.warn("Executing block without lock as this is not a Postgres database")
    @lock_obtained = true
    yield
  end
end