Lots of updates
This commit is contained in:
38
app/jobs/fetch_ipapi_data_job.rb
Normal file
38
app/jobs/fetch_ipapi_data_job.rb
Normal file
@@ -0,0 +1,38 @@
|
||||
class FetchIpapiDataJob < ApplicationJob
|
||||
queue_as :default
|
||||
|
||||
# Fetches IPAPI enrichment data for a NetworkRange
|
||||
# @param network_range_id [Integer] ID of the NetworkRange to enrich
|
||||
def perform(network_range_id:)
|
||||
network_range = NetworkRange.find_by(id: network_range_id)
|
||||
return unless network_range
|
||||
|
||||
# Skip if we already have IPAPI data and it's recent (< 30 days old)
|
||||
if network_range.has_network_data_from?(:ipapi) &&
|
||||
network_range.last_api_fetch.present? &&
|
||||
network_range.last_api_fetch > 30.days.ago
|
||||
Rails.logger.info "Skipping IPAPI fetch for #{network_range.cidr} - data is recent"
|
||||
return
|
||||
end
|
||||
|
||||
# Use the network address (first IP in range) as the representative IP
|
||||
sample_ip = network_range.network_address.split('/').first
|
||||
|
||||
Rails.logger.info "Fetching IPAPI data for #{network_range.cidr} using IP #{sample_ip}"
|
||||
|
||||
ipapi_data = Ipapi.lookup(sample_ip)
|
||||
|
||||
if ipapi_data.present? && !ipapi_data.key?('error')
|
||||
network_range.set_network_data(:ipapi, ipapi_data)
|
||||
network_range.last_api_fetch = Time.current
|
||||
network_range.save!
|
||||
|
||||
Rails.logger.info "Successfully fetched IPAPI data for #{network_range.cidr}"
|
||||
else
|
||||
Rails.logger.warn "IPAPI returned error for #{network_range.cidr}: #{ipapi_data}"
|
||||
end
|
||||
rescue => e
|
||||
Rails.logger.error "Failed to fetch IPAPI data for network_range #{network_range_id}: #{e.message}"
|
||||
Rails.logger.error e.backtrace.join("\n")
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user