Skip to content

Commit

Permalink
Enhance bg-sync progress output
Browse files Browse the repository at this point in the history
  • Loading branch information
onnimonni committed Jan 29, 2017
1 parent e3a38ce commit 0e3f613
Showing 1 changed file with 31 additions and 8 deletions.
39 changes: 31 additions & 8 deletions bin/gdev
Original file line number Diff line number Diff line change
Expand Up @@ -146,18 +146,34 @@ HEREDOC

settings = get_container_settings()
if is_osx?
puts "INFO: Waiting for host <-> container filesystems to be synced..."
puts "INFO: Waiting for initial host <-> container filesystem sync..."
print "Progress:"
last_text = ""
30.times do # 30 * 2 is good enough timeout for the process
host_data_amount = `gdev run #{settings['unison']['container_name']} du -sm #{settings['unison']['source']} | cut -f1`.strip.to_f
container_data_amount = `gdev run #{settings['unison']['container_name']} du -sm #{settings['unison']['destination']} | cut -f1`.strip.to_f
host_data_amount = `du -I '.git' -I '.docker' -sm #{settings['unison']['source']} | cut -f1`.strip
container_data_amount = `gdev run #{settings['unison']['container_name']} du -sm #{settings['unison']['destination']} | cut -f1`.strip

puts "#{container_data_amount}M / #{host_data_amount}M"
if host_data_amount - container_data_amount < 5
# Only show minimal progress bar if text didn't change from the last time
percent = ( container_data_amount.to_f / host_data_amount.to_f * 100 ).round(2)

# This is only approximation but it's close enough
# Usually the data generated by container makes this calculation go south
if host_data_amount.to_i - container_data_amount.to_i < 20 or container_data_amount.to_f / host_data_amount.to_f > 0.98
print "\n#{host_data_amount}M / #{host_data_amount}M - 100%\n"
sleep 1
puts "INFO: Container is synced!"
puts "INFO: Initial sync is ready!"
return true
end
sleep 2

new_text = "#{container_data_amount.rjust(host_data_amount.length, ' ')}M / #{host_data_amount}M - #{percent}% "
if last_text == new_text
print '.'
else
print "\n#{new_text}"
end

sleep 3
last_text = new_text
end
puts "ERROR: Sync process timeout..."
return false
Expand Down Expand Up @@ -267,11 +283,18 @@ HEREDOC
settings['unison']['port'] = port unless port.nil?

elsif data['image'].include? 'devgeniem/bg-sync'
# Take the absolute path for the host machine volume source
volume = data['volumes'].select { |volume| volume.include?(':/source') }.first
if volume
source = volume.split(':').first
else
source = nil
end
settings = { 'unison' => {
'container_name' => container_name,
'image' => 'devgeniem/bg-sync',
'destination' => data['environment']['SYNC_DESTINATION'],
'source' => '/source' # This is fixed in bg-sync
'source' => source # This is the volume path in host machine
} }

end
Expand Down

0 comments on commit 0e3f613

Please sign in to comment.