Skip to content

Commit 692e7b4

Browse files
authored
Change how we are pulling the maxmind db (#1190)
Update the way we are pulling in the maxmind db on intialization and set fallback if the file doesnt download.
1 parent 3f36685 commit 692e7b4

5 files changed

+25
-12
lines changed

Gemfile.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,7 @@ DEPENDENCIES
697697
webpacker (~> 5.0.1)
698698

699699
RUBY VERSION
700-
ruby 2.6.5p114
700+
ruby 2.6.6p146
701701

702702
BUNDLED WITH
703703
2.1.4

app/jobs/download_and_extract_maxmind_file_job.rb

+11-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
require "rubygems/package"
55

66
class DownloadAndExtractMaxmindFileJob < ApplicationJob
7-
MAXMIND_URI = URI("http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.tar.gz")
8-
MAXMIND_DIR = Rails.root.join("db/maxmind")
7+
MAXMIND_URI = URI("https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-City&license_key=#{ENV["MAXMIND_LICENSE_KEY"]}&suffix=tar.gz")
8+
MAXMIND_DIR = Rails.root.join("tmp/maxmind")
99
MAXMIND_PATH = MAXMIND_DIR.join("GeoLite2-City.tar.gz")
10+
MAXMIND_LEGACY_DIR = Rails.root.join("db/maxmind")
11+
MAXMIND_LEGACY_PATH = MAXMIND_LEGACY_DIR.join("GeoLite2-City.tar.gz")
1012

1113
queue_as :low
1214

@@ -21,14 +23,20 @@ def perform
2123

2224
def download
2325
FileUtils.rm_f MAXMIND_PATH
26+
2427
File.open MAXMIND_PATH, "wb" do |file|
2528
Net::HTTP.start(MAXMIND_URI.host) do |http|
2629
http.open_timeout = 1
2730
http.read_timeout = 1
28-
http.request_get(MAXMIND_URI.path) do |response|
31+
http.request_get(MAXMIND_URI) do |response|
32+
throw :abort if response.body.empty?
2933
response.read_body { |segment| file.write segment }
3034
end
3135
end
36+
rescue => e
37+
FileUtils.rm_f MAXMIND_PATH
38+
FileUtils.cp MAXMIND_LEGACY_PATH, MAXMIND_PATH
39+
logger.error "Error downloading maxmind file! #{e.message}"
3240
end
3341
end
3442

config/initializers/maxmind.rb

+5-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
DownloadAndExtractMaxmindFileJob.new.extract
1+
if Rails.env.production? || ENV["CI"] == "true"
2+
DownloadAndExtractMaxmindFileJob.perform_now
3+
else
4+
DownloadAndExtractMaxmindFileJob.perform_later
5+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
require "test_helper"
2+
3+
class DownloadAndExtractMaxmindFilesJobTest < ActiveJob::TestCase
4+
test "download" do
5+
stub_request(:get, /download\.maxmind\.com/).to_return(status: 200)
6+
DownloadAndExtractMaxmindFileJob.perform_now
7+
end
8+
end

test/jobs/download_maxmind_files_job_test.rb

-7
This file was deleted.

0 commit comments

Comments
 (0)