Class: Knockoff::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/knockoff/config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



13
14
15
16
17
18
19
20
21
22
# File 'lib/knockoff/config.rb', line 13

def initialize
  @environment = 'development'
  @replicas_configuration_hash = {}
  set_replica_configs

  if !properly_configured? && Knockoff.enabled
    puts "[Knockoff] WARNING: Detected enabled Knockoff without proper replica pool configuration. Setting Knockoff.enabled to false."
    Knockoff.enabled = false
  end
end

Instance Attribute Details

#environmentObject (readonly)

The current environment. Normally set to Rails.env, but will default to ‘development’ outside of Rails apps.



5
6
7
# File 'lib/knockoff/config.rb', line 5

def environment
  @environment
end

#replica_configsObject (readonly)

An array of configs to use for the replica pool.



8
9
10
# File 'lib/knockoff/config.rb', line 8

def replica_configs
  @replica_configs
end

#replicas_configuration_hashObject (readonly)

A hash of replica configs to their config hash.



11
12
13
# File 'lib/knockoff/config.rb', line 11

def replicas_configuration_hash
  @replicas_configuration_hash
end

Instance Method Details

#parse_knockoff_replica_envs_to_configsObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/knockoff/config.rb', line 28

def parse_knockoff_replica_envs_to_configs
  # As a basic prevention of crashes, attempt to parse each DB uri
  # and don't add the uri to the final list if it can't be parsed
  replica_env_keys.map.with_index(0) do |env_key, index|
    begin

      # Configure parameters such as prepared_statements, pool, reaping_frequency for all replicas.
      to_copy = ActiveRecord::Base.configurations.configs_for(env_name: 'knockoff_replicas')&.first&.configuration_hash || {}
      register_replica_copy(index, env_key, to_copy)

    rescue URI::InvalidURIError
      Rails.logger.info "LOG NOTIFIER: Invalid URL specified in follower_env_keys. Not including URI, which may result in no followers used." # URI is purposely not printed to logs
      # Return a 'nil' which will be removed from
      # configs with `compact`, resulting in no configs and no followers,
      # therefore disabled since this env will not be in environments list.
      nil
    end
  end.compact
end

#properly_configured?Boolean

If replica_configs actually containts some configuration information, then we know it was properly configured. Improper URI’s will be ignored during the initialization step.

Returns:

  • (Boolean)


69
70
71
# File 'lib/knockoff/config.rb', line 69

def properly_configured?
  !@replica_configs.empty?
end

#replica_database_keysObject



73
74
75
# File 'lib/knockoff/config.rb', line 73

def replica_database_keys
  @replicas_configuration_hash.keys
end

#replica_env_keysObject



48
49
50
51
52
53
54
# File 'lib/knockoff/config.rb', line 48

def replica_env_keys
  if ENV['KNOCKOFF_REPLICA_ENVS'].nil?
    []
  else
    ENV['KNOCKOFF_REPLICA_ENVS'].split(',').map(&:strip)
  end
end

#set_replica_configsObject



24
25
26
# File 'lib/knockoff/config.rb', line 24

def set_replica_configs
  @replica_configs ||= parse_knockoff_replica_envs_to_configs
end

#update_replica_configs(new_configs) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/knockoff/config.rb', line 56

def update_replica_configs(new_configs)
  if ActiveRecord::Base.configurations.configs_for(env_name: 'knockoff_replicas').present?
    updated_config = new_configs.deep_dup.merge!(ActiveRecord::Base.configurations.configs_for(env_name: 'knockoff_replicas').first.configuration_hash)
  end

  @replicas_configuration_hash.each do |key, _config|
    update_replica_config(key, updated_config)
  end
end