Method: Synapse::ServiceWatcher::BaseWatcher#initialize

Defined in:
lib/synapse/service_watcher/base.rb

#initialize(opts = {}, synapse) ⇒ BaseWatcher

Returns a new instance of BaseWatcher.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
# File 'lib/synapse/service_watcher/base.rb', line 12

def initialize(opts={}, synapse)
  super()

  @synapse = synapse

  # set required service parameters
  %w{name discovery haproxy}.each do |req|
    raise ArgumentError, "missing required option #{req}" unless opts[req]
  end

  @name = opts['name']
  @discovery = opts['discovery']

  # deprecated singular filter
  @singular_label_filter = @discovery['label_filter']
  unless @singular_label_filter.nil?
    log.warn "synapse: `label_filter` parameter is deprecated; use `label_filters` -- an array"
  end

  @label_filters = [@singular_label_filter, @discovery['label_filters']].flatten.compact

  @leader_election = opts['leader_election'] || false
  @leader_last_warn = Time.now - LEADER_WARN_INTERVAL

  # the haproxy config
  @haproxy = opts['haproxy']
  @haproxy['server_options'] ||= ""
  @haproxy['server_port_override'] ||= nil
  %w{backend frontend listen}.each do |sec|
    @haproxy[sec] ||= []
  end

  unless @haproxy.include?('port')
    log.warn "synapse: service #{name}: haproxy config does not include a port; only backend sections for the service will be created; you must move traffic there manually using configuration in `extra_sections`"
  end

  # set initial backends to default servers, if any
  @default_servers = opts['default_servers'] || []
  @backends = @default_servers

  @keep_default_servers = opts['keep_default_servers'] || false

  # If there are no default servers and a watcher reports no backends, then
  # use the previous backends that we already know about.
  @use_previous_backends = opts.fetch('use_previous_backends', true)

  # set a flag used to tell the watchers to exit
  # this is not used in every watcher
  @should_exit = false

  validate_discovery_opts
end