Sha256: 35d6aea1f676470ed00525b01a9a736aa31ea95543532135dc477f2de5e071b5

Contents?: true

Size: 1.45 KB

Versions: 1

Compression:

Stored size: 1.45 KB

Contents

module Reactor
  module Workers
    module Configuration
      extend ActiveSupport::Concern

      included do
        include Sidekiq::Worker

        CONFIG = [:source, :action, :async, :delay, :deprecated]

        class_attribute *CONFIG
      end

      class_methods do
        def configured?
          CONFIG.all? {|field| !self.send(field).nil? }
        end

        def perform_where_needed(data)
          if deprecated
            return
          elsif delay > 0
            perform_in(delay, data)
          elsif async
            perform_async(data)
          else
            new.perform(data)
          end
          source
        end
      end

      def configured?
        self.class.configured?
      end

      def perform(data)
        raise_unconfigured! unless configured?
        return :__perform_aborted__ unless should_perform?
        event = Reactor::Event.new(data)
        if action.is_a?(Symbol)
          source.send(action, event)
        else
          action.call(event)
        end
      end

      def should_perform?
        if Reactor.test_mode?
          Reactor.test_mode_subscriber_enabled? source
        else
          true
        end
      end

      private

      def raise_unconfigured!
        settings = Hash[CONFIG.map {|s| [s, self.class.send(s)] }]
        raise UnconfiguredWorkerError.new(
            "#{self.class.name} is not properly configured! Here are the settings: #{settings}"
        )
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
reactor-0.16.0 lib/reactor/workers/concerns/configuration.rb