X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=bin%2Fsup-sync;h=5e89dac9cd8c479555a5f0b486a350a90bd438fd;hb=82a14a27c9cf43ce7137e583330c412d90a1eb9a;hp=2aa00c3720bcd0dbbce60499e5d14a70e01ae418;hpb=1d3816e0e62f1732ea5511d11ee20726631957d5;p=sup diff --git a/bin/sup-sync b/bin/sup-sync index 2aa00c3..5e89dac 100755 --- a/bin/sup-sync +++ b/bin/sup-sync @@ -99,13 +99,13 @@ index = Redwood::Index.init restored_state = if opts[:restore] dump = {} - $stderr.puts "Loading state dump from #{opts[:restore]}..." + puts "Loading state dump from #{opts[:restore]}..." IO.foreach opts[:restore] do |l| l =~ /^(\S+) \((.*?)\)$/ or raise "Can't read dump line: #{l.inspect}" mid, labels = $1, $2 dump[mid] = labels.to_set_of_symbols end - $stderr.puts "Read #{dump.size} entries from dump file." + puts "Read #{dump.size} entries from dump file." dump else {} @@ -139,7 +139,7 @@ begin end sources.each do |source| - $stderr.puts "Scanning #{source}..." + puts "Scanning #{source}..." num_added = num_updated = num_scanned = num_restored = 0 last_info_time = start_time = Time.now @@ -173,13 +173,22 @@ begin ## decide what to do based on message labels and the operation we're performing dothis, new_labels = case - when (op == :restore) && restored_state[m.id] && old_m && (old_m.labels != restored_state[m.id]) - [:update_message_state, restored_state[m.id]] + when (op == :restore) && restored_state[m.id] + if old_m && (old_m.labels != restored_state[m.id]) + num_restored += 1 + [:update_message_state, restored_state[m.id]] + elsif old_m.nil? + num_restored += 1 + m.labels = restored_state[m.id] + :add_message + else + # labels are the same; don't do anything + end when op == :discard if old_m && (old_m.labels != m.labels) [:update_message_state, m.labels] else - # don't do anything + # labels are the same; don't do anything end else ## duplicate behavior of poll mode: if index_state is non-nil, this is a newer @@ -198,15 +207,15 @@ begin ## now, actually do the operation case dothis when :add_message - $stderr.puts "Adding new message #{source}###{m.source_info} with labels #{m.labels}" if opts[:verbose] + puts "Adding new message #{source}##{m.source_info} with labels #{m.labels}" if opts[:verbose] index.add_message m unless opts[:dry_run] num_added += 1 when :update_message - $stderr.puts "Updating message #{source}###{m.source_info}; labels #{old_m.labels} => #{m.labels}; offset #{old_m.source_info} => #{m.source_info}" if opts[:verbose] + puts "Updating message #{source}##{m.source_info}; labels #{old_m.labels} => #{m.labels}; offset #{old_m.source_info} => #{m.source_info}" if opts[:verbose] index.update_message m unless opts[:dry_run] num_updated += 1 when :update_message_state - $stderr.puts "Changing flags for #{source}##{m.source_info} from #{m.labels} to #{new_labels}" + puts "Changing flags for #{source}##{m.source_info} from #{m.labels} to #{new_labels}" if opts[:verbose] m.labels = new_labels index.update_message_state m unless opts[:dry_run] num_updated += 1 @@ -217,18 +226,18 @@ begin elapsed = last_info_time - start_time pctdone = source.respond_to?(:pct_done) ? source.pct_done : 100.0 * (source.cur_offset.to_f - source.start_offset).to_f / (source.end_offset - source.start_offset).to_f remaining = (100.0 - pctdone) * (elapsed.to_f / pctdone) - $stderr.printf "## read %dm (about %.0f%%) @ %.1fm/s. %s elapsed, about %s remaining\n", num_scanned, pctdone, num_scanned / elapsed, elapsed.to_time_s, remaining.to_time_s + printf "## read %dm (about %.0f%%) @ %.1fm/s. %s elapsed, about %s remaining\n", num_scanned, pctdone, num_scanned / elapsed, elapsed.to_time_s, remaining.to_time_s end end - $stderr.puts "Scanned #{num_scanned}, added #{num_added}, updated #{num_updated} messages from #{source}." - $stderr.puts "Restored state on #{num_restored} (#{100.0 * num_restored / num_scanned}%) messages." if num_restored > 0 + puts "Scanned #{num_scanned}, added #{num_added}, updated #{num_updated} messages from #{source}." + puts "Restored state on #{num_restored} (#{100.0 * num_restored / num_scanned}%) messages." if num_restored > 0 end ## delete any messages in the index that claim they're from one of ## these sources, but that we didn't see. if (target == :all || target == :changed) - $stderr.puts "Deleting missing messages from the index..." + puts "Deleting missing messages from the index..." num_del, num_scanned = 0, 0 sources.each do |source| raise "no source id for #{source}" unless source.id @@ -242,15 +251,15 @@ begin end end end - $stderr.puts "Deleted #{num_del} / #{num_scanned} messages" + puts "Deleted #{num_del} / #{num_scanned} messages" end index.save if opts[:optimize] - $stderr.puts "Optimizing index..." + puts "Optimizing index..." optt = time { index.optimize unless opts[:dry_run] } - $stderr.puts "Optimized index of size #{index.size} in #{optt}s." + puts "Optimized index of size #{index.size} in #{optt}s." end rescue Redwood::FatalSourceError => e $stderr.puts "Sorry, I couldn't communicate with a source: #{e.message}"