Module: Cadence::Saga::Concern

Defined in:
lib/cadence/saga/concern.rb

Instance Method Summary collapse

Instance Method Details

#compensate?(error, compensate_on: [], do_not_compensate_on: []) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
30
31
32
33
34
# File 'lib/cadence/saga/concern.rb', line 27

def compensate?(error, compensate_on: [], do_not_compensate_on: [])
  error_class = error.class
  if compensate_on.any?
    compensate_on.include?(error_class)
  else
    !do_not_compensate_on.include?(error_class)
  end
end

#run_saga(configuration = {}, &block) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/cadence/saga/concern.rb', line 7

def run_saga(configuration = {}, &block)
  saga = Cadence::Saga::Saga.new(workflow)

  block.call(saga)

  Result.new(true)
rescue StandardError => error # TODO: is there a need for a specialized error here?
  logger.error("Saga execution aborted: #{error.inspect}")
  logger.debug(error.backtrace.join("\n"))

  if compensate?(error, **configuration)
    logger.error('Saga compensating')
    saga.compensate
    Result.new(false, error)
  else
    logger.error('Saga not compensating')
    raise error
  end
end