Class: Pod

Inherits:
ApplicationRecord show all
Defined in:
app/models/pod.rb

Constant Summary collapse

ERROR_MAP =
{
  ConnectionTester::AddressFailure  => :dns_failed,
  ConnectionTester::DNSFailure      => :dns_failed,
  ConnectionTester::NetFailure      => :net_failed,
  ConnectionTester::SSLFailure      => :ssl_failed,
  ConnectionTester::HTTPFailure     => :http_failed,
  ConnectionTester::NodeInfoFailure => :version_failed
}
CURL_ERROR_MAP =

this are only the most common errors, the rest will be unknown_error

{
  couldnt_resolve_host:         :dns_failed,
  couldnt_connect:              :net_failed,
  operation_timedout:           :net_failed,
  ssl_cipher:                   :ssl_failed,
  ssl_cacert:                   :ssl_failed,
  redirected_to_other_hostname: :http_failed
}.freeze
DEFAULT_PORTS =
[URI::HTTP::DEFAULT_PORT, URI::HTTPS::DEFAULT_PORT]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.check_all!Object


63
64
65
# File 'app/models/pod.rb', line 63

def check_all!
  Pod.find_in_batches(batch_size: 20) {|batch| batch.each(&:test_connection!) }
end

.check_scheduled!Object


67
68
69
# File 'app/models/pod.rb', line 67

def check_scheduled!
  Pod.where(scheduled_check: true).find_each(&:test_connection!)
end

.find_or_create_by(opts) ⇒ Object

Rename this method to not override an AR method


45
46
47
48
49
50
51
52
# File 'app/models/pod.rb', line 45

def find_or_create_by(opts) # Rename this method to not override an AR method
  uri = URI.parse(opts.fetch(:url))
  port = DEFAULT_PORTS.include?(uri.port) ? nil : uri.port
  find_or_initialize_by(host: uri.host, port: port).tap do |pod|
    pod.ssl ||= (uri.scheme == "https")
    pod.save
  end
end

.offline_statusesObject

don't consider a failed version reading to be fatal


55
56
57
58
59
60
61
# File 'app/models/pod.rb', line 55

def offline_statuses
  [Pod.statuses[:dns_failed],
   Pod.statuses[:net_failed],
   Pod.statuses[:ssl_failed],
   Pod.statuses[:http_failed],
   Pod.statuses[:unknown_error]]
end

Instance Method Details

#active?Boolean

a pod is active if it is online or was online less than 14 days ago

Returns:

  • (Boolean)

77
78
79
# File 'app/models/pod.rb', line 77

def active?
  !offline? || offline_since.try {|date| date > DateTime.now.utc - 14.days }
end

#offline?Boolean

Returns:

  • (Boolean)

72
73
74
# File 'app/models/pod.rb', line 72

def offline?
  Pod.offline_statuses.include?(Pod.statuses[status])
end

#schedule_check_if_neededObject


85
86
87
# File 'app/models/pod.rb', line 85

def schedule_check_if_needed
  update_column(:scheduled_check, true) if offline? && !scheduled_check
end

#test_connection!Object


89
90
91
92
93
94
95
96
# File 'app/models/pod.rb', line 89

def test_connection!
  result = ConnectionTester.check uri.to_s
  logger.debug "tested pod: '#{uri}' - #{result.inspect}"

  transaction do
    update_from_result(result)
  end
end

#to_sObject


81
82
83
# File 'app/models/pod.rb', line 81

def to_s
  "#{id}:#{host}"
end

#update_offline_sinceObject


104
105
106
107
108
109
110
# File 'app/models/pod.rb', line 104

def update_offline_since
  if offline?
    self.offline_since ||= DateTime.now.utc
  else
    self.offline_since = nil
  end
end

#url_to(path) ⇒ String

Parameters:

Returns:


100
101
102
# File 'app/models/pod.rb', line 100

def url_to(path)
  uri.tap {|uri| uri.path = path }.to_s
end