Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better retry when copying files #21

Merged
merged 1 commit into from
Aug 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 35 additions & 31 deletions bin/SyncDBData.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,6 @@ def parse_commandline
require 'SFTPUploader.rb' if $source == :sftp || $target == :sftp

connect

$source_path.chomp!('/')
$target_path.chomp!('/')
end

def connect
Expand Down Expand Up @@ -232,6 +229,9 @@ def connect
else
$logger.error "No target found! Specify either --to-ftp, --to-sftp or --to-local."
end

$source_path.chomp!('/')
$target_path.chomp!('/')
end

# ---------
Expand Down Expand Up @@ -312,43 +312,47 @@ def copy_file(copy_from, fs_from, copy_to, fs_to)
def sync
$datasources.each { |datasource|
[false, true].each{ |is_infofile|

datasource = datasource.chomp(File.extname(datasource)) + '.info' if is_infofile

copy_from = datasource.gsub('{data_folder}', $source_path)
if file_exists(copy_from, $src_fs) then
print ' ' + copy_from + ' ... '

copy_to = datasource.gsub('{data_folder}', $target_path)
make_file_dir(copy_to, $tgt_fs)

skip = false
if file_exists(copy_to, $tgt_fs) then
if file_size(copy_from, $src_fs) == file_size(copy_to, $tgt_fs) then
if compare_file_time(copy_from, $src_fs, copy_to, $tgt_fs) then
skip = true
counter = 0
max_attempts = 5
begin
counter += 1
if file_exists(copy_from, $src_fs) then
print ' ' + copy_from + ' ... '

copy_to = datasource.gsub('{data_folder}', $target_path)
make_file_dir(copy_to, $tgt_fs)

skip = false
if file_exists(copy_to, $tgt_fs) then
if file_size(copy_from, $src_fs) == file_size(copy_to, $tgt_fs) then
if compare_file_time(copy_from, $src_fs, copy_to, $tgt_fs) then
skip = true
end
end
end
end

if skip then
puts 'OK.'
else
counter = 0
max_attempts = 5
begin
counter += 1

if skip then
puts 'OK.'
else
copy_file(copy_from, $src_fs, copy_to, $tgt_fs)
rescue => e
$logger.writeln "copy file failed, attempt #{counter}, message #{e.message}`"
(connect; retry;) unless counter > max_attempts
end
else
if $continue then
$logger.writeln "File not found: #{copy_from}" unless is_infofile
else
$logger.error "File not found: #{copy_from}" unless is_infofile
end
end
else
if $continue then
$logger.writeln "File not found: #{copy_from}" unless is_infofile
rescue => e
if counter <= max_attempts then
$logger.writeln "copy file failed, attempt #{counter}, message #{e.message}"
connect
retry
else
$logger.error "File not found: #{copy_from}" unless is_infofile
raise e
end
end
}
Expand Down