Class: ActionSubscriber::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/action_subscriber/configuration.rb

Constant Summary collapse

CONFIGURATION_MUTEX =
::Mutex.new
NETWORK_RECOVERY_INTERVAL =
1.freeze
DEFAULTS =
{
  :allow_low_priority_methods => false,
  :connection_reaping_interval => 6,
  :connection_reaping_timeout_interval => 5,
  :default_exchange => 'events',
  :heartbeat => 5,
  :host => 'localhost',
  :hosts => [],
  :network_recovery_interval => NETWORK_RECOVERY_INTERVAL,
  :password => "guest",
  :port => 5672,
  :prefetch => 2,
  :resubscribe_on_consumer_cancellation => true,
  :seconds_to_wait_for_graceful_shutdown => 30,
  :threadpool_size => 8,
  :timeout => 1,
  :tls => false,
  :tls_ca_certificates => [],
  :tls_cert => nil,
  :tls_key => nil,
  :username => "guest",
  :verify_peer => true,
  :virtual_host => "/"
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Instance Methods



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/action_subscriber/configuration.rb', line 99

def initialize
  self.decoder = {
    'application/json' => lambda { |payload| JSON.parse(payload) },
    'text/plain' => lambda { |payload| payload.dup }
  }

  self.error_handler = lambda do |error, env_hash|
    logger = ::ActionSubscriber::Logging.logger

    logger.error(error.message)
    logger.error(error.class.to_s)
    logger.error(error.backtrace.join("\n")) if error.try(:backtrace) && error.backtrace.is_a?(::Array)
  end

  DEFAULTS.each_pair do |key, value|
    self.__send__("#{key}=", value)
  end
end

Instance Attribute Details

#allow_low_priority_methodsObject

Returns the value of attribute allow_low_priority_methods.



6
7
8
# File 'lib/action_subscriber/configuration.rb', line 6

def allow_low_priority_methods
  @allow_low_priority_methods
end

#connection_reaping_intervalObject

Returns the value of attribute connection_reaping_interval.



6
7
8
# File 'lib/action_subscriber/configuration.rb', line 6

def connection_reaping_interval
  @connection_reaping_interval
end

#connection_reaping_timeout_intervalObject

Returns the value of attribute connection_reaping_timeout_interval.



6
7
8
# File 'lib/action_subscriber/configuration.rb', line 6

def connection_reaping_timeout_interval
  @connection_reaping_timeout_interval
end

#decoderObject

Returns the value of attribute decoder.



6
7
8
# File 'lib/action_subscriber/configuration.rb', line 6

def decoder
  @decoder
end

#default_exchangeObject

Returns the value of attribute default_exchange.



6
7
8
# File 'lib/action_subscriber/configuration.rb', line 6

def default_exchange
  @default_exchange
end

#error_handlerObject

Returns the value of attribute error_handler.



6
7
8
# File 'lib/action_subscriber/configuration.rb', line 6

def error_handler
  @error_handler
end

#heartbeatObject

Returns the value of attribute heartbeat.



6
7
8
# File 'lib/action_subscriber/configuration.rb', line 6

def heartbeat
  @heartbeat
end

#hostObject

Returns the value of attribute host.



6
7
8
# File 'lib/action_subscriber/configuration.rb', line 6

def host
  @host
end

#hostsObject

Returns the value of attribute hosts.



6
7
8
# File 'lib/action_subscriber/configuration.rb', line 6

def hosts
  @hosts
end

#network_recovery_intervalObject

Returns the value of attribute network_recovery_interval.



6
7
8
# File 'lib/action_subscriber/configuration.rb', line 6

def network_recovery_interval
  @network_recovery_interval
end

#passwordObject

Returns the value of attribute password.



6
7
8
# File 'lib/action_subscriber/configuration.rb', line 6

def password
  @password
end

#portObject

Returns the value of attribute port.



6
7
8
# File 'lib/action_subscriber/configuration.rb', line 6

def port
  @port
end

#prefetchObject

Returns the value of attribute prefetch.



6
7
8
# File 'lib/action_subscriber/configuration.rb', line 6

def prefetch
  @prefetch
end

#resubscribe_on_consumer_cancellationObject

Returns the value of attribute resubscribe_on_consumer_cancellation.



6
7
8
# File 'lib/action_subscriber/configuration.rb', line 6

def resubscribe_on_consumer_cancellation
  @resubscribe_on_consumer_cancellation
end

#seconds_to_wait_for_graceful_shutdownObject

Returns the value of attribute seconds_to_wait_for_graceful_shutdown.



6
7
8
# File 'lib/action_subscriber/configuration.rb', line 6

def seconds_to_wait_for_graceful_shutdown
  @seconds_to_wait_for_graceful_shutdown
end

#threadpool_sizeObject

Returns the value of attribute threadpool_size.



6
7
8
# File 'lib/action_subscriber/configuration.rb', line 6

def threadpool_size
  @threadpool_size
end

#timeoutObject

Returns the value of attribute timeout.



6
7
8
# File 'lib/action_subscriber/configuration.rb', line 6

def timeout
  @timeout
end

#tlsObject

Returns the value of attribute tls.



6
7
8
# File 'lib/action_subscriber/configuration.rb', line 6

def tls
  @tls
end

#tls_ca_certificatesObject

Returns the value of attribute tls_ca_certificates.



6
7
8
# File 'lib/action_subscriber/configuration.rb', line 6

def tls_ca_certificates
  @tls_ca_certificates
end

#tls_certObject

Returns the value of attribute tls_cert.



6
7
8
# File 'lib/action_subscriber/configuration.rb', line 6

def tls_cert
  @tls_cert
end

#tls_keyObject

Returns the value of attribute tls_key.



6
7
8
# File 'lib/action_subscriber/configuration.rb', line 6

def tls_key
  @tls_key
end

#usernameObject

Returns the value of attribute username.



6
7
8
# File 'lib/action_subscriber/configuration.rb', line 6

def username
  @username
end

#verify_peerObject

Returns the value of attribute verify_peer.



6
7
8
# File 'lib/action_subscriber/configuration.rb', line 6

def verify_peer
  @verify_peer
end

#virtual_hostObject

Returns the value of attribute virtual_host.



6
7
8
# File 'lib/action_subscriber/configuration.rb', line 6

def virtual_host
  @virtual_host
end

Class Method Details

.configure_from_yaml_and_cli(cli_options = {}, reload = false) ⇒ Object

Class Methods



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/action_subscriber/configuration.rb', line 62

def self.configure_from_yaml_and_cli(cli_options = {}, reload = false)
  CONFIGURATION_MUTEX.synchronize do
    @configure_from_yaml_and_cli = nil if reload
    @configure_from_yaml_and_cli ||= begin
      env = ENV["RAILS_ENV"] || ENV["RACK_ENV"] || ENV["APP_ENV"] || "development"

      yaml_config = {}
      absolute_config_path = ::File.expand_path(::File.join("config", "action_subscriber.yml"))
      if ::File.exists?(absolute_config_path)
        erb_yaml = ::ERB.new(::File.read(absolute_config_path)).result
        # Defined in Psych 3.2+ and the new canonical way to load trusted documents:
        # https://github.com/ruby/psych/issues/533#issuecomment-1019363688
        yaml_config = ::YAML.respond_to?(:unsafe_load) ? ::YAML.unsafe_load(erb_yaml)[env] : ::YAML.load(erb_yaml)[env]
      end

      ::ActionSubscriber::Configuration::DEFAULTS.each_pair do |key, value|
        exists, setting = fetch_config_value(key, cli_options, yaml_config)
        ::ActionSubscriber.config.__send__("#{key}=", setting) if exists
      end

      true
    end
  end
end

Instance Method Details

#add_decoder(decoders) ⇒ Object

Instance Methods



121
122
123
124
125
126
127
128
129
# File 'lib/action_subscriber/configuration.rb', line 121

def add_decoder(decoders)
  decoders.each_pair do |content_type, decoder|
    unless decoder.arity == 1
      raise "ActionSubscriber decoders must have an arity of 1. The #{content_type} decoder was given with arity of #{decoder.arity}."
    end
  end

  self.decoder.merge!(decoders)
end

#connection_string=(url) ⇒ Object



131
132
133
134
135
136
# File 'lib/action_subscriber/configuration.rb', line 131

def connection_string=(url)
  settings = ::ActionSubscriber::URI.parse_amqp_url(url)
  settings.each do |key, value|
    send("#{key}=", value)
  end
end

#inspectObject



147
148
149
150
151
152
153
154
155
156
157
# File 'lib/action_subscriber/configuration.rb', line 147

def inspect
  inspection_string  = <<-INSPECT.strip_heredoc
    Rabbit Hosts: #{hosts}
    Rabbit Port: #{port}
    Threadpool Size: #{threadpool_size}
    Low Priority Subscriber: #{allow_low_priority_methods}
    Decoders:
  INSPECT
  decoder.each_key { |key| inspection_string << "  --#{key}\n" }
  return inspection_string
end

#middlewareObject



143
144
145
# File 'lib/action_subscriber/configuration.rb', line 143

def middleware
  @middleware ||= Middleware.initialize_stack
end