Class: Wait
- Inherits:
-
Object
- Object
- Wait
- Defined in:
- lib/tasks/ci/common.rb
Overview
helper class to wait for TCP/HTTP services to boot
Constant Summary collapse
- DEFAULT_TIMEOUT =
10
Class Method Summary collapse
- .check(smth) ⇒ Object
- .check_file(file_path) ⇒ Object
- .check_port(port) ⇒ Object
- .check_url(url) ⇒ Object
- .for(smth, max_timeout = DEFAULT_TIMEOUT) ⇒ Object
Class Method Details
.check(smth) ⇒ Object
112 113 114 115 116 117 118 119 120 |
# File 'lib/tasks/ci/common.rb', line 112 def self.check(smth) if smth.is_a? Integer check_port smth elsif smth.include? 'http' check_url smth else check_file smth end end |
.check_file(file_path) ⇒ Object
108 109 110 |
# File 'lib/tasks/ci/common.rb', line 108 def self.check_file(file_path) File.exist?(file_path) end |
.check_port(port) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/tasks/ci/common.rb', line 81 def self.check_port(port) Timeout.timeout(0.5) do begin s = TCPSocket.new('localhost', port) s.close return true rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH return false end end rescue Timeout::Error return false end |
.check_url(url) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/tasks/ci/common.rb', line 95 def self.check_url(url) Timeout.timeout(0.5) do begin r = HTTParty.get(url) return (200...300).cover? r.code rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH return false end end rescue Timeout::Error return false end |
.for(smth, max_timeout = DEFAULT_TIMEOUT) ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/tasks/ci/common.rb', line 122 def self.for(smth, max_timeout = DEFAULT_TIMEOUT) start_time = Time.now status = false n = 1 puts "Trying #{smth}" loop do puts n.to_s status = check(smth) break if status || Time.now > start_time + max_timeout n += 1 sleep 0.25 end raise "Still not up after #{max_timeout}s" unless status puts 'Found!' status end |