Sha256: c9b9f8065a525151d70c824949d4fa27a8cc6b3373588ca9cf2bc3d5decfad93

Contents?: true

Size: 1.53 KB

Versions: 3

Compression:

Stored size: 1.53 KB

Contents

=begin
EventWorker is an abstract worker for handling events defined by on_event.
You can create handlers by subclassing and redefining the configuration class
methods, or by using Reactor::Workers::EventWorker.dup and overriding the
methods on the new class.
=end
module Reactor
  module Workers
    class EventWorker

      include Sidekiq::Worker

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

      class_attribute *CONFIG

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

      def self.perform_where_needed(data)
        if delay > 0
          perform_in(delay, data)
        elsif async
          perform_async(data)
        else
          new.perform(data)
        end
        source
      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

3 entries across 3 versions & 1 rubygems

Version Path
reactor-0.15.1 lib/reactor/workers/event_worker.rb
reactor-0.15.0 lib/reactor/workers/event_worker.rb
reactor-0.14.0 lib/reactor/workers/event_worker.rb