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

Returns a new instance of 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 objects that need to be notified of service changes
  @config_generators = create_config_generators(opts)
  raise "no config generators supplied" if @config_generators.empty?

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

  # 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

#available_generatorsObject



79
80
81
# File 'lib/synapse.rb', line 79

def available_generators
  Hash[@config_generators.collect{|cg| [cg.name, cg]}]
end

#reconfigure!Object



75
76
77
# File 'lib/synapse.rb', line 75

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
69
70
71
72
73
# 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
      @config_generators.each do |config_generator|
        log.info "synapse: configuring #{config_generator.name}"
        config_generator.update_config(@service_watchers)
      end
    end

    sleep 1
    @config_generators.each do |config_generator|
      config_generator.tick(@service_watchers)
    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