Skip to content

Commit

Permalink
Change error handling in CmdRunner to handle stderr errors
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
shuldt committed Oct 3, 2023
1 parent f9d3154 commit 1a69801
Showing 1 changed file with 25 additions and 9 deletions.
34 changes: 25 additions & 9 deletions script/training_db_sync.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 1a69801

Please sign in to comment.