Sha256: 8176a07f24663c5dfa408ee284b15524973a12fda59c6da812bdd7b0c92d125f

Contents?: true

Size: 1.58 KB

Versions: 6

Compression:

Stored size: 1.58 KB

Contents

module Qwirk

  # Base Worker Class for any class that will be processing requests from queues and replying
  module ReplyWorker
    include Worker

    module ClassMethods
      # Define the marshaling and time_to_live that will occur on the response
      def response(options)
        queue_options[:response] = options
      end

      # By default, exceptions don't get forwarded to a fail queue (they get returned to the caller)
      def default_fail_queue_target
        false
      end
    end

    def self.included(base)
      Worker.included(base)
      base.extend(ClassMethods)
    end

    def perform(object)
      begin
        response = request(object)
      rescue Exception => e
        on_exception(e)
      else
        impl.send_response(message, config.marshaler.marshal(response))
      end
      post_request(object)
    rescue Exception => e
      Qwirk.logger.error("Exception in send_response or post_request: #{e.message}")
      log_backtrace(e)
    end

    def request(object)
      raise "#{self}: Need to override request method in #{self.class.name} in order to act on #{object}"
    end

    # Handle any processing that you want to perform after the reply
    def post_request(object)
    end

    #########
    protected
    #########

    def on_exception(e)
      begin
        impl.send_exception(message, e)
      rescue Exception => e
        Qwirk.logger.error("Exception in exception reply: #{e.message}")
        log_backtrace(e)
      end

      # Send it on to the fail queue if it was explicitly set (See default_fail_queue_target above)
      super
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
qwirk-0.2.4 lib/qwirk/reply_worker.rb
qwirk-0.2.3 lib/qwirk/reply_worker.rb
qwirk-0.2.2 lib/qwirk/reply_worker.rb
qwirk-0.2.1 lib/qwirk/reply_worker.rb
qwirk-0.2.0 lib/qwirk/reply_worker.rb
qwirk-0.1.0 lib/qwirk/reply_worker.rb