diff --git a/ecosystem/platform/server/app/helpers/node_helper.rb b/ecosystem/platform/server/app/helpers/node_helper.rb index 5b7167177e43c..6299a5da7ea15 100644 --- a/ecosystem/platform/server/app/helpers/node_helper.rb +++ b/ecosystem/platform/server/app/helpers/node_helper.rb @@ -56,6 +56,8 @@ def initialize(hostname, metrics_port, http_api_port) # @return IPResult def resolve_ip + return IPResult.new(true, @hostname, nil) if @hostname =~ Resolv::IPv4::Regex + resolved_ip = Resolv::DNS.open do |dns| dns.timeouts = 0.5 dns.getaddress @hostname @@ -67,11 +69,13 @@ def resolve_ip # @return [LocationResult] def location + return LocationResult(false, "Can not fetch location with no IP: #{@ip.message}", nil) unless @ip.ok + client = MaxMind::GeoIP2::Client.new( account_id: ENV.fetch('MAXMIND_ACCOUNT_ID'), license_key: ENV.fetch('MAXMIND_LICENSE_KEY') ) - LocationResult.new(true, nil, client.insights(@ip)) + LocationResult.new(true, nil, client.insights(@ip.ip)) rescue StandardError => e Sentry.capture_exception(e) LocationResult.new(false, "Error: #{e}", nil) diff --git a/ecosystem/platform/server/app/jobs/location_job.rb b/ecosystem/platform/server/app/jobs/location_job.rb index 262a07c3ffa08..db56a261ccfe6 100644 --- a/ecosystem/platform/server/app/jobs/location_job.rb +++ b/ecosystem/platform/server/app/jobs/location_job.rb @@ -14,7 +14,11 @@ def perform(args) # pass zeroes as a hack here: we only need the validator address node_verifier = NodeHelper::NodeVerifier.new(it1_profile.validator_address, 0, 0) - location_res = node_verifier.location + + unless node_verifier.ip.ok + raise LocationFetchError, + "Error fetching IP for #{it1_profile.validator_address}: #{node_verifier.ip.message}" + end new_ip = node_verifier.ip.ip if new_ip != it1_profile.validator_ip @@ -22,11 +26,13 @@ def perform(args) "#{it1_profile.validator_address}: was #{it1_profile.validator_ip}, got #{new_ip}" end + location_res = node_verifier.location + unless location_res.ok # TODO: DO SOMETHING (SENTRY? THROW?) IF THIS IS NOT OK - raise LocationFetchError("Error fetching location for #{it1_profile.validator_ip}: #{location_res.message}") + raise LocationFetchError, "Error fetching location for '#{it1_profile.validator_ip}': #{location_res.message}" end - Location.upsert_from_maxmind!(@it1_profile, location_res.record) + Location.upsert_from_maxmind!(it1_profile, location_res.record) end end diff --git a/ecosystem/platform/server/app/models/location.rb b/ecosystem/platform/server/app/models/location.rb index 7d842e63ced0d..f618e1deb446b 100644 --- a/ecosystem/platform/server/app/models/location.rb +++ b/ecosystem/platform/server/app/models/location.rb @@ -7,7 +7,7 @@ class Location < ApplicationRecord # @param [MaxMind::GeoIP2::Model::Insights] data def self.upsert_from_maxmind!(item, data) - item.location = (item.location || Location.new(attr)).tap { |location| location.assign_from_maxmind(data) } + item.location = (item&.location || Location.new).tap { |location| location.assign_from_maxmind(data) } item.location.save! end diff --git a/ecosystem/platform/server/app/views/it1_profiles/_form.html.erb b/ecosystem/platform/server/app/views/it1_profiles/_form.html.erb index 795a9ecfa0fdf..b77e09ba99429 100644 --- a/ecosystem/platform/server/app/views/it1_profiles/_form.html.erb +++ b/ecosystem/platform/server/app/views/it1_profiles/_form.html.erb @@ -67,7 +67,9 @@