lib/zold/commands/remote.rb in zold-0.17.7 vs lib/zold/commands/remote.rb in zold-0.17.8

- old
+ new

@@ -100,10 +100,13 @@ 'The maximum amount of election winners the election (default: 1)', default: 1 o.bool '--skip-ping', 'Don\'t ping back the node when adding it (not recommended)', default: false + o.bool '--ignore-ping', + 'Don\'t fail if ping fails, just report the problem in the log', + default: false o.integer '--depth', 'The amount of update cycles to run, in order to fetch as many nodes as possible (default: 2)', default: 2 o.string '--network', "The name of the network we work in (default: #{Wallet::MAINET}", @@ -189,16 +192,17 @@ end if opts['ignore-if-exists'] && @remotes.exists?(host, port) @log.debug("#{host}:#{port} already exists, won't add because of --ignore-if-exists") return end - unless opts['skip-ping'] - res = Http.new(uri: "http://#{host}:#{port}/version", network: opts['network']).get - raise "The node #{host}:#{port} is not responding, #{res.status}:#{res.status_line}" unless res.status == 200 + return unless ping(host, port, opts) + if @remotes.exists?(host, port) + @log.info("#{host}:#{port} already exists among #{@remotes.all.count} others") + else + @remotes.add(host, port) + @log.info("#{host}:#{port} added to the list, #{@remotes.all.count} total") end - @remotes.add(host, port) - @log.info("#{host}:#{port} added to the list, #{@remotes.all.count} total") end def remove(host, port, _) @remotes.remove(host, port) @log.info("#{host}:#{port} removed from the list, #{@remotes.all.count} total") @@ -256,26 +260,20 @@ gem = Zold::Gem.new if Semantic::Version.new(VERSION) < Semantic::Version.new(json['version']) || Semantic::Version.new(VERSION) < Semantic::Version.new(gem.last_version) if opts['reboot'] @log.info("#{r}: their version #{json['version']} is higher than mine #{VERSION}, reboot! \ - (use --never-reboot to avoid this from happening)") +(use --never-reboot to avoid this from happening)") terminate end @log.debug("#{r}: their version #{json['version']} is higher than mine #{VERSION}, \ - it's recommended to reboot, but I don't do it because of --never-reboot") +it's recommended to reboot, but I don't do it because of --never-reboot") end if cycle.positive? json['all'].each do |s| - if opts['ignore-node'].include?("#{s['host']}:#{s['port']}") - @log.debug("#{s['host']}:#{s['port']}, which is found at #{r} \ - won't be added since it's in the --ignore-node list") - next - end next if @remotes.exists?(s['host'], s['port']) - @remotes.add(s['host'], s['port']) - @log.info("#{s['host']}:#{s['port']} found at #{r} and added to the list of #{@remotes.all.count}") + add(s['host'], s['port'], opts) end end capacity << { host: score.host, port: score.port, count: json['all'].count } @log.info("#{r}: the score is #{Rainbow(score.value).green} (#{json['version']}) in #{Age.new(start)}") end @@ -301,8 +299,17 @@ def terminate @log.info("All threads before exit: #{Thread.list.map { |t| "#{t.name}/#{t.status}" }.join(', ')}") require_relative '../node/front' Front.stop! + end + + def ping(host, port, opts) + return true if opts['skip-ping'] + res = Http.new(uri: "http://#{host}:#{port}/version", network: opts['network']).get + return true if res.status == 200 + raise "The node #{host}:#{port} is not responding, #{res.status}:#{res.status_line}" unless opts['ignore-ping'] + @log.error("The node #{host}:#{port} is not responding, #{res.status}:#{res.status_line}") + false end end end