Sha256: 247bb87624d0a103d7de7c3f416edba3586ee14f48ca3f9502bd28c85585b8f0

Contents?: true

Size: 1.93 KB

Versions: 1

Compression:

Stored size: 1.93 KB

Contents

# frozen_string_literal: true

require 'set'

module Emittance
  module Resque
    class Dispatcher < Emittance::Dispatcher; end
  end
end

require 'emittance/resque/event_fanout_job'
require 'emittance/resque/event_serializer'
require 'emittance/resque/event_serializer/default'

module Emittance
  module Resque
    ##
    # The Resque dispatcher for Emittance.
    #
    class Dispatcher
      MethodCallRegistration = Struct.new(:klass_name, :method_name, :queue)

      EVENT_FANOUT_JOB = Emittance::Resque::EventFanoutJob
      DEFAULT_QUEUE = :default

      class << self
        include Emittance::Helpers::ConstantHelpers

        attr_writer :default_queue

        def default_queue
          @default_queue || DEFAULT_QUEUE
        end

        private

        def _process_event(event)
          serialized_event = serialize_event(event)

          enqueue_fanout_job(serialized_event)
        end

        def _register(_identifier, _params = {}, &_callback)
          raise InvalidCallbackError, 'Emittance::Resque cannot accept closures as callbacks at this time'
        end

        def _register_method_call(identifier, object, method_name, params = {})
          validate_method_call object, method_name

          registrations_for(identifier) << new_registration(object, method_name, params)
        end

        def new_registration(object, method_name, params = {})
          queue = params[:queue]

          MethodCallRegistration.new(object.name, method_name, queue)
        end

        def enqueue_fanout_job(event)
          ::Resque.enqueue EVENT_FANOUT_JOB, event
        end

        def serialize_event(event)
          Emittance::Resque::EventSerializer.serialize event
        end

        def validate_method_call(object, _method_name)
          error_msg = 'Emittance::Resque can only call methods on classes and modules'
          raise InvalidCallbackError, error_msg unless object.is_a?(Module)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
emittance-resque-1.1.0 lib/emittance/resque/dispatcher.rb