mirror of
https://github.com/lingble/chatwoot.git
synced 2025-10-29 18:22:53 +00:00
fix: ip-lookup database lazy loading for all environments (#8052)
The current task for loading `GeoLite2-City.mmdb` doesn't work for all build types. This PR addresses this and move the task to initializer to ensure consistency across environments. --------- Co-authored-by: Sojan Jose <sojan@pepalo.com> Co-authored-by: Sojan Jose <sojan.official@gmail.com>
This commit is contained in:
49
app/services/geocoder/setup_service.rb
Normal file
49
app/services/geocoder/setup_service.rb
Normal file
@@ -0,0 +1,49 @@
|
||||
require 'rubygems/package'
|
||||
|
||||
class Geocoder::SetupService
|
||||
def perform
|
||||
return if File.exist?(GeocoderConfiguration::LOOK_UP_DB)
|
||||
|
||||
ip_lookup_api_key = ENV.fetch('IP_LOOKUP_API_KEY', nil)
|
||||
if ip_lookup_api_key.blank?
|
||||
log_info('IP_LOOKUP_API_KEY empty. Skipping geoip database setup')
|
||||
return
|
||||
end
|
||||
|
||||
log_info('Fetch GeoLite2-City database')
|
||||
fetch_and_extract_database(ip_lookup_api_key)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def fetch_and_extract_database(api_key)
|
||||
base_url = ENV.fetch('IP_LOOKUP_BASE_URL', 'https://download.maxmind.com/app/geoip_download')
|
||||
source_file = Down.download("#{base_url}?edition_id=GeoLite2-City&suffix=tar.gz&license_key=#{api_key}")
|
||||
|
||||
extract_tar_file(source_file)
|
||||
log_info('Fetch complete')
|
||||
rescue StandardError => e
|
||||
log_error(e.message)
|
||||
end
|
||||
|
||||
def extract_tar_file(source_file)
|
||||
tar_extract = Gem::Package::TarReader.new(Zlib::GzipReader.open(source_file))
|
||||
tar_extract.rewind
|
||||
|
||||
tar_extract.each do |entry|
|
||||
next unless entry.full_name.include?('GeoLite2-City.mmdb') && entry.file?
|
||||
|
||||
File.open GeocoderConfiguration::LOOK_UP_DB, 'wb' do |f|
|
||||
f.print entry.read
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def log_info(message)
|
||||
Rails.logger.info "[rake ip_lookup:setup] #{message}"
|
||||
end
|
||||
|
||||
def log_error(message)
|
||||
Rails.logger.error "[rake ip_lookup:setup] #{message}"
|
||||
end
|
||||
end
|
||||
@@ -24,3 +24,7 @@ module GeocoderConfiguration
|
||||
end
|
||||
|
||||
Geocoder.configure(ip_lookup: :geoip2, geoip2: { file: GeocoderConfiguration::LOOK_UP_DB }) if ENV['IP_LOOKUP_API_KEY'].present?
|
||||
|
||||
Rails.application.config.after_initialize do
|
||||
Geocoder::SetupService.new.perform
|
||||
end
|
||||
|
||||
@@ -2,35 +2,6 @@ require 'rubygems/package'
|
||||
|
||||
namespace :ip_lookup do
|
||||
task setup: :environment do
|
||||
next if File.exist?(GeocoderConfiguration::LOOK_UP_DB)
|
||||
|
||||
ip_lookup_api_key = ENV.fetch('IP_LOOKUP_API_KEY', nil)
|
||||
if ip_lookup_api_key.blank?
|
||||
Rails.logger.info '[rake ip_lookup:setup] IP_LOOKUP_API_KEY empty. Skipping geoip database setup'
|
||||
next
|
||||
end
|
||||
|
||||
Rails.logger.info '[rake ip_lookup:setup] Fetch GeoLite2-City database'
|
||||
|
||||
begin
|
||||
base_url = ENV.fetch('IP_LOOKUP_BASE_URL', 'https://download.maxmind.com/app/geoip_download')
|
||||
source_file = Down.download(
|
||||
"#{base_url}?edition_id=GeoLite2-City&suffix=tar.gz&license_key=#{ip_lookup_api_key}"
|
||||
)
|
||||
|
||||
tar_extract = Gem::Package::TarReader.new(Zlib::GzipReader.open(source_file))
|
||||
tar_extract.rewind
|
||||
|
||||
tar_extract.each do |entry|
|
||||
next unless entry.full_name.include?('GeoLite2-City.mmdb') && entry.file?
|
||||
|
||||
File.open GeocoderConfiguration::LOOK_UP_DB, 'wb' do |f|
|
||||
f.print entry.read
|
||||
end
|
||||
end
|
||||
Rails.logger.info '[rake ip_lookup:setup] Fetch complete'
|
||||
rescue StandardError => e
|
||||
Rails.logger.error "[rake ip_lookup:setup] #{e.message}"
|
||||
end
|
||||
Geocoder::SetupService.new.perform
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user