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
# 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']
  @label_filter = @discovery['label_filter'] || false

  @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



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/synapse/service_watcher/base.rb', line 75

def backends
  if @leader_election
    if @backends.all?{|b| b.key?('id') && b['id']}
      smallest = @backends.sort_by{ |b| b['id']}.first
      log.debug "synapse: leader election chose one of #{@backends.count} backends " \
        "(#{smallest['host']}:#{smallest['port']} with id #{smallest['id']})"

      return [smallest]
    elsif (Time.now - @leader_last_warn) > LEADER_WARN_INTERVAL
      log.warn "synapse: service #{@name}: leader election failed; not all backends include an id"
      @leader_last_warn = Time.now
    end

    # if leader election fails, return no backends
    return []
  elsif @label_filter
    return filter_backends_by_label(@backends, @label_filter)
  end

  return @backends
end

#ping?Boolean

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

Returns:

  • (Boolean)


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

def ping?
  true
end

#startObject

this should be overridden to actually start your watcher



59
60
61
# File 'lib/synapse/service_watcher/base.rb', line 59

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`



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

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