class Synapse::BaseWatcher

Constants

LEADER_WARN_INTERVAL

Attributes

haproxy[R]
name[R]

Public Class Methods

new(opts={}, synapse) click to toggle source
Calls superclass method
# 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

Public Instance Methods

backends() click to toggle source
# 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?() click to toggle source

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

# File lib/synapse/service_watcher/base.rb, line 65
def ping?
  true
end
start() click to toggle source

this should be overridden to actually start your watcher

# File lib/synapse/service_watcher/base.rb, line 53
def start
  log.info "synapse: starting stub watcher; this means doing nothing at all!"
end
stop() click to toggle source

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

# 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

Private Instance Methods

reconfigure!() click to toggle source
# File lib/synapse/service_watcher/base.rb, line 105
def reconfigure!
  @synapse.reconfigure!
end
set_backends(new_backends) click to toggle source
# File lib/synapse/service_watcher/base.rb, line 97
def set_backends(new_backends)
  if @keep_default_servers
    @backends = @default_servers + new_backends
  else
    @backends = new_backends
  end
end
validate_discovery_opts() click to toggle source
# File lib/synapse/service_watcher/base.rb, line 90
def validate_discovery_opts
  raise ArgumentError, "invalid discovery method '#{@discovery['method']}' for base watcher" \
    unless @discovery['method'] == 'base'

  log.warn "synapse: warning: a stub watcher with no default servers is pretty useless" if @default_servers.empty?
end