Module: EventHub::Configuration

Extended by:
Configuration, Helper
Included in:
Configuration
Defined in:
lib/eventhub/configuration.rb

Overview

Configuraiton module

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helper

create_bunny_connection, get_name_from_class, now_stamp, stringify_keys

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *_args, &_block) ⇒ Object



110
111
112
113
# File 'lib/eventhub/configuration.rb', line 110

def method_missing(name, *_args, &_block)
  @config_data[name.to_sym] ||
    fail(NoMethodError, "unknown configuration [#{name}]", caller)
end

Instance Attribute Details

#config_dataObject (readonly)

data from configuration file



13
14
15
# File 'lib/eventhub/configuration.rb', line 13

def config_data
  @config_data
end

#config_fileObject (readonly)

name of configuration file



12
13
14
# File 'lib/eventhub/configuration.rb', line 12

def config_file
  @config_file
end

#detachedObject (readonly)

run processor run as a daemon



11
12
13
# File 'lib/eventhub/configuration.rb', line 11

def detached
  @detached
end

#environmentObject (readonly)

environment the processor is running



10
11
12
# File 'lib/eventhub/configuration.rb', line 10

def environment
  @environment
end

#nameObject

name of processor



9
10
11
# File 'lib/eventhub/configuration.rb', line 9

def name
  @name
end

Instance Method Details

#dataObject



150
151
152
153
154
# File 'lib/eventhub/configuration.rb', line 150

def data
  warn "[DEPRECATION] `data` is deprecated. Please use new configuration" \
       " access method."
  stringify_keys(@config_data)
end

#deep_merge!(target, data) ⇒ Object

Deep merging of hashes deep_merge by Stefan Rusterholz, see www.ruby-forum.com/topic/142809



102
103
104
105
106
107
108
# File 'lib/eventhub/configuration.rb', line 102

def deep_merge!(target, data)
  return if data.nil?
  merger = proc do |_, v1, v2|
    (v1.is_a?(Hash) && v2.is_a?(Hash)) ? v1.merge(v2, &merger) : v2
  end
  target.merge! data, &merger
end

#default_configurationObject



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/eventhub/configuration.rb', line 120

def default_configuration
  {
    server: {
      user: "guest",
      password: "guest",
      host: "localhost",
      vhost: "event_hub",
      port: 5672,
      tls: false,
      tls_cert: nil,
      tls_key: nil,
      tls_ca_certificates: [],
      verify_peer: false,
      show_bunny_logs: false
    },
    processor: {
      heartbeat_cycle_in_s: 300,
      watchdog_cycle_in_s: 60,
      restart_in_s: 15,
      listener_queues: [@name]
    }
  }
end

#instanceObject



144
145
146
147
148
# File 'lib/eventhub/configuration.rb', line 144

def instance
  warn "[DEPRECATION] `instance` is deprecated. Please use new" \
       " configuration access method."
  self
end

#load!(args = {}) ⇒ Object

load configuration from file



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/eventhub/configuration.rb', line 64

def load!(args = {})
  # for better rspec testing
  @config_file = args[:config_file] if args[:config_file]
  @environment = args[:environment] if args[:environment]

  new_data = {}
  begin
    new_data = JSON.parse(File.read(@config_file), symbolize_names: true)
  rescue => e
    EventHub.logger.warn("Exception while loading configuration file: #{e}")
    EventHub.logger.info("Using default configuration values")
  end

  deep_merge!(@config_data, default_configuration)
  new_data = new_data[@environment.to_sym]
  deep_merge!(@config_data, new_data)
end

#load_more!(args = {}) ⇒ Object

load and merge more configuration files



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/eventhub/configuration.rb', line 83

def load_more!(args = {})
  return unless args[:pattern]

  Dir.glob(args[:pattern]).each do |name|
    next if File.directory?(name)

    begin
      EventHub.logger.info("About to load file [#{name}]...")
      new_data = JSON.parse(File.read(name), symbolize_names: true)
      new_data = new_data[@environment.to_sym]
      deep_merge!(@config_data, new_data)
    rescue => e
      EventHub.logger.warn("Exception while loading file [#{name}]: #{e}")
    end
  end
end

#parse_options(argv = ARGV) ⇒ Object

parse options from argument list



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/eventhub/configuration.rb', line 35

def parse_options(argv = ARGV)
  @config_file = File.join(Dir.getwd, "config", "#{@name}.json")

  OptionParser.new { |opts|
    note = "Define environment"
    opts.on("-e", "--environment ENVIRONMENT", note) do |environment|
      @environment = environment
    end

    opts.on("-d", "--detached", "Run processor detached as a daemon") do
      @detached = true
    end

    note = "Define configuration file"
    opts.on("-c", "--config CONFIG", note) do |config|
      @config_file = config
    end
  }.parse!(argv)

  true
rescue OptionParser::InvalidOption => e
  EventHub.logger.warn("Argument Parsing: #{e}")
  false
rescue OptionParser::MissingArgument => e
  EventHub.logger.warn("Argument Parsing: #{e}")
  false
end

#resetObject



26
27
28
29
30
31
32
# File 'lib/eventhub/configuration.rb', line 26

def reset
  @name = "undefined"
  @environment = "development"
  @detached = false
  @config_file = File.join(Dir.getwd, "config", "#{@name}.json")
  @config_data = {}
end

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


115
116
117
118
# File 'lib/eventhub/configuration.rb', line 115

def respond_to_missing?(name, include_private = false)
  return true if @config_data[name.to_sym]
  false
end