Class: MinioRunner::Network

Inherits:
Object
  • Object
show all
Defined in:
lib/minio_runner/network.rb

Defined Under Namespace

Classes: NetworkError

Constant Summary collapse

LONG_RESPONSE_TIME_SECONDS =
3
MAC_OS_LOCAL_DOMAIN_ERROR_MESSAGE =
"For macOS, there are some issues which cause large delays for .local domain names. See\nhttps://superuser.com/a/1297335/245469 and https://stackoverflow.com/a/17982964/875941. To\nresolve this, you need to add IPV6 lookup addresses to the hosts file, and it helps to put\nall the entries on one line.\n\n::1 minio.local testbucket.minio.local\nfe80::1%lo0 minio.local testbucket.minio.local\n127.0.0.1 minio.local testbucket.minio.local\n"

Class Method Summary collapse

Class Method Details

.download(url, &block) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/minio_runner/network.rb', line 60

def download(url, &block)
  file_name = File.basename(url)
  tempfile =
    Tempfile.open(["", file_name], binmode: true) do |file|
      file.print Network.get(url)
      file
    end

  raise "Could not download #{url}" unless File.exist?(tempfile.to_path)

  MinioRunner.logger.debug("Successfully downloaded #{tempfile.to_path}")

  yield tempfile if block_given?
ensure
  tempfile&.close!
end

.get(url) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/minio_runner/network.rb', line 24

def get(url)
  MinioRunner.logger.debug("Making network call to #{url}")
  uri = URI(url)
  response = nil
  request_start_time = Time.now

  begin
    Net::HTTP.start(
      uri.host,
      uri.port,
      use_ssl: uri.scheme == "https",
      open_timeout: MinioRunner::System.mac? ? 10 : 3,
    ) { |http| response = http.get(uri.path) }
  rescue SocketError, Net::OpenTimeout => err
    MinioRunner.logger.debug(
      "Connection error when checking minio server health: #{err.message}",
    )
    log_time_error(request_start_time)
    raise MinioRunner::Network::NetworkError.new(
            "Connection error, cannot reach URL: #{url} (#{err.class})",
          )
  end

  MinioRunner.logger.debug("Get response: #{response.inspect}")

  case response
  when Net::HTTPSuccess
    log_time_error(request_start_time)
    response.body
  else
    raise MinioRunner::Network::NetworkError.new(
            "#{response.class::EXCEPTION_TYPE}: #{response.code} \"#{response.message}\" with #{url}",
          )
  end
end

.log_time_error(request_start_time) ⇒ Object



77
78
79
80
81
82
# File 'lib/minio_runner/network.rb', line 77

def log_time_error(request_start_time)
  if (Time.now - request_start_time) > Network::LONG_RESPONSE_TIME_SECONDS &&
       MinioRunner.config.minio_domain.end_with?(".local") && MinioRunner::System.mac?
    MinioRunner.logger.warn(MAC_OS_LOCAL_DOMAIN_ERROR_MESSAGE)
  end
end