def run
      log.info "synapse: starting..."
      statsd_increment('synapse.start')

      # start all the watchers
      statsd_time('synapse.watchers.start.time') do
        @service_watchers.map do |watcher|
          begin
            watcher.start
            statsd_increment("synapse.watcher.start", ['start_result:success', "watcher_name:#{watcher.name}"])
          rescue Exception => e
            statsd_increment("synapse.watcher.start", ['start_result:fail', "watcher_name:#{watcher.name}", "exception_name:#{e.class.name}", "exception_message:#{e.message}"])
            raise e
          end
        end
      end

      statsd_time('synapse.main_loop.elapsed_time') do
        # main loop
        loops = 0
        loop do
          @service_watchers.each do |w|
            alive = w.ping?
            statsd_increment('synapse.watcher.ping.count', ["watcher_name:#{w.name}", "ping_result:#{alive ? "success" : "failure"}"])
            raise "synapse: service watcher #{w.name} failed ping!" unless alive
          end

          if @config_updated
            @config_updated = false
            statsd_increment('synapse.config.update')
            @config_generators.each do |config_generator|
              log.info "synapse: configuring #{config_generator.name}"
              begin
                config_generator.update_config(@service_watchers)
              rescue StandardError => e
                statsd_increment("synapse.config.update_failed", ["config_name:#{config_generator.name}"])
                log.error "synapse: update config failed for config #{config_generator.name} with exception #{e}"
                raise e
              end
            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
      end

    rescue StandardError => e
      statsd_increment('synapse.stop', ['stop_avenue:abort', 'stop_location:main_loop', "exception_name:#{e.class.name}", "exception_message:#{e.message}"])
      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 do |w|
        begin
          w.stop
          statsd_increment("synapse.watcher.stop", ['stop_avenue:clean', 'stop_location:main_loop', "watcher_name:#{w.name}"])
        rescue Exception => e
          statsd_increment("synapse.watcher.stop", ['stop_avenue:exception', 'stop_location:main_loop', "watcher_name:#{w.name}", "exception_name:#{e.class.name}", "exception_message:#{e.message}"])
          raise e
        end
      end
      statsd_increment('synapse.stop', ['stop_avenue:clean', 'stop_location:main_loop'])
    end
def run
      log.info "synapse: starting..."
      statsd_increment('synapse.start')

      # start all the watchers
      statsd_time('synapse.watchers.start.time') do
        @service_watchers.map do |watcher|
          begin
            watcher.start
            statsd_increment("synapse.watcher.start", ['start_result:success', "watcher_name:#{watcher.name}"])
          rescue Exception => e
            statsd_increment("synapse.watcher.start", ['start_result:fail', "watcher_name:#{watcher.name}", "exception_name:#{e.class.name}", "exception_message:#{e.message}"])
            raise e
          end
        end
      end

      statsd_time('synapse.main_loop.elapsed_time') do
        # main loop
        loops = 0
        loop do
          @service_watchers.each do |w|
            alive = w.ping?
            statsd_increment('synapse.watcher.ping.count', ["watcher_name:#{w.name}", "ping_result:#{alive ? "success" : "failure"}"])
            raise "synapse: service watcher #{w.name} failed ping!" unless alive
          end

          if @config_updated
            @config_updated = false
            statsd_increment('synapse.config.update')
            @config_generators.each do |config_generator|
              log.info "synapse: configuring #{config_generator.name}"
              begin
                config_generator.update_config(@service_watchers)
              rescue StandardError => e
                statsd_increment("synapse.config.update_failed", ["config_name:#{config_generator.name}"])
                log.error "synapse: update config failed for config #{config_generator.name} with exception #{e}"
                raise e
              end
            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
      end

    rescue StandardError => e
      statsd_increment('synapse.stop', ['stop_avenue:abort', 'stop_location:main_loop', "exception_name:#{e.class.name}", "exception_message:#{e.message}"])
      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 do |w|
        begin
          w.stop
          statsd_increment("synapse.watcher.stop", ['stop_avenue:clean', 'stop_location:main_loop', "watcher_name:#{w.name}"])
        rescue Exception => e
          statsd_increment("synapse.watcher.stop", ['stop_avenue:exception', 'stop_location:main_loop', "watcher_name:#{w.name}", "exception_name:#{e.class.name}", "exception_message:#{e.message}"])
          raise e
        end
      end
      statsd_increment('synapse.stop', ['stop_avenue:clean', 'stop_location:main_loop'])
    end
start all the watchers and enable haproxy configuration