mirror of
https://github.com/discourse/discourse.git
synced 2025-06-11 11:03:41 +08:00
DEV: Extract maxmind refresh logic to its own task (#23195)
This commit is contained in:
@ -226,77 +226,7 @@ def log_task_duration(task_description, &task)
|
|||||||
STDERR.puts
|
STDERR.puts
|
||||||
end
|
end
|
||||||
|
|
||||||
def geolite_dbs
|
task "assets:precompile" => %w[assets:precompile:before maxminddb:refresh] do
|
||||||
@geolite_dbs ||= %w[GeoLite2-City GeoLite2-ASN]
|
|
||||||
end
|
|
||||||
|
|
||||||
def get_mmdb_time(root_path)
|
|
||||||
mmdb_time = nil
|
|
||||||
|
|
||||||
geolite_dbs.each do |name|
|
|
||||||
path = File.join(root_path, "#{name}.mmdb")
|
|
||||||
if File.exist?(path)
|
|
||||||
mmdb_time = File.mtime(path)
|
|
||||||
else
|
|
||||||
mmdb_time = nil
|
|
||||||
break
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
mmdb_time
|
|
||||||
end
|
|
||||||
|
|
||||||
def copy_maxmind(from_path, to_path)
|
|
||||||
puts "Copying MaxMindDB from #{from_path} to #{to_path}"
|
|
||||||
|
|
||||||
geolite_dbs.each do |name|
|
|
||||||
from = File.join(from_path, "#{name}.mmdb")
|
|
||||||
to = File.join(to_path, "#{name}.mmdb")
|
|
||||||
FileUtils.cp(from, to, preserve: true)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
task "assets:precompile" => "assets:precompile:before" do
|
|
||||||
refresh_days = GlobalSetting.refresh_maxmind_db_during_precompile_days
|
|
||||||
|
|
||||||
if refresh_days.to_i > 0
|
|
||||||
mmdb_time = get_mmdb_time(DiscourseIpInfo.path)
|
|
||||||
|
|
||||||
backup_mmdb_time =
|
|
||||||
if GlobalSetting.maxmind_backup_path.present?
|
|
||||||
get_mmdb_time(GlobalSetting.maxmind_backup_path)
|
|
||||||
end
|
|
||||||
|
|
||||||
mmdb_time ||= backup_mmdb_time
|
|
||||||
if backup_mmdb_time && backup_mmdb_time >= mmdb_time
|
|
||||||
copy_maxmind(GlobalSetting.maxmind_backup_path, DiscourseIpInfo.path)
|
|
||||||
mmdb_time = backup_mmdb_time
|
|
||||||
end
|
|
||||||
|
|
||||||
if !mmdb_time || mmdb_time < refresh_days.days.ago
|
|
||||||
puts "Downloading MaxMindDB..."
|
|
||||||
mmdb_thread =
|
|
||||||
Thread.new do
|
|
||||||
name = "unknown"
|
|
||||||
begin
|
|
||||||
geolite_dbs.each do |db|
|
|
||||||
name = db
|
|
||||||
DiscourseIpInfo.mmdb_download(db)
|
|
||||||
end
|
|
||||||
|
|
||||||
if GlobalSetting.maxmind_backup_path.present?
|
|
||||||
copy_maxmind(DiscourseIpInfo.path, GlobalSetting.maxmind_backup_path)
|
|
||||||
end
|
|
||||||
rescue OpenURI::HTTPError => e
|
|
||||||
STDERR.puts("*" * 100)
|
|
||||||
STDERR.puts("MaxMindDB (#{name}) could not be downloaded: #{e}")
|
|
||||||
STDERR.puts("*" * 100)
|
|
||||||
Rails.logger.warn("MaxMindDB (#{name}) could not be downloaded: #{e}")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if $bypass_sprockets_uglify
|
if $bypass_sprockets_uglify
|
||||||
puts "Compressing Javascript and Generating Source Maps"
|
puts "Compressing Javascript and Generating Source Maps"
|
||||||
manifest = Sprockets::Manifest.new(assets_path)
|
manifest = Sprockets::Manifest.new(assets_path)
|
||||||
@ -356,8 +286,6 @@ task "assets:precompile" => "assets:precompile:before" do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
mmdb_thread.join if mmdb_thread
|
|
||||||
end
|
end
|
||||||
|
|
||||||
Rake::Task["assets:precompile"].enhance do
|
Rake::Task["assets:precompile"].enhance do
|
||||||
|
@ -1,10 +1,82 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
desc "downloads MaxMind's GeoLite2-City database"
|
GEOLITE_DBS = %w[GeoLite2-City GeoLite2-ASN]
|
||||||
task "maxminddb:get" => "environment" do
|
|
||||||
puts "Downloading MaxMindDb's GeoLite2-City..."
|
|
||||||
DiscourseIpInfo.mmdb_download("GeoLite2-City")
|
|
||||||
|
|
||||||
puts "Downloading MaxMindDb's GeoLite2-ASN..."
|
desc "downloads MaxMind's GeoLite2-City databases"
|
||||||
DiscourseIpInfo.mmdb_download("GeoLite2-ASN")
|
task "maxminddb:get" => "environment" do
|
||||||
|
GEOLITE_DBS.each do |name|
|
||||||
|
puts "Downloading MaxMindDb's #{name}..."
|
||||||
|
DiscourseIpInfo.mmdb_download(name)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def get_mmdb_time(root_path)
|
||||||
|
mmdb_time = nil
|
||||||
|
|
||||||
|
GEOLITE_DBS.each do |name|
|
||||||
|
path = File.join(root_path, "#{name}.mmdb")
|
||||||
|
if File.exist?(path)
|
||||||
|
mmdb_time = File.mtime(path)
|
||||||
|
else
|
||||||
|
mmdb_time = nil
|
||||||
|
break
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
mmdb_time
|
||||||
|
end
|
||||||
|
|
||||||
|
def copy_maxmind(from_path, to_path)
|
||||||
|
puts "Copying MaxMindDB from #{from_path} to #{to_path}"
|
||||||
|
|
||||||
|
GEOLITE_DBS.each do |name|
|
||||||
|
from = File.join(from_path, "#{name}.mmdb")
|
||||||
|
to = File.join(to_path, "#{name}.mmdb")
|
||||||
|
FileUtils.cp(from, to, preserve: true)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
maxmind_thread = nil
|
||||||
|
|
||||||
|
task "maxminddb:refresh" do
|
||||||
|
refresh_days = GlobalSetting.refresh_maxmind_db_during_precompile_days
|
||||||
|
return if refresh_days.to_i <= 0
|
||||||
|
|
||||||
|
mmdb_time = get_mmdb_time(DiscourseIpInfo.path)
|
||||||
|
|
||||||
|
if GlobalSetting.maxmind_backup_path.present?
|
||||||
|
backup_mmdb_time = get_mmdb_time(GlobalSetting.maxmind_backup_path)
|
||||||
|
mmdb_time ||= backup_mmdb_time
|
||||||
|
end
|
||||||
|
|
||||||
|
if backup_mmdb_time && backup_mmdb_time >= mmdb_time
|
||||||
|
copy_maxmind(GlobalSetting.maxmind_backup_path, DiscourseIpInfo.path)
|
||||||
|
mmdb_time = backup_mmdb_time
|
||||||
|
end
|
||||||
|
|
||||||
|
return if mmdb_time && mmdb_time >= refresh_days.days.ago
|
||||||
|
|
||||||
|
puts "Downloading MaxMindDB..."
|
||||||
|
|
||||||
|
maxmind_thread =
|
||||||
|
Thread.new do
|
||||||
|
name = "unknown"
|
||||||
|
begin
|
||||||
|
GEOLITE_DBS.each do |db|
|
||||||
|
name = db
|
||||||
|
DiscourseIpInfo.mmdb_download(db)
|
||||||
|
end
|
||||||
|
|
||||||
|
if GlobalSetting.maxmind_backup_path.present?
|
||||||
|
copy_maxmind(DiscourseIpInfo.path, GlobalSetting.maxmind_backup_path)
|
||||||
|
end
|
||||||
|
rescue OpenURI::HTTPError => e
|
||||||
|
STDERR.puts("*" * 100)
|
||||||
|
STDERR.puts("MaxMindDB (#{name}) could not be downloaded: #{e}")
|
||||||
|
STDERR.puts("*" * 100)
|
||||||
|
Rails.logger.warn("MaxMindDB (#{name}) could not be downloaded: #{e}")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
at_exit { maxmind_thread.join }
|
||||||
end
|
end
|
||||||
|
Reference in New Issue
Block a user