lib/instana/backend/host_agent.rb in instana-1.216.0 vs lib/instana/backend/host_agent.rb in instana-1.217.0

- old
+ new

@@ -10,10 +10,12 @@ def initialize(discovery: Concurrent::Atom.new(nil), logger: ::Instana.logger) @discovery = discovery @logger = logger @future = nil @client = nil + # Timer task to poll for agent liveliness + @agent_connection_task = Concurrent::TimerTask.new(execution_interval: 75) { announce } end def setup; end def spawn_background_thread @@ -25,11 +27,16 @@ end alias start spawn_background_thread def announce - @client = until_not_nil { HostAgentLookup.new.call } + @client = with_timeout { HostAgentLookup.new.call } + # Shuts down the connection task based on agent connection client. + @client ? @agent_connection_task.shutdown : @agent_connection_task.execute + # Do not continue further if the agent is down/connection to the agent is unsuccessfull + return nil unless @client + begin @discovery.send(:observers)&.send(:notify_and_delete_observers, Time.now, nil, nil) ensure @discovery.delete_observers end @@ -72,17 +79,19 @@ discovery_value['secrets'] end private - def until_not_nil - loop do + def with_timeout + 15.times do result = yield return result unless result.nil? @logger.debug("Waiting on a connection to the agent.") sleep(1) end + @logger.info("Agent connection timed out retrying after 60 seconds") + nil end def discovery_value v = @discovery.value v || {}