Class: Stockpile::Lock

Inherits:
Object
  • Object
show all
Defined in:
lib/stockpile/lock.rb

Overview

Stockpile::Lock

Attempts to set up exclusive lock to execute a block of code. Returns Stockpile::LockedExcutionResult holding result of execution. If lock can not be established (someone else is executing the code) then Stockpile::LockedExcutionResult will hold Stockpile::FailedLockExecution as a result of execution

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(db, lock_key) ⇒ Lock

Returns a new instance of Lock.



32
33
34
35
# File 'lib/stockpile/lock.rb', line 32

def initialize(db, lock_key)
  @db = db
  @lock_key = lock_key
end

Instance Attribute Details

#dbObject (readonly)

Returns the value of attribute db.



26
27
28
# File 'lib/stockpile/lock.rb', line 26

def db
  @db
end

#lock_keyObject (readonly)

Returns the value of attribute lock_key.



26
27
28
# File 'lib/stockpile/lock.rb', line 26

def lock_key
  @lock_key
end

Class Method Details

.perform_locked(db: :default, lock_key:, &block) ⇒ Object



28
29
30
# File 'lib/stockpile/lock.rb', line 28

def self.perform_locked(db: :default, lock_key:, &block)
  new(db, lock_key).perform_locked(&block)
end

Instance Method Details

#perform_locked(&block) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/stockpile/lock.rb', line 37

def perform_locked(&block)
  if lock
    successful_execution(&block)
  else
    failed_execution
  end
end