Class: TingYun::Configuration::Manager

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

Instance Method Summary collapse

Constructor Details

#initializeManager

Returns a new instance of Manager.



58
59
60
# File 'lib/ting_yun/configuration/manager.rb', line 58

def initialize
  reset_to_defaults
end

Instance Method Details

#[](key) ⇒ Object



62
63
64
# File 'lib/ting_yun/configuration/manager.rb', line 62

def [](key)
  @cache[key]
end

#add_config_for_testing(source, level = 0) ⇒ Object



123
124
125
126
127
128
# File 'lib/ting_yun/configuration/manager.rb', line 123

def add_config_for_testing(source, level=0)
  raise 'Invalid config type for testing' unless [Hash, DottedHash].include?(source.class)
  @configs_for_testing << [source.freeze, level]
  reset_cache
  log_config(:add, source)
end

#app_namesObject



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/ting_yun/configuration/manager.rb', line 74

def app_names
  if TingYun::Agent.config[:'nbs.auto_app_naming']
    begin
    [::TingYun::Frameworks.framework.root.split('/').last]
    rescue
      ::TingYun::Configuration.get_name
    end
  else
    ::TingYun::Configuration.get_name
  end
end

#config_classes_for_testingObject



222
223
224
# File 'lib/ting_yun/configuration/manager.rb', line 222

def config_classes_for_testing
  config_stack.map(&:class)
end

#evaluate_procs(value) ⇒ Object



115
116
117
118
119
120
121
# File 'lib/ting_yun/configuration/manager.rb', line 115

def evaluate_procs(value)
  if value.respond_to?(:call)
    instance_eval(&value)
  else
    value
  end
end

#fetch(key) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/ting_yun/configuration/manager.rb', line 103

def fetch(key)
  config_stack.each do |config|
    next unless config
    accessor = key.to_sym

    if config.has_key?(accessor)
      return evaluate_procs(config[accessor]) #if it's proc
    end
  end
  nil
end

#finished_configuring?Boolean

Returns:



183
184
185
# File 'lib/ting_yun/configuration/manager.rb', line 183

def finished_configuring?
  !@server_source.nil?
end

#flattenedObject



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/ting_yun/configuration/manager.rb', line 206

def flattened
  config_stack.reverse.inject({}) do |flat, layer|
    thawed_layer = layer.to_hash.dup
    thawed_layer.each do |k, v|
      begin
        thawed_layer[k] = instance_eval(&v) if v.respond_to?(:call)
      rescue => e
        ::TingYun::Agent.logger.debug("#{e.class.name} : #{e.message} - when accessing config key #{k}")
        thawed_layer[k] = nil
      end
      thawed_layer.delete(:config)
    end
    flat.merge(thawed_layer.to_hash)
  end
end

#has_key?(key) ⇒ Boolean

Returns:



66
67
68
# File 'lib/ting_yun/configuration/manager.rb', line 66

def has_key?(key)
  @cache.has_key?(key)
end

#keysObject



70
71
72
# File 'lib/ting_yun/configuration/manager.rb', line 70

def keys
  @cache.keys
end

#log_config(direction, source) ⇒ Object



196
197
198
199
200
201
202
203
204
# File 'lib/ting_yun/configuration/manager.rb', line 196

def log_config(direction, source)
  # Just generating this log message (specifically calling
  # flattened.inspect) is expensive enough that we don't want to do it
  # unless we're actually going to be logging the message based on our
  # current log level.
  ::TingYun::Agent.logger.debug do
    "Updating config (#{direction}) from #{source.class}. Results: #{flattened.inspect}"
  end
end

#remove_config(source) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/ting_yun/configuration/manager.rb', line 142

def remove_config(source)
  case source
    when YamlSource         then  @yaml_source          = nil
    when DefaultSource      then  @default_source       = nil
    when EnvironmentSource  then  @environment_source   = nil
    when ManualSource       then  @manual_source        = nil
    when ServerSource       then  @server_source        = nil
    else
      @configs_for_testing.delete_if { |src, lvl| src == source }
  end

  reset_cache

  #invoke_callbacks(:remove,source)

  log_config(:remove, source)

end

#remove_config_type(sym) ⇒ Object



130
131
132
133
134
135
136
137
138
139
# File 'lib/ting_yun/configuration/manager.rb', line 130

def remove_config_type(sym)
  source = case sym
             when :environment then   @environment_source
             when :server      then   @server_source
             when :manual      then   @manual_source
             when :yaml        then   @yaml_source
             when :default     then   @default_source
           end
  remove_config(source)
end

#replace_or_add_config(source) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/ting_yun/configuration/manager.rb', line 161

def replace_or_add_config(source)
  source.freeze

  was_finished = finished_configuring?

  case source
    when YamlSource        then   @yaml_source          = source
    when DefaultSource     then   @default_source       = source
    when EnvironmentSource then   @environment_source   = source
    when ServerSource      then   @server_source        = source
    when ManualSource      then   @manual_source        = source
    else
      TingYun::Agent.logger.warn("Invalid config format; config will be ignored: #{source}")
  end
  reset_cache

  log_config(:add, source)

  TingYun::Agent.instance.events.notify(:finished_configuring) if !was_finished && finished_configuring?
end

#reset_cacheObject



99
100
101
# File 'lib/ting_yun/configuration/manager.rb', line 99

def reset_cache
  @cache = Hash.new { |hash, key| hash[key] = self.fetch(key) }
end

#reset_to_defaultsObject



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/ting_yun/configuration/manager.rb', line 86

def reset_to_defaults
  @default_source = DefaultSource.new
  @environment_source = EnvironmentSource.new
  @yaml_source = nil
  @server_source  = nil
  @manual_source = nil
  # @callbacks = Hash.new {|hash,key| hash[key] =[]}#存放需要merge本地和服务端配置的info'

  @configs_for_testing = []

  reset_cache
end

#source(key) ⇒ Object



188
189
190
191
192
193
194
# File 'lib/ting_yun/configuration/manager.rb', line 188

def source(key)
  config_stack.each do |config|
    if config.respond_to?(key.to_sym) || config.has_key?(key.to_sym)
      return config
    end
  end
end

#to_collector_hashObject



229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/ting_yun/configuration/manager.rb', line 229

def to_collector_hash
  DottedHash.new(flattened).to_hash.delete_if do |k, v|
    default = DEFAULTS[k]
    if default
      default[:exclude_from_reported_settings]
    else
      # In our tests, we add totally bogus configs, because testing.
      # In those cases, there will be no default. So we'll just let
      # them through.
      false
    end
  end
end