Class: ScoutAgent::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/scout_agent/server.rb

Overview

This class is a thin wrapper over RestClient for Scout’s check-in API. Public methods are provided for each action you can perform againt the API.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(log = WireTap.new(nil)) ⇒ Server

Create a new API wrapper, optionally with a log to write connection details to.



14
15
16
17
18
19
20
21
22
23
# File 'lib/scout_agent/server.rb', line 14

def initialize(log = WireTap.new(nil))
  @log         = log
  @rest_client = RestClient::Resource.new(
    Plan.agent_url,
    :headers => { :client_version   => ScoutAgent::VERSION,
                  :accept_encoding  => "gzip" }
  )
  # make sure proxy is set, if needed
  RestClient.proxy = Plan.proxy_url
end

Instance Attribute Details

#logObject (readonly)

The log connection notes will be written to.



26
27
28
# File 'lib/scout_agent/server.rb', line 26

def log
  @log
end

Instance Method Details

#get_plan(additional_headers = { }) ⇒ Object

This method fetches the current plan for this agent. You can pass any additional_headers for the request if needed (mainly useful for :if_modified_since).

This method will return the raw plan when it is successfully fetched. An empty String is returned if the plan is malformed or unchanged. Finally, nil is returned if the plan cannot be retrieved for some reason.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/scout_agent/server.rb', line 37

def get_plan(additional_headers = { })
  no_warnings {  # keep OpenSSL quiet
    @rest_client["plan.scout"].get(additional_headers)
  }
rescue Zlib::Error  # could not decompress response
  log.warn("Plan was malformed zipped data.")
  ""  # replace bad plan with empty plan
rescue RestClient::RequestFailed => error  # RestClient bug workaround
  log.warn("Plan was returned as a failure code:  #{error.http_code}.")
  nil  # failed to retrieve plan
rescue RestClient::NotModified
  log.info("Plan was not modified.")
  ""  # empty plan
rescue Exception => error  # networking problem
  log.warn("Plan could not be retrieved:  #{error.class}.")
  nil  # failed to retrieve plan
end

#post_checkin(data) ⇒ Object

This method can be used to send data to the Scout API as a check-in. The data is zipped before it is sent to reduce the cost duplicated reports as much as possible.

If the ckeck-in succeeds, true is returned. However, false doesn’t ensure that we failed. RestClient may timeout a long connection attempt, but the server may still complete it eventually.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/scout_agent/server.rb', line 64

def post_checkin(data)
  io   =  StringIO.new
  gzip =  Zlib::GzipWriter.new(io)
  gzip << data.to_json
  gzip.close
  no_warnings do  # keep OpenSSL quiet
    @rest_client["checkin.scout"].post(
      io.string,
      :content_type     => "application/json",
      :content_encoding => "gzip"
    )
  end
  true
rescue Zlib::Error  # could not compress data for sending
  log.error("Check-in could not be zipped.")
  false
rescue RestClient::RequestFailed => error  # RestClient bug workaround
  log.warn( "Check-in was returned as a failure code:  " +
            "#{error.http_code}." )
  false
rescue Exception => error  # networking problem
  log.warn("Check-in could not be sent:  #{error.class}.")
  false  # we failed to send and will retry later
end

#post_log(log_file) ⇒ Object

Uploads log_file to the server for troubleshooting. Returns true if the upload succeeded.



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/scout_agent/server.rb', line 93

def post_log(log_file)
  no_warnings do  # keep OpenSSL quiet
    @rest_client["log.scout"].post(
      log_file.read,
      :content_type     => "text/plain",
      :content_encoding => "gzip"
    )
  end
  true
rescue RestClient::RequestFailed => error  # RestClient bug workaround
  log.warn( "Log upload was returned as a failure code:  " +
            "#{error.http_code}." )
  false
rescue Errno::ECONNREFUSED, RestClient::Exception  # networking problem
  log.warn("Log could not be sent:  #{error.class}.")
  false  # could not send log
end