Sha256: b3bbe7b7be0916a0526e9ec3b09ce81fb69526da89a935c570d0d5cf14b86164

Contents?: true

Size: 1.44 KB

Versions: 1

Compression:

Stored size: 1.44 KB

Contents

module Novel
  class Executor
    class CompensationFlow
      include Dry::Monads[:result]

      attr_reader :container, :repository

      def initialize(container:, repository:)
        @container = container
        @repository = repository
      end

      def call(context, state_machine, steps)
        steps.each_with_index.map do |step, index|
          result = execut_step(context, state_machine, step, steps[index + 1])
          context = result.value![:context]

          if result.value![:status] == :waiting
            return result
          else
            result
          end
        end
      end

    private

      def execut_step(context, state_machine, step, next_step)
        result = container.resolve("#{step[:name]}.compensation").call(context)
        status = transaction_status(next_step, state_machine)

        Success(
          status: status,
          result: result,
          context: repository.persist_context(
            context,
            failed: true,
            saga_status: state_machine.state,
            last_competed_compensation_step: step[:name],
            compensation_step_results: context.to_h[:compensation_step_results].merge(step[:name] => result.value!)
          )
        )
      end

      def transaction_status(next_step, state_machine)
        if next_step&.fetch(:async) 
          state_machine.wait

          :waiting
        else
          :processing
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
novel-0.3.0 lib/novel/executor/compensation_flow.rb