Sha256: 589bb94689b2c7da3e8014528ee1212bc160b45d3bb5f779ce8f22d5a03e6f67

Contents?: true

Size: 1.67 KB

Versions: 3

Compression:

Stored size: 1.67 KB

Contents

require 'qs/queue'

module Qs

  class ErrorHandler

    attr_reader :exception, :context, :error_procs

    def initialize(exception, context_hash)
      @exception   = exception
      @context     = ErrorContext.new(context_hash)
      @error_procs = context_hash[:daemon_data].error_procs.reverse
    end

    # The exception that we are handling can change in the case that the
    # configured error proc raises an exception. If this occurs, the new
    # exception will be passed to subsequent error procs. This is designed to
    # avoid "hidden" errors, this way the daemon will log based on the last
    # exception that occurred.
    def run
      @error_procs.each do |error_proc|
        begin
          error_proc.call(@exception, @context)
        rescue StandardError => proc_exception
          @exception = proc_exception
        end
      end
    end

  end

  class ErrorContext
    attr_reader :daemon_data
    attr_reader :queue_name, :serialized_payload
    attr_reader :job, :handler_class

    def initialize(args)
      @daemon_data        = args[:daemon_data]
      @queue_name         = Queue::RedisKey.parse_name(args[:queue_redis_key].to_s)
      @serialized_payload = args[:serialized_payload]
      @job                = args[:job]
      @handler_class      = args[:handler_class]
    end

    def ==(other)
      if other.kind_of?(self.class)
        self.daemon_data        == other.daemon_data &&
        self.queue_name         == other.queue_name &&
        self.serialized_payload == other.serialized_payload &&
        self.job                == other.job &&
        self.handler_class      == other.handler_class
      else
        super
      end
    end
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
qs-0.3.0 lib/qs/error_handler.rb
qs-0.2.0 lib/qs/error_handler.rb
qs-0.1.0 lib/qs/error_handler.rb