Sha256: e5a972bcbb739ed6ed782eea2bba099416584689f8b0fee31ace580a7c215874

Contents?: true

Size: 1.6 KB

Versions: 1

Compression:

Stored size: 1.6 KB

Contents

# frozen_string_literal: true

module BusinessFlow
  # Include me to fire ActiveSupport notifications on the flow, every step,
  # and for any errors that happen.
  module Instrument
    def self.included(klass)
      klass.extend(ClassMethods)
      klass.step_executor ::BusinessFlow::InstrumentedExecutor
    end

    # Contains methods that we add to the DSL
    module ClassMethods
      INSTRUMENTATION_PREFIX = 'business_flow'

      def with_instrument_payload(payload = nil, opts = {}, &blk)
        if payload.is_a?(Hash)
          @payload = Step.new(Callable.new(proc { payload }), {})
        elsif payload || blk
          @payload = Step.new(Callable.new(payload || blk), { default_output: :instrument_payload}.merge(opts))
        else
          @payload ||= Step.new(Callable.new(proc { {} }), opts)
        end
      end

      def self.instrument_payload(flow)
        catch(:halt_step) { flow.class.with_instrument_payload.call(flow)&.merge_into(flow) }
      end

      def instrument(name, flow)
        payload = { flow: flow }.merge(ClassMethods.instrument_payload(flow))
        ActiveSupport::Notifications.instrument(
          "#{INSTRUMENTATION_PREFIX}.#{name}.#{instrumentation_name}", payload
        ) do
          yield payload
        end
      end

      def instrument_steps
        step_executor ::BusinessFlow::InstrumentedStepExecutor
      end

      def instrumentation_name
        @instrumentation_name ||=
          to_s.underscore.freeze
      end

      def event_name
        @event_name ||=
          "#{INSTRUMENTATION_PREFIX}.flow.#{instrumentation_name}"
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
business_flow-0.19.6 lib/business_flow/instrument.rb