Class: Synapse::ConfigGenerator::Haproxy::HaproxyState

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/synapse/config_generator/haproxy.rb

Overview

methods for managing the state file

Constant Summary collapse

KEY_WATCHER_CONFIG_FOR_GENERATOR =

TODO: enable version in the Haproxy Cache File

"watcher_config_for_generator"
NON_BACKENDS_KEYS =
[KEY_WATCHER_CONFIG_FOR_GENERATOR]

Instance Method Summary collapse

Methods included from Logging

configure_logger_for, #log, logger_for

Constructor Details

#initialize(state_file_path, state_file_ttl, haproxy) ⇒ HaproxyState

Returns a new instance of HaproxyState.



1335
1336
1337
1338
1339
# File 'lib/synapse/config_generator/haproxy.rb', line 1335

def initialize(state_file_path, state_file_ttl, haproxy)
  @state_file_path = state_file_path
  @state_file_ttl = state_file_ttl
  @haproxy = haproxy
end

Instance Method Details

#backends(watcher_name) ⇒ Object



1341
1342
1343
1344
1345
1346
1347
# File 'lib/synapse/config_generator/haproxy.rb', line 1341

def backends(watcher_name)
  if seen.key?(watcher_name)
    seen[watcher_name].select { |section, data| !NON_BACKENDS_KEYS.include?(section) }
  else
    {}
  end
end

#config_for_generator(watcher_name) ⇒ Object



1349
1350
1351
1352
1353
1354
1355
1356
# File 'lib/synapse/config_generator/haproxy.rb', line 1349

def config_for_generator(watcher_name)
  cache_config = {}
  if seen.key?(watcher_name) && seen[watcher_name].key?(KEY_WATCHER_CONFIG_FOR_GENERATOR)
    cache_config = seen[watcher_name][KEY_WATCHER_CONFIG_FOR_GENERATOR]
  end

  cache_config
end

#update_state_file(watchers) ⇒ Object



1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
# File 'lib/synapse/config_generator/haproxy.rb', line 1358

def update_state_file(watchers)
  # if we don't support the state file, do nothing
  return if @state_file_path.nil?

  log.info "synapse: writing state file"
  timestamp = Time.now.to_i

  # Remove stale backends
  seen.each do |watcher_name, data|
    backends(watcher_name).each do |backend_name, backend|
      ts = backend.fetch('timestamp', 0)
      delta = (timestamp - ts).abs
      if delta > @state_file_ttl
        log.info "synapse: expiring #{backend_name} with age #{delta}"
        data.delete(backend_name)
      end
    end
  end

  # Remove any services which no longer have any backends
  seen.reject!{|watcher_name, data| backends(watcher_name).keys.length == 0}

  # Add backends and config from watchers
  watchers.each do |watcher|
    seen[watcher.name] ||= {}

    watcher.backends.each do |backend|
      backend_name = @haproxy.construct_name(backend)
      data = {
        'timestamp' => timestamp,
      }
      server_id = @haproxy.server_id_map[watcher.name][backend_name].to_i
      if server_id && server_id > 0 && server_id <= MAX_SERVER_ID
        data['haproxy_server_id'] = server_id
      end

      seen[watcher.name][backend_name] = data.merge(backend)
    end

    # Add config for generator from watcher
    if watcher.config_for_generator.key?(@haproxy.name)
      seen[watcher.name][KEY_WATCHER_CONFIG_FOR_GENERATOR] =
        watcher.config_for_generator[@haproxy.name]
    end
  end

  # write the data!
  write_data_to_state_file(seen)
end