From 6a5ee0693d6b150b50fedf8ccbffa5024d87ae48 Mon Sep 17 00:00:00 2001 From: Jay Pfaffman Date: Thu, 27 Jun 2024 14:10:14 -0700 Subject: [PATCH] FIX: rescue mmdb download failure (#27556) * rescue mmdb download failure If mmdb database fails to download a bootstrap fails. This is a trivial fix for that problem. A more elegant solution might check whether the dataabase was downloaded and provide a helpful error message. * linting --------- Co-authored-by: Joffrey JAFFEUX --- lib/discourse_ip_info.rb | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/lib/discourse_ip_info.rb b/lib/discourse_ip_info.rb index 23dc4569640..e7e28fb0d8a 100644 --- a/lib/discourse_ip_info.rb +++ b/lib/discourse_ip_info.rb @@ -55,27 +55,31 @@ class DiscourseIpInfo end end - gz_file = - FileHelper.download( - url, - max_file_size: 100.megabytes, - tmp_file_name: "#{name}.gz", - validate_uri: false, - follow_redirect: true, - extra_headers:, - ) + begin + gz_file = + FileHelper.download( + url, + max_file_size: 100.megabytes, + tmp_file_name: "#{name}.gz", + validate_uri: false, + follow_redirect: true, + extra_headers:, + ) - filename = File.basename(gz_file.path) + filename = File.basename(gz_file.path) - dir = "#{Dir.tmpdir}/#{SecureRandom.hex}" + dir = "#{Dir.tmpdir}/#{SecureRandom.hex}" - Discourse::Utils.execute_command("mkdir", "-p", dir) + Discourse::Utils.execute_command("mkdir", "-p", dir) - Discourse::Utils.execute_command("cp", gz_file.path, "#{dir}/#{filename}") + Discourse::Utils.execute_command("cp", gz_file.path, "#{dir}/#{filename}") - Discourse::Utils.execute_command("tar", "-xzvf", "#{dir}/#{filename}", chdir: dir) + Discourse::Utils.execute_command("tar", "-xzvf", "#{dir}/#{filename}", chdir: dir) - Dir["#{dir}/**/*.mmdb"].each { |f| FileUtils.mv(f, mmdb_path(name)) } + Dir["#{dir}/**/*.mmdb"].each { |f| FileUtils.mv(f, mmdb_path(name)) } + rescue => e + Discourse.warn_exception(e, message: "MaxMind database download failed.") + end ensure FileUtils.rm_r(dir, force: true) if dir gz_file&.close!