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
# 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 = enforce_contracts!(contracts.select(&:before?))
  super unless halted
ensure
  unless halted
    # 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!
    # important when multiple contracts have been applied
    Thread.new do
      sleep 0
      synchronize { enforce_contracts! contracts.select(&:after?) }
    end
  end
end