Class: Baton::Configuration
- Inherits:
-
Object
- Object
- Baton::Configuration
- Includes:
- Logging
- Defined in:
- lib/baton/configuration.rb
Instance Attribute Summary collapse
-
#amqp_host_list ⇒ Object
Returns the value of attribute amqp_host_list.
-
#config ⇒ Object
Returns the value of attribute config.
-
#heartbeat ⇒ Object
Returns the value of attribute heartbeat.
-
#host ⇒ Object
Returns the value of attribute host.
-
#password ⇒ Object
Returns the value of attribute password.
-
#user ⇒ Object
Returns the value of attribute user.
-
#vhost ⇒ Object
Returns the value of attribute vhost.
Instance Method Summary collapse
-
#config_path=(path) ⇒ Object
Public: Loads the config file given as parameter and sets up RabbitMQ’s options.
-
#connection_opts ⇒ Object
Public: Defines the connection options for RabbitMQ as a Hash.
-
#initialize ⇒ Configuration
constructor
A new instance of Configuration.
-
#method_missing(name, *args, &block) ⇒ Object
Public: ensure that any configuration options are automatically exposed.
-
#setup_rabbitmq_opts ⇒ Object
Public: Setup RabbitMQ’s options from a config file.
Methods included from Logging
Constructor Details
#initialize ⇒ Configuration
Returns a new instance of Configuration.
10 11 12 |
# File 'lib/baton/configuration.rb', line 10 def initialize @config = {} end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
Public: ensure that any configuration options are automatically exposed.
16 17 18 19 20 21 22 |
# File 'lib/baton/configuration.rb', line 16 def method_missing(name, *args, &block) if name.to_s[-1] == '=' config[name[0..-2].to_s.upcase] = args[0] else config.fetch(name.to_s.upcase) {nil} end end |
Instance Attribute Details
#amqp_host_list ⇒ Object
Returns the value of attribute amqp_host_list.
8 9 10 |
# File 'lib/baton/configuration.rb', line 8 def amqp_host_list @amqp_host_list end |
#config ⇒ Object
Returns the value of attribute config.
8 9 10 |
# File 'lib/baton/configuration.rb', line 8 def config @config end |
#heartbeat ⇒ Object
Returns the value of attribute heartbeat.
8 9 10 |
# File 'lib/baton/configuration.rb', line 8 def heartbeat @heartbeat end |
#host ⇒ Object
Returns the value of attribute host.
8 9 10 |
# File 'lib/baton/configuration.rb', line 8 def host @host end |
#password ⇒ Object
Returns the value of attribute password.
8 9 10 |
# File 'lib/baton/configuration.rb', line 8 def password @password end |
#user ⇒ Object
Returns the value of attribute user.
8 9 10 |
# File 'lib/baton/configuration.rb', line 8 def user @user end |
#vhost ⇒ Object
Returns the value of attribute vhost.
8 9 10 |
# File 'lib/baton/configuration.rb', line 8 def vhost @vhost end |
Instance Method Details
#config_path=(path) ⇒ Object
Public: Loads the config file given as parameter and sets up RabbitMQ’s options.
path - A file path representing a config file
Examples
config_file = "/path/to/file"
Returns nothing. Raises Errno::ENOENT if file cannot be found.
34 35 36 37 38 39 40 41 |
# File 'lib/baton/configuration.rb', line 34 def config_path=(path) config_file = YAML.load_file(path) config.merge!(config_file) setup_rabbitmq_opts rescue Errno::ENOENT => e self.host = "localhost" logger.error "Could not find a baton configuration file at #{path}" end |
#connection_opts ⇒ Object
Public: Defines the connection options for RabbitMQ as a Hash.
Examples
# => {:host=>"localhost", :vhost=>"baton", :user=>"baton", :password=>"password"}
Returns a hash of RabbitMQ connection options.
94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/baton/configuration.rb', line 94 def connection_opts { :host => host, :port => port, :vhost => vhost, :user => user, :password => password, :pass => password, :heartbeat => heartbeat }.delete_if{|k,v| v.nil?} end |
#setup_rabbitmq_opts ⇒ Object
Public: Setup RabbitMQ’s options from a config file. You have the option of passing in a comma seperated string of RabbitMQ servers to connect to. When using a pool of servers one will be randomly picked for the initial connection.
config_file - A hash representing a config file
Examples
# Single RabbitMQ server
config = {
"RABBIT_HOST" => "localhost",
"RABBIT_VHOST" => "baton",
"RABBIT_USER" => "baton",
"RABBIT_PASS" => "password"
}
# Use a pool of RabbitMQ servers
config = {
"RABBIT_HOST" => "host1,host2,host3",
"RABBIT_VHOST" => "baton",
"RABBIT_USER" => "baton",
"RABBIT_PASS" => "password"
}
setup_rabbitmq_opts
Returns nothing.
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/baton/configuration.rb', line 70 def setup_rabbitmq_opts rabbit_hosts = config.fetch("RABBIT_HOST") {"localhost"} rabbit_hosts = rabbit_hosts.split(',') # Pick a random host to connect to self.host = rabbit_hosts[Kernel.rand(rabbit_hosts.size)] self.amqp_host_list = rabbit_hosts self.port = config["RABBIT_PORT"] self.vhost = config["RABBIT_VHOST"] self.user = config["RABBIT_USER"] self.password = config["RABBIT_PASS"] self.heartbeat = config.fetch("RABBIT_HEARTBEAT", 60).to_i end |