lib/zold/commands/fetch.rb in zold-0.14.26 vs lib/zold/commands/fetch.rb in zold-0.14.27

- old
+ new

@@ -24,12 +24,14 @@ require 'json' require 'time' require 'tempfile' require 'slop' require 'rainbow' +require 'concurrent/atomics' require_relative 'args' require_relative '../log' +require_relative '../age' require_relative '../http' require_relative '../score' require_relative '../json_page' require_relative '../copies' @@ -73,22 +75,28 @@ end private def fetch(id, cps, opts) - total = 0 - nodes = 0 - done = 0 + total = Concurrent::AtomicFixnum.new + nodes = Concurrent::AtomicFixnum.new + done = Concurrent::AtomicFixnum.new @remotes.iterate(@log) do |r| - nodes += 1 - total += fetch_one(id, r, cps, opts) - done += 1 + nodes.increment + total.increment(fetch_one(id, r, cps, opts)) + done.increment end - raise "There are no remote nodes, run 'zold remote reset'" if nodes.zero? - raise "No nodes out of #{nodes} have the wallet #{id}" if done.zero? && !opts['quiet-if-absent'] - @log.info("#{done} copies of #{id} fetched for the total score of #{total} from #{nodes} nodes") - @log.debug("#{cps.all.count} local copies:\n #{cps.all.map { |c| "#{c[:name]}: #{c[:score]}" }.join("\n ")}") + raise "There are no remote nodes, run 'zold remote reset'" if nodes.value.zero? + raise "No nodes out of #{nodes.value} have the wallet #{id}" if done.value.zero? && !opts['quiet-if-absent'] + @log.info("#{done.value} copies of #{id} fetched with the total score of \ +#{total.value} from #{nodes.value} nodes") + @log.debug("#{cps.all.count} local copies:") + cps.all.each do |c| + wallet = Wallet.new(c[:path]) + @log.debug(" #{c[:name]}: #{c[:score]} #{wallet.balance}/#{wallet.txns.count}t/\ +#{wallet.digest[0, 6]}/#{File.size(c[:path])}b/#{Age.new(File.mtime(c[:path]))}") + end end def fetch_one(id, r, cps, opts) start = Time.now if opts['ignore-node'].include?(r.to_s) @@ -116,31 +124,19 @@ end if wallet.balance.negative? && !wallet.root? raise "The balance of #{id} is #{wallet.balance} and it's not a root wallet" end copy = cps.add(File.read(f), score.host, score.port, score.value) - @log.info("#{r} returned #{body.length}b/#{wallet.balance}/#{wallet.txns.count}t/#{digest(json)}/#{age(json)} \ + @log.info("#{r} returned #{body.length}b/#{wallet.balance}/#{wallet.txns.count}t/\ +#{digest(json)}/#{Age.new(json['mtime'])} \ as copy #{copy} of #{id} in #{(Time.now - start).round(2)}s: #{Rainbow(score.value).green} (#{json['version']})") end score.value end def digest(json) hash = json['digest'] return '?' if hash.nil? hash[0, 6] - end - - def age(json) - mtime = json['mtime'] - return '?' if mtime.nil? - sec = Time.now - Time.parse(mtime) - if sec < 60 - "#{sec.round(2)}s" - elsif sec < 60 * 60 - "#{(sec / 60).round}m" - else - "#{(sec / 3600).round}h" - end end end end