class Pwwka::Configuration

Attributes

app_id[W]
async_job_klass[W]
background_job_processor[RW]
default_prefetch[RW]
delayed_exchange_name[RW]
error_handling_chain[W]
log_hooks[RW]
log_level[RW]
logger[RW]
options[RW]
process_name[RW]
rabbit_mq_host[RW]
requeue_on_error[R]
send_message_resque_backoff_strategy[RW]
topic_exchange_name[RW]

Public Class Methods

new() click to toggle source
# File lib/pwwka/configuration.rb, line 24
def initialize
  @rabbit_mq_host        = nil
  @topic_exchange_name   = "pwwka.topics.#{Pwwka.environment}"
  @delayed_exchange_name = "pwwka.delayed.#{Pwwka.environment}"
  @logger                = MonoLogger.new(STDOUT)
  @log_level             = :info
  @log_hooks             = {}
  @options               = {}
  @send_message_resque_backoff_strategy = [5,                  #intermittent glitch?
                                           60,                 # quick interruption
                                           600, 600, 600] # longer-term outage?
  @requeue_on_error = false
  @keep_alive_on_handler_klass_exceptions = false
  @background_job_processor = :resque
  @default_prefetch = nil
  @receive_raw_payload = false
  @process_name = ""
end

Public Instance Methods

allow_delayed?() click to toggle source
# File lib/pwwka/configuration.rb, line 77
def allow_delayed?
  options[:allow_delayed]
end
app_id() click to toggle source
# File lib/pwwka/configuration.rb, line 47
def app_id
  if @app_id.to_s.strip == ""
    if defined?(Rails)
      if Rails.respond_to?(:application) && Rails.respond_to?(:version)
        app_klass = Rails.application.class
        app_parent = app_klass.module_parent
        app_parent.name
      else
        raise "'Rails' is defined, but it doesn't respond to #application or #version, so could not derive the app_id; you must explicitly set it"
      end
    else
      raise "Could not derive the app_id; you must explicitly set it"
    end
  else
    @app_id
  end
end
async_job_klass() click to toggle source
# File lib/pwwka/configuration.rb, line 65
def async_job_klass
  @async_job_klass || background_jobs[background_job_processor]
end
default_prefetch=(val) click to toggle source
# File lib/pwwka/configuration.rb, line 120
def default_prefetch=(val)
  @default_prefetch = val.nil? ? val : val.to_i
end
error_handling_chain() click to toggle source
# File lib/pwwka/configuration.rb, line 81
def error_handling_chain
  @error_handling_chain ||= begin
                              klasses = [ Pwwka::ErrorHandlers::IgnorePayloadFormatErrors ]
                              if self.requeue_on_error
                                klasses << Pwwka::ErrorHandlers::NackAndRequeueOnce
                              else
                                klasses << Pwwka::ErrorHandlers::NackAndIgnore
                              end
                              unless self.keep_alive_on_handler_klass_exceptions?
                                klasses << Pwwka::ErrorHandlers::Crash
                              end
                              klasses
                            end
end
keep_alive_on_handler_klass_exceptions=(val) click to toggle source
# File lib/pwwka/configuration.rb, line 96
def keep_alive_on_handler_klass_exceptions=(val)
  @keep_alive_on_handler_klass_exceptions = val
  if @keep_alive_on_handler_klass_exceptions
    @error_handling_chain.delete(Pwwka::ErrorHandlers::Crash)
  elsif !@error_handling_chain.include?(Pwwka::ErrorHandlers::Crash)
    @error_handling_chain << Pwwka::ErrorHandlers::Crash
  end
end
keep_alive_on_handler_klass_exceptions?() click to toggle source
# File lib/pwwka/configuration.rb, line 43
def keep_alive_on_handler_klass_exceptions?
  @keep_alive_on_handler_klass_exceptions
end
omit_payload_from_log?(level_of_message_with_payload) click to toggle source

True if we should omit the payload from the log

::level_of_message_with_payload the level of the message about to be logged

# File lib/pwwka/configuration.rb, line 148
def omit_payload_from_log?(level_of_message_with_payload)
  return true if @receive_raw_payload
  Pwwka::Logging::LEVELS[Pwwka.configuration.payload_logging.to_sym] > Pwwka::Logging::LEVELS[level_of_message_with_payload.to_sym]
end
payload_logging() click to toggle source
# File lib/pwwka/configuration.rb, line 69
def payload_logging
  @payload_logging || :info
end
payload_logging=(new_payload_logging_level) click to toggle source
# File lib/pwwka/configuration.rb, line 73
def payload_logging=(new_payload_logging_level)
  @payload_logging = new_payload_logging_level
end
payload_parser() click to toggle source

Returns a proc that, when called with the payload, parses it according to the configuration.

By default, this will assume the payload is JSON, parse it, and return a HashWithIndifferentAccess.

# File lib/pwwka/configuration.rb, line 135
def payload_parser
  @payload_parser ||= if @receive_raw_payload
                        ->(payload) { payload }
                      else
                        ->(payload) {
                          ActiveSupport::HashWithIndifferentAccess.new(JSON.parse(payload))
                        }
                      end
end
receive_raw_payload=(val) click to toggle source

Set this if you don’t want the payload parsed. This can be useful is you are expecting a lot of malformed JSON or if you aren’t using JSON at all. Note that currently, setting this to true will prevent all payloads from being logged

# File lib/pwwka/configuration.rb, line 127
def receive_raw_payload=(val)
  @receive_raw_payload = val
  @payload_parser = nil
end
requeue_on_error=(val) click to toggle source
# File lib/pwwka/configuration.rb, line 105
def requeue_on_error=(val)
  @requeue_on_error = val
  if @requeue_on_error
    index = error_handling_chain.index(Pwwka::ErrorHandlers::NackAndIgnore)
    if index
      @error_handling_chain[index] = Pwwka::ErrorHandlers::NackAndRequeueOnce
    end
  else
    index = error_handling_chain.index(Pwwka::ErrorHandlers::NackAndRequeueOnce)
    if index
      @error_handling_chain[index] = Pwwka::ErrorHandlers::NackAndIgnore
    end
  end
end

Private Instance Methods

background_jobs() click to toggle source
# File lib/pwwka/configuration.rb, line 155
def background_jobs
  {
    resque: Pwwka::SendMessageAsyncJob,
    sidekiq: Pwwka::SendMessageAsyncSidekiqJob,
  }
end