Class: Synapse::ServiceWatcher::BaseWatcher

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/synapse/service_watcher/base.rb

Constant Summary collapse

LEADER_WARN_INTERVAL =
30

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

configure_logger_for, #log, logger_for

Constructor Details

#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

Instance Attribute Details

#haproxyObject (readonly)

Returns the value of attribute haproxy.



10
11
12
# File 'lib/synapse/service_watcher/base.rb', line 10

def haproxy
  @haproxy
end

#nameObject (readonly)

Returns the value of attribute name.



10
11
12
# File 'lib/synapse/service_watcher/base.rb', line 10

def name
  @name
end

Instance Method Details

#backendsObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/synapse/service_watcher/base.rb', line 82

def backends
  filtered = backends_filtered_by_labels

  if @leader_election
    failure_warning = nil
    if filtered.empty?
      failure_warning = "synapse: service #{@name}: leader election failed: no backends to choose from"
    end

    all_backends_have_ids = filtered.all?{|b| b.key?('id') && b['id']}
    unless all_backends_have_ids
      failure_warning = "synapse: service #{@name}: leader election failed; not all backends include an id"
    end

    # no problems encountered, lets do the leader election
    if failure_warning.nil?
      smallest = filtered.sort_by{ |b| b['id']}.first
      log.debug "synapse: leader election chose one of #{filtered.count} backends " \
        "(#{smallest['host']}:#{smallest['port']} with id #{smallest['id']})"

      return [smallest]

    # we had some sort of problem, lets log about it
    elsif (Time.now - @leader_last_warn) > LEADER_WARN_INTERVAL
      @leader_last_warn = Time.now
      log.warn failure_warning
      return []
    end
  end

  return filtered
end

#ping?Boolean

this should be overridden to do a health check of the watcher

Returns:

  • (Boolean)


78
79
80
# File 'lib/synapse/service_watcher/base.rb', line 78

def ping?
  true
end

#startObject

this should be overridden to actually start your watcher



66
67
68
# File 'lib/synapse/service_watcher/base.rb', line 66

def start
  log.info "synapse: starting stub watcher; this means doing nothing at all!"
end

#stopObject

this should be overridden to actually stop your watcher if necessary if you are running a thread, your loop should run ‘until @should_exit`



72
73
74
75
# File 'lib/synapse/service_watcher/base.rb', line 72

def stop
  log.info "synapse: stopping watcher #{self.name} using default stop handler"
  @should_exit = true
end