Class: Synapse::ZookeeperDnsWatcher::Dns

Inherits:
DnsWatcher show all
Defined in:
lib/synapse/service_watcher/zookeeper_dns.rb

Constant Summary

Constants inherited from BaseWatcher

BaseWatcher::LEADER_WARN_INTERVAL

Instance Attribute Summary collapse

Attributes inherited from BaseWatcher

#haproxy, #name

Instance Method Summary collapse

Methods inherited from DnsWatcher

#ping?, #start

Methods inherited from BaseWatcher

#backends, #ping?, #start

Methods included from Logging

configure_logger_for, #log, logger_for

Constructor Details

#initialize(opts = {}, synapse, message_queue) ⇒ Dns

Returns a new instance of Dns



54
55
56
57
58
# File 'lib/synapse/service_watcher/zookeeper_dns.rb', line 54

def initialize(opts={}, synapse, message_queue)
  @message_queue = message_queue

  super(opts, synapse)
end

Instance Attribute Details

#discovery_serversObject

Overrides the discovery_servers method on the parent class



52
53
54
# File 'lib/synapse/service_watcher/zookeeper_dns.rb', line 52

def discovery_servers
  @discovery_servers
end

Instance Method Details

#stopObject



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

def stop
  @message_queue.push(Messages::STOP_WATCHER_MESSAGE)
end

#watchObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/synapse/service_watcher/zookeeper_dns.rb', line 64

def watch
  last_resolution = nil
  while true
    # Blocks on message queue, the message will be a signal to stop
    # watching, to check a new set of servers from ZK, or to re-resolve
    # the DNS (triggered every check_interval seconds)
    message = @message_queue.pop

    log.debug "synapse: received message #{message.inspect}"

    case message
    when Messages::StopWatcher
      break
    when Messages::NewServers
      self.discovery_servers = message.servers
    when Messages::CheckInterval
      # Proceed to re-resolve the DNS
    else
      raise Messages::InvalidMessageError,
        "Received unrecognized message: #{message.inspect}"
    end

    # Empty servers means we haven't heard back from ZK yet or ZK is
    # empty.  This should only occur if we don't get results from ZK
    # within check_interval seconds or if ZK is empty.
    if self.discovery_servers.nil? || self.discovery_servers.empty?
      log.warn "synapse: no backends for service #{@name}"
    else
      # Resolve DNS names with the nameserver
      current_resolution = resolve_servers
      unless last_resolution == current_resolution
        last_resolution = current_resolution
        configure_backends(last_resolution)
      end
    end
  end
end