Class: Synapse::Synapse

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

Instance Method Summary collapse

Methods included from Logging

configure_logger_for, #log, logger_for

Constructor Details

#initialize(opts = {}) ⇒ Synapse



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/synapse.rb', line 15

def initialize(opts={})
  # create the service watchers for all our services
  raise "specify a list of services to connect in the config" unless opts.has_key?('services')
  @service_watchers = create_service_watchers(opts['services'])

  # create the haproxy object
  raise "haproxy config section is missing" unless opts.has_key?('haproxy')
  @haproxy = Haproxy.new(opts['haproxy'])

  # configuration is initially enabled to configure on first loop
  @config_updated = true

  # Any exceptions in the watcher threads should wake the main thread so
  # that we can fail fast.
  Thread.abort_on_exception = true

  log.debug "synapse: completed init"
end

Instance Method Details

#reconfigure!Object



70
71
72
# File 'lib/synapse.rb', line 70

def reconfigure!
  @config_updated = true
end

#runObject

start all the watchers and enable haproxy configuration



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
64
65
66
67
68
# File 'lib/synapse.rb', line 35

def run
  log.info "synapse: starting..."

  # start all the watchers
  @service_watchers.map { |watcher| watcher.start }

  # main loop
  loops = 0
  loop do
    @service_watchers.each do |w|
      raise "synapse: service watcher #{w.name} failed ping!" unless w.ping?
    end

    if @config_updated
      @config_updated = false
      log.info "synapse: regenerating haproxy config"
      @haproxy.update_config(@service_watchers)
    else
      sleep 1
    end

    loops += 1
    log.debug "synapse: still running at #{Time.now}" if (loops % 60) == 0
  end

rescue StandardError => e
  log.error "synapse: encountered unexpected exception #{e.inspect} in main thread"
  raise e
ensure
  log.warn "synapse: exiting; sending stop signal to all watchers"

  # stop all the watchers
  @service_watchers.map(&:stop)
end