From 1a6980137952eef58475f24a5195ec2fa8fe6596 Mon Sep 17 00:00:00 2001 From: Suze Huldt Date: Tue, 3 Oct 2023 16:50:19 +0100 Subject: [PATCH] Change error handling in CmdRunner to handle stderr errors The training_db_sync script does not break when errors are thrown while running commands with Ruby module Open3. This edit changes the error handling in the script to check for errors present in stderr. This allows us to capture any such errors, and raise an exception if any are present. If errors are present in stderr while capturing data from source; copying data; or loading data to the destination, the script will delete sessions and tempfiles before exiting. --- script/training_db_sync.rb | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/script/training_db_sync.rb b/script/training_db_sync.rb index 38e5c6a20..b4e90b9d6 100644 --- a/script/training_db_sync.rb +++ b/script/training_db_sync.rb @@ -63,14 +63,26 @@ def capture_data_from_source --no-owner CMD Kernel.puts "Running pg_dump on #{source} ==> #{cmd}" - CmdRunner.run(cmd) + begin + CmdRunner.run(cmd) + rescue => e + clear_sessions + remove_temp_files + raise e + end end def copy_data_to_destination authenticate_to(destination) cmd = %(cat #{data_filename} | cf ssh beis-roda-#{destination} -c "cat > #{data_filename}") Kernel.puts "Copying data to #{destination} ==> #{cmd}" - CmdRunner.run(cmd) + begin + CmdRunner.run(cmd) + rescue => e + clear_sessions + remove_temp_files + raise e + end end def load_source_data_to_destination @@ -80,7 +92,13 @@ def load_source_data_to_destination psql < #{data_filename} CMD Kernel.puts "Loading data from #{source} to #{destination} ==> #{cmd}" - CmdRunner.run(cmd) + begin + CmdRunner.run(cmd) + rescue => e + clear_sessions + remove_temp_files + raise e + end end def force_password_reset_for_users @@ -138,13 +156,11 @@ class CmdRunner require "open3" def self.run(command) - begin - stdout, _stderr, _status = Open3.capture3(command) - rescue => error - raise "'#{command}' failed (#{error})" - end + _stdout, stderr, _status = Open3.capture3(command) - Kernel.puts stdout.chomp + if stderr + raise StandardError.new("Command: '#{command}' FAILED with error: '#{stderr}'") + end end end end