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
219 220 221 222 223 224 225 226 227 |
# File 'lib/tasks/ci/common.rb', line 219 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
215 216 217 |
# File 'lib/tasks/ci/common.rb', line 215 def self.check_file(file_path) File.exist?(file_path) end |
.check_port(port) ⇒ Object
188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/tasks/ci/common.rb', line 188 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, EOFError, Errno::ECONNRESET return false end end rescue Timeout::Error return false end |
.check_url(url) ⇒ Object
202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/tasks/ci/common.rb', line 202 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, EOFError, Errno::ECONNRESET return false end end rescue Timeout::Error return false end |
.for(smth, max_timeout = DEFAULT_TIMEOUT) ⇒ Object
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/tasks/ci/common.rb', line 229 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 |