Skip to content

Commit

Permalink
[CP] Fix location job (aptos-labs#988)
Browse files Browse the repository at this point in the history
  • Loading branch information
CapCap authored May 13, 2022
1 parent c96baf0 commit a66b0d2
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
6 changes: 5 additions & 1 deletion ecosystem/platform/server/app/helpers/node_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
12 changes: 9 additions & 3 deletions ecosystem/platform/server/app/jobs/location_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,25 @@ 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
Rails.logger.warn "IP does not match one in profile for it1_profile ##{it1_profile.id} - "\
"#{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
2 changes: 1 addition & 1 deletion ecosystem/platform/server/app/models/location.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@
</div>

<div class="mb-16">
<h3 class="text-teal-400 font-mono uppercase text-2xl mb-2">Full Node</h3>
<h3 class="text-teal-400 font-mono uppercase text-2xl mb-2">
Full Node <span class="text-teal-400 opacity-50 font-mono lowercase text-sm mb-2">[optional]</span>
</h3>
<div class="mb-8"><%= render 'shared/bump' %></div>

<div class="mb-4">
Expand Down

0 comments on commit a66b0d2

Please sign in to comment.