Module: JobContracts::Contractable::Prepends

Extended by:
ActiveSupport::Concern
Includes:
MonitorMixin
Defined in:
lib/job_contracts/concerns/contractable.rb

Instance Method Summary collapse

Instance Method Details

#perform(*args) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/job_contracts/concerns/contractable.rb', line 14

def perform(*args)
  # fetch sidekiq job/worker metadata on main thread
  try :sidekiq_job_metadata

  halted = false
  contracts.select(&:before?).each do |contract|
    contract.enforce! self unless halted
    halted = true if contract.breached? && contract.halt?
  end
  super unless halted
ensure
  # enforce after contracts in a separate thread to ensure that any perform related behavior
  # defined in ContractablePrepends will finish executing before we invoke contract.enforce!
  Thread.new do
    sleep 0
    synchronize do
      contracts.select(&:after?).each do |contract|
        contract.enforce! self unless halted
      end
    end
  end
end