FIX: Do not change directory when decompressing S3 inventory

FIX: Do not change directory when decompressing S3 inventory

In sidekiq, jobs are run in multiple threads within the same process. cd affects the entire process, so can cause unexpected issues in other running jobs.

diff --git a/lib/discourse.rb b/lib/discourse.rb
index 027d5bf..2b53a91 100644
--- a/lib/discourse.rb
+++ b/lib/discourse.rb
@@ -23,8 +23,8 @@ module Discourse
   end
 
   class Utils
-    def self.execute_command(*command, failure_message: "", success_status_codes: [0])
-      stdout, stderr, status = Open3.capture3(*command)
+    def self.execute_command(*command, failure_message: "", success_status_codes: [0], chdir: ".")
+      stdout, stderr, status = Open3.capture3(*command, chdir: chdir)
 
       if !status.exited? || !success_status_codes.include?(status.exitstatus)
         failure_message = "#{failure_message}\n" if !failure_message.blank?
diff --git a/lib/s3_inventory.rb b/lib/s3_inventory.rb
index 1990176..cba0f9c 100644
--- a/lib/s3_inventory.rb
+++ b/lib/s3_inventory.rb
@@ -125,11 +125,9 @@ class S3Inventory
   end
 
   def decompress_inventory_files
-    FileUtils.cd(tmp_directory) do
-      files.each do |file|
-        log "Decompressing inventory file '#{file[:filename]}', this may take a while..."
-        Discourse::Utils.execute_command('gzip', '--decompress', file[:filename], failure_message: "Failed to decompress inventory file '#{file[:filename]}'.")
-      end
+    files.each do |file|
+      log "Decompressing inventory file '#{file[:filename]}', this may take a while..."
+      Discourse::Utils.execute_command('gzip', '--decompress', file[:filename], failure_message: "Failed to decompress inventory file '#{file[:filename]}'.", chdir: tmp_directory)
     end
   end

GitHub sha: ed21128e

1 Like