Module: TingYun::Agent::InstanceMethods::Connect
- Includes:
- HandleErrors
- Included in:
- TingYun::Agent::InstanceMethods
- Defined in:
- lib/ting_yun/agent/instance_methods/connect.rb
Instance Attribute Summary collapse
-
#connect_attempts ⇒ Object
number of attempts we’ve made to contact the server.
Instance Method Summary collapse
-
#catch_errors ⇒ Object
-
:keep_retrying => falseto only try to connect once, and return with the connection set to nil.
-
- #connect_in_sync ⇒ Object
-
#connect_retry_period ⇒ Object
Retry period is a minute for each failed attempt that we’ve made.
-
#connect_settings ⇒ Object
Initializes the hash of settings that we send to the server.
-
#connect_to_server ⇒ Object
Returns connect data passed back from the server.
- #connected? ⇒ Boolean
-
#disconnect ⇒ Object
Disconnect just sets connected to false, which prevents the agent from trying to connect again.
- #disconnected? ⇒ Boolean
-
#environment_for_connect ⇒ Object
Checks whether we should send environment info, and if so, returns the snapshot from the local environment.
-
#finish_setup(config_data) ⇒ Object
Takes a hash of configuration data returned from the server and uses it to set local variables and to initialize various parts of the agent that are configured separately.
- #generate_environment_report ⇒ Object
- #local_host ⇒ Object
- #log_collector_messages(messages) ⇒ Object
- #log_connection!(config_data) ⇒ Object
- #note_connect_failure ⇒ Object
-
#query_server_for_configuration ⇒ Object
merge server config.
-
#sanitize_environment_report ⇒ Object
We’ve seen objects in the environment report (Rails.env in particular) that can’t seralize to JSON.
-
#should_connect?(force = false) ⇒ Boolean
Don’t connect if we’re already connected, or if we tried to connect and were rejected with prejudice because of a license issue, unless we’re forced to by force_reconnect.
Methods included from HandleErrors
#handle_force_disconnect, #handle_force_restart, #handle_license_error, #handle_other_error, #handle_server_error, #handle_unrecoverable_agent_error, #log_error
Instance Attribute Details
#connect_attempts ⇒ Object
number of attempts we’ve made to contact the server
18 19 20 |
# File 'lib/ting_yun/agent/instance_methods/connect.rb', line 18 def connect_attempts @connect_attempts end |
Instance Method Details
#catch_errors ⇒ Object
-
:keep_retrying => falseto only try to connect once, and return with the connection set to nil. This ensures we may try again later (default true). -
force_reconnect => trueif you want to establish a new connection to the server before running the worker loop. This means you get a separate agent run and Ting Yun sees it as a separate instance (default is false).
115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/ting_yun/agent/instance_methods/connect.rb', line 115 def catch_errors yield rescue TingYun::Support::Exception::ExpiredConfigurationException => e handle_force_restart(e) retry rescue TingYun::Support::Exception::InvalidDataTokenException => e handle_force_restart(e) retry rescue TingYun::Support::Exception::InvalidDataException => e handle_server_error(e) rescue => e handle_other_error(e) end |
#connect_in_sync ⇒ Object
162 163 164 |
# File 'lib/ting_yun/agent/instance_methods/connect.rb', line 162 def connect_in_sync TingYun::Agent.disable_all_tracing { connect!(:keep_retrying => false) } end |
#connect_retry_period ⇒ Object
Retry period is a minute for each failed attempt that we’ve made. This should probably do some sort of sane TCP backoff to prevent hammering the server, but a minute for each attempt seems to work reasonably well.
46 47 48 |
# File 'lib/ting_yun/agent/instance_methods/connect.rb', line 46 def connect_retry_period [600, connect_attempts * 60].min end |
#connect_settings ⇒ Object
Initializes the hash of settings that we send to the server. Returns a literal hash containing the options
80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/ting_yun/agent/instance_methods/connect.rb', line 80 def connect_settings sanitize_environment_report settings = { :pid => $$, :port => ::TingYun::Agent.config[:port], :host => local_host, :appName => ::TingYun::Agent.config.app_names, :language => 'Ruby', :agentVersion => ::TingYun::VERSION::STRING, :env => @environment_report, :config => ::TingYun::Agent.config.to_collector_hash } settings end |
#connect_to_server ⇒ Object
Returns connect data passed back from the server
100 101 102 |
# File 'lib/ting_yun/agent/instance_methods/connect.rb', line 100 def connect_to_server @service.connect(connect_settings) end |
#connected? ⇒ Boolean
27 28 29 |
# File 'lib/ting_yun/agent/instance_methods/connect.rb', line 27 def connected? @connect_state == :connected end |
#disconnect ⇒ Object
Disconnect just sets connected to false, which prevents the agent from trying to connect again
22 23 24 25 |
# File 'lib/ting_yun/agent/instance_methods/connect.rb', line 22 def disconnect @connect_state = :disconnected true end |
#disconnected? ⇒ Boolean
31 32 33 |
# File 'lib/ting_yun/agent/instance_methods/connect.rb', line 31 def disconnected? @connect_state == :disconnected end |
#environment_for_connect ⇒ Object
Checks whether we should send environment info, and if so, returns the snapshot from the local environment. Generating the EnvironmentReport has the potential to trigger require calls in Rails environments, so this method should only be called synchronously from on the main thread.
73 74 75 |
# File 'lib/ting_yun/agent/instance_methods/connect.rb', line 73 def environment_for_connect ::TingYun::Agent.config[:send_environment_info] ? TingYun::EnvironmentReport.new.data : {} end |
#finish_setup(config_data) ⇒ Object
Takes a hash of configuration data returned from the server and uses it to set local variables and to initialize various parts of the agent that are configured separately.
135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/ting_yun/agent/instance_methods/connect.rb', line 135 def finish_setup(config_data) return if config_data == nil if config_data['config'] ::TingYun::Agent.logger.debug "Using config from server" end ::TingYun::Agent.logger.debug "Server provided config: #{config_data.inspect}" server_config = TingYun::Configuration::ServerSource.new(config_data) ::TingYun::Agent.config.replace_or_add_config(server_config) #log_connection!(config_data) end |
#generate_environment_report ⇒ Object
54 55 56 |
# File 'lib/ting_yun/agent/instance_methods/connect.rb', line 54 def generate_environment_report @environment_report = environment_for_connect end |
#local_host ⇒ Object
95 96 97 |
# File 'lib/ting_yun/agent/instance_methods/connect.rb', line 95 def local_host TingYun::Support::Hostname.get end |
#log_collector_messages(messages) ⇒ Object
156 157 158 159 160 |
# File 'lib/ting_yun/agent/instance_methods/connect.rb', line 156 def () .each do || ::TingYun::Agent.logger.send(['level'].downcase, ['message']) end end |
#log_connection!(config_data) ⇒ Object
147 148 149 150 151 152 153 154 |
# File 'lib/ting_yun/agent/instance_methods/connect.rb', line 147 def log_connection!(config_data) ::TingYun::Agent.logger.debug "Connected to TingYun Service at #{@service.collector.name}" ::TingYun::Agent.logger.debug "Application Run = #{@service.applicationId}." ::TingYun::Agent.logger.debug "Connection data = #{config_data.inspect}" if config_data['messages'] && config_data['messages'].any? (config_data['messages']) end end |
#note_connect_failure ⇒ Object
50 51 52 |
# File 'lib/ting_yun/agent/instance_methods/connect.rb', line 50 def note_connect_failure self.connect_attempts += 1 end |
#query_server_for_configuration ⇒ Object
merge server config
105 106 107 |
# File 'lib/ting_yun/agent/instance_methods/connect.rb', line 105 def query_server_for_configuration finish_setup(connect_to_server) end |
#sanitize_environment_report ⇒ Object
We’ve seen objects in the environment report (Rails.env in particular) that can’t seralize to JSON. Cope with that here and clear out so downstream code doesn’t have to check again.
61 62 63 64 65 |
# File 'lib/ting_yun/agent/instance_methods/connect.rb', line 61 def sanitize_environment_report if !@service.valid_to_marshal?(@environment_report) @environment_report = {} end end |
#should_connect?(force = false) ⇒ Boolean
Don’t connect if we’re already connected, or if we tried to connect and were rejected with prejudice because of a license issue, unless we’re forced to by force_reconnect.
38 39 40 |
# File 'lib/ting_yun/agent/instance_methods/connect.rb', line 38 def should_connect?(force=false) force || (!connected? && !disconnected?) end |