Class: Freyr::Pinger
- Inherits:
-
Object
- Object
- Freyr::Pinger
- Defined in:
- lib/freyr/pinger.rb
Instance Attribute Summary collapse
-
#response ⇒ Object
readonly
Response object.
-
#time ⇒ Object
readonly
Time that the ping took.
-
#url ⇒ Object
readonly
URL to ping.
Instance Method Summary collapse
- #code ⇒ Object
-
#initialize(service) ⇒ Pinger
constructor
A new instance of Pinger.
-
#ping ⇒ Object
Send a ping to the url.
-
#server_error? ⇒ Boolean
Did the response recieve a 500 error.
-
#server_probably_launched? ⇒ Boolean
Did it receive 2xx or 500.
-
#success? ⇒ Boolean
Did the response recieve a success http code.
-
#uri ⇒ Object
The URI object for the given URL.
- #wait_for_resp(wait = 40, interval = 0.6, &blk) ⇒ Object
Constructor Details
#initialize(service) ⇒ Pinger
Returns a new instance of Pinger.
12 13 14 15 |
# File 'lib/freyr/pinger.rb', line 12 def initialize(service) @service = service @url = service.info.ping end |
Instance Attribute Details
#response ⇒ Object (readonly)
Response object
10 11 12 |
# File 'lib/freyr/pinger.rb', line 10 def response @response end |
#time ⇒ Object (readonly)
Time that the ping took
8 9 10 |
# File 'lib/freyr/pinger.rb', line 8 def time @time end |
#url ⇒ Object (readonly)
URL to ping
6 7 8 |
# File 'lib/freyr/pinger.rb', line 6 def url @url end |
Instance Method Details
#code ⇒ Object
57 58 59 |
# File 'lib/freyr/pinger.rb', line 57 def code response.code if response end |
#ping ⇒ Object
Send a ping to the url
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/freyr/pinger.rb', line 46 def ping t = Time.now @response = Net::HTTP.get_response(uri) rescue Errno::ECONNREFUSED rescue Exception => e STDERR.puts "Error fetching #{@url}: #{e}" ensure @time = Time.now-t @response end |
#server_error? ⇒ Boolean
Did the response recieve a 500 error
67 68 69 |
# File 'lib/freyr/pinger.rb', line 67 def server_error? response.is_a?(Net::HTTPInternalServerError) end |
#server_probably_launched? ⇒ Boolean
Did it receive 2xx or 500
72 73 74 |
# File 'lib/freyr/pinger.rb', line 72 def server_probably_launched? success? || server_error? end |
#success? ⇒ Boolean
Did the response recieve a success http code
62 63 64 |
# File 'lib/freyr/pinger.rb', line 62 def success? response.is_a?(Net::HTTPSuccess) || response.is_a?(Net::HTTPRedirection) end |
#uri ⇒ Object
The URI object for the given URL
41 42 43 |
# File 'lib/freyr/pinger.rb', line 41 def uri @uri ||= URI.parse(url) end |
#wait_for_resp(wait = 40, interval = 0.6, &blk) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/freyr/pinger.rb', line 17 def wait_for_resp wait=40, interval = 0.6, &blk OUT.puts "\nWaiting for response from #{url}" start = Time.now blk ||= lambda {true} begin OUT.print '.'; OUT.flush ping sleep(interval) end until server_probably_launched? || (Time.now-start) > wait || !blk.call if blk.call if response OUT.puts '*', "Last response received with code #{response.code}" else OUT.puts 'x', "Couldn't reach #{@service.name} service" end else OUT.puts 'x',"Service died durring launch" end end |