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



1370
1371
1372
1373
1374
# File 'lib/synapse/config_generator/haproxy.rb', line 1370

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



1376
1377
1378
1379
1380
1381
1382
# File 'lib/synapse/config_generator/haproxy.rb', line 1376

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



1384
1385
1386
1387
1388
1389
1390
1391
# File 'lib/synapse/config_generator/haproxy.rb', line 1384

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



1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
# File 'lib/synapse/config_generator/haproxy.rb', line 1393

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