Class: Synapse::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



11
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
# File 'lib/synapse/service_watcher/base.rb', line 11

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']

  @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

  # 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



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

def haproxy
  @haproxy
end

#nameObject (readonly)

Returns the value of attribute name



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

def name
  @name
end

Instance Method Details

#backendsObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/synapse/service_watcher/base.rb', line 69

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 []
  end

  return @backends
end

#ping?Boolean

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

Returns:

  • (Boolean)


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

def ping?
  true
end

#startObject

this should be overridden to actually start your watcher



53
54
55
# File 'lib/synapse/service_watcher/base.rb', line 53

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`



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

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