Skip to content

Commit

Permalink
Merge pull request #2177 from UKGovernmentBEIS/fix/surface-training-s…
Browse files Browse the repository at this point in the history
…ync-errors

Change error handling in CmdRunner to handle stderr errors
  • Loading branch information
shuldt authored Oct 4, 2023
2 parents f9d3154 + 1a69801 commit 1154958
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 1154958

Please sign in to comment.