module Mmtrix::Agent::Agent::InstanceMethods::StartWorkerThread

All of this module used to be contained in the start_worker_thread method - this is an artifact of refactoring and can be moved, renamed, etc at will

Constants

LOG_ONCE_KEYS_RESET_PERIOD
MIN_ALLOWED_REPORT_PERIOD

Never allow any data type to be reported more frequently than once per second.

Public Instance Methods

catch_errors() { || ... } click to toggle source

a wrapper method to handle all the errors that can happen in the connection and worker thread system. This guarantees a no-throw from the background thread.

# File lib/mmtrix/agent/agent.rb, line 657
def catch_errors
  yield
rescue Mmtrix::Agent::ForceRestartException => e
  handle_force_restart(e)
  retry
rescue Mmtrix::Agent::ForceDisconnectException => e
  handle_force_disconnect(e)
rescue => e
  handle_other_error(e)
end
create_and_run_event_loop() click to toggle source
# File lib/mmtrix/agent/agent.rb, line 606
def create_and_run_event_loop
  @event_loop = create_event_loop
  @event_loop.on(:report_data) do
    transmit_data
  end
  @event_loop.on(:report_event_data) do
    transmit_event_data
  end
  @event_loop.on(:reset_log_once_keys) do
    ::Mmtrix::Agent.logger.clear_already_logged
  end
  @event_loop.fire_every(Agent.config[:data_report_period],       :report_data)
  @event_loop.fire_every(report_period_for(:analytic_event_data), :report_event_data)
  @event_loop.fire_every(LOG_ONCE_KEYS_RESET_PERIOD,              :reset_log_once_keys)

  @event_loop.run
end
create_event_loop() click to toggle source
# File lib/mmtrix/agent/agent.rb, line 582
def create_event_loop
  EventLoop.new
end
deferred_work!(connection_options) click to toggle source

This is the method that is run in a new thread in order to background the harvesting and sending of data during the normal operation of the agent.

Takes connection options that determine how we should connect to the server, and loops endlessly - typically we never return from this method unless we’re shutting down the agent

# File lib/mmtrix/agent/agent.rb, line 676
def deferred_work!(connection_options)
  catch_errors do
    Mmtrix::Agent.disable_all_tracing do
      connect(connection_options)
      if connected?
        create_and_run_event_loop
        # never reaches here unless there is a problem or
        # the agent is exiting
      else
        ::Mmtrix::Agent.logger.debug "No connection.  Worker thread ending."
      end
    end
  end
end
handle_force_disconnect(error) click to toggle source

when a disconnect is requested, stop the current thread, which is the worker thread that gathers data and talks to the server.

# File lib/mmtrix/agent/agent.rb, line 638
def handle_force_disconnect(error)
  ::Mmtrix::Agent.logger.warn "Mmtrix forced this agent to disconnect (#{error.message})"
  disconnect
end
handle_force_restart(error) click to toggle source

Handles the case where the server tells us to restart - this clears the data, clears connection attempts, and waits a while to reconnect.

# File lib/mmtrix/agent/agent.rb, line 627
def handle_force_restart(error)
  ::Mmtrix::Agent.logger.debug error.message
  drop_buffered_data
  @service.force_restart if @service
  @connect_state = :pending
  sleep 30
end
handle_other_error(error) click to toggle source

Handles an unknown error in the worker thread by logging it and disconnecting the agent, since we are now in an unknown state.

# File lib/mmtrix/agent/agent.rb, line 646
def handle_other_error(error)
  ::Mmtrix::Agent.logger.error "Unhandled error in worker thread, disconnecting this agent process:"
  # These errors are fatal (that is, they will prevent the agent from
  # reporting entirely), so we really want backtraces when they happen
  ::Mmtrix::Agent.logger.log_exception(:error, error)
  disconnect
end
report_period_for(method) click to toggle source
# File lib/mmtrix/agent/agent.rb, line 590
def report_period_for(method)
  config_key = "data_report_periods.#{method}".to_sym
  period = Agent.config[config_key]
  if !period
    period = Agent.config[:data_report_period]
    ::Mmtrix::Agent.logger.warn("Could not find configured period for #{method}, falling back to data_report_period (#{period} s)")
  end
  if period < MIN_ALLOWED_REPORT_PERIOD
    ::Mmtrix::Agent.logger.warn("Configured #{config_key} was #{period}, but minimum allowed is #{MIN_ALLOWED_REPORT_PERIOD}, using #{MIN_ALLOWED_REPORT_PERIOD}.")
    period = MIN_ALLOWED_REPORT_PERIOD
  end
  period
end