Class: Daikon::Client
- Inherits:
-
Object
- Object
- Daikon::Client
- Defined in:
- lib/daikon/client.rb
Constant Summary collapse
- EXCEPTIONS =
[Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError, JSON::ParserError, Excon::Errors::SocketError]
Instance Attribute Summary collapse
-
#config ⇒ Object
Returns the value of attribute config.
-
#http ⇒ Object
Returns the value of attribute http.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#monitor ⇒ Object
Returns the value of attribute monitor.
-
#redis ⇒ Object
Returns the value of attribute redis.
Instance Method Summary collapse
- #connect ⇒ Object
- #exception(error) ⇒ Object
- #log(message) ⇒ Object
- #push(method, path, body) ⇒ Object
- #report_info ⇒ Object
- #request(method, path, options = {}) ⇒ Object
- #rotate_monitor(start, stop) ⇒ Object
- #setup(config, logger = nil) ⇒ Object
- #start_monitor ⇒ Object
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
10 11 12 |
# File 'lib/daikon/client.rb', line 10 def config @config end |
#http ⇒ Object
Returns the value of attribute http.
10 11 12 |
# File 'lib/daikon/client.rb', line 10 def http @http end |
#logger ⇒ Object
Returns the value of attribute logger.
10 11 12 |
# File 'lib/daikon/client.rb', line 10 def logger @logger end |
#monitor ⇒ Object
Returns the value of attribute monitor.
10 11 12 |
# File 'lib/daikon/client.rb', line 10 def monitor @monitor end |
#redis ⇒ Object
Returns the value of attribute redis.
10 11 12 |
# File 'lib/daikon/client.rb', line 10 def redis @redis end |
Instance Method Details
#connect ⇒ Object
22 23 24 |
# File 'lib/daikon/client.rb', line 22 def connect Redis.connect(:url => config.redis_url) end |
#exception(error) ⇒ Object
34 35 36 37 38 39 |
# File 'lib/daikon/client.rb', line 34 def exception(error) log error.to_s error.backtrace.each do |line| log line end end |
#log(message) ⇒ Object
30 31 32 |
# File 'lib/daikon/client.rb', line 30 def log() logger.info if logger end |
#push(method, path, body) ⇒ Object
51 52 53 54 55 56 57 |
# File 'lib/daikon/client.rb', line 51 def push(method, path, body) json = body.to_json request(method, path, :body => json, :headers => {"Content-Length" => json.size.to_s, "Content-Type" => "application/json"}) end |
#report_info ⇒ Object
70 71 72 73 74 75 |
# File 'lib/daikon/client.rb', line 70 def report_info push :post, "/api/v1/infos.json", redis.info rescue *EXCEPTIONS => ex log "Error when reporting info" exception(ex) end |
#request(method, path, options = {}) ⇒ Object
41 42 43 44 45 46 47 48 49 |
# File 'lib/daikon/client.rb', line 41 def request(method, path, = {}) [:method] = method.to_s.upcase [:path] = path [:headers] ||= {} [:headers]['Authorization'] = config.api_key http.reset http.request() end |
#rotate_monitor(start, stop) ⇒ Object
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/daikon/client.rb', line 59 def rotate_monitor(start, stop) payload = monitor.rotate.merge({ "created_at" => stop }) push :post, "/api/v1/summaries.json", payload rescue *EXCEPTIONS => ex log "Error when rotating monitor" exception(ex) end |
#setup(config, logger = nil) ⇒ Object
12 13 14 15 16 17 18 19 20 |
# File 'lib/daikon/client.rb', line 12 def setup(config, logger = nil) self.config = config self.logger = logger self.redis = connect self.monitor = Monitor.new(connect, logger) self.http = Excon.new(config.server_prefix) log "Started Daikon v#{VERSION}" end |
#start_monitor ⇒ Object
26 27 28 |
# File 'lib/daikon/client.rb', line 26 def start_monitor monitor.start end |