class Aggro::SagaRunner

Private: Aggregate which runs saga processes.

Public Instance Methods

bindings() click to toggle source
# File lib/aggro/saga_runner.rb, line 23
def bindings
  @bindings ||= []
end
cancel_bindings() click to toggle source
# File lib/aggro/saga_runner.rb, line 27
def cancel_bindings
  bindings.each(&:cancel)
  @bindings = []
end
reject(reason) click to toggle source
# File lib/aggro/saga_runner.rb, line 32
def reject(reason)
  did.rejected reason: reason

  teardown
end
resolve(value) click to toggle source
# File lib/aggro/saga_runner.rb, line 38
def resolve(value)
  did.resolved value: value

  teardown
end
transition(step_name, *args) click to toggle source
# File lib/aggro/saga_runner.rb, line 44
def transition(step_name, *args)
  cancel_bindings
  did.transitioned state: step_name, args: args

  run_step step_name, args
end

Private Instance Methods

did() click to toggle source
Calls superclass method Aggro::Aggregate#did
# File lib/aggro/saga_runner.rb, line 53
def did
  @_context = @details
  super
end
run_step(step_name, args = []) click to toggle source
# File lib/aggro/saga_runner.rb, line 58
def run_step(step_name, args = [])
  with_thread_ids do
    handler = @klass.handler_for_step(step_name)

    fail "Step '#{step_name}' does not exist" unless handler

    @saga.send(:instance_exec, *args, &handler)
  end
rescue => e
  reject e.to_s
end
teardown() click to toggle source
# File lib/aggro/saga_runner.rb, line 70
def teardown
  @saga = nil
  cancel_bindings
  Aggro.event_bus.subscribe(@id, self)
end
with_thread_ids() { || ... } click to toggle source
# File lib/aggro/saga_runner.rb, line 76
def with_thread_ids
  old_causation_id = Thread.current[:causation_id]
  old_correlation_id = Thread.current[:correlation_id]
  Thread.current[:causation_id] = @details[:causation_id]
  Thread.current[:correlation_id] = @details[:correlation_id]
  yield
ensure
  Thread.current[:causation_id] = old_causation_id
  Thread.current[:correlation_id] = old_correlation_id
end