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



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

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



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

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

#console_log_onlyObject (readonly)

only log to console



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

def console_log_only
  @console_log_only
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



156
157
158
159
160
# File 'lib/eventhub/configuration.rb', line 156

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



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

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



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/eventhub/configuration.rb', line 126

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



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

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

#load!(args = {}) ⇒ Object

load configuration from file



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/eventhub/configuration.rb', line 70

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



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/eventhub/configuration.rb', line 89

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

  Dir.glob(args[:pattern]).sort.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



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
62
63
64
65
66
67
# File 'lib/eventhub/configuration.rb', line 37

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

    opts.on(nil, "--console-log-only", "Only log to console (containers)") do
      @console_log_only = 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



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

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

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

Returns:

  • (Boolean)


121
122
123
124
# File 'lib/eventhub/configuration.rb', line 121

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