class Zold::Routines::Reconcile

R

Author

Yegor Bugayenko (yegor256@gmail.com)

Copyright

Copyright © 2018 Yegor Bugayenko

License

MIT

Public Class Methods

new(opts, wallets, remotes, copies, address, log: Log::NULL) click to toggle source
# File lib/zold/commands/routines/reconcile.rb, line 35
def initialize(opts, wallets, remotes, copies, address, log: Log::NULL)
  @opts = opts
  @wallets = wallets
  @remotes = remotes
  @copies = copies
  @address = address
  @log = log
end

Public Instance Methods

exec(_ = 0) click to toggle source
# File lib/zold/commands/routines/reconcile.rb, line 44
def exec(_ = 0)
  sleep(20 * 60) unless @opts['routine-immediately']
  @remotes.iterate(@log) do |r|
    next unless r.master?
    next if r.to_mnemo == @address
    res = r.http('/wallets').get
    r.assert_code(200, res)
    missing = res.body.strip.split("\n").compact
      .select { |i| /^[a-f0-9]{16}$/.match?(i) }
      .reject { |i| @wallets.acq(Zold::Id.new(i), &:exists?) }
    missing.each { |i| pull(i) }
    if missing.empty?
      log.info("Nothing to reconcile with #{r}, we are good at #{@address}")
    else
      @log.info("Reconcile routine pulled #{missing.count} wallets from #{r}")
    end
  end
end

Private Instance Methods

pull(id) click to toggle source
# File lib/zold/commands/routines/reconcile.rb, line 65
def pull(id)
  Zold::Pull.new(wallets: @wallets, remotes: @remotes, copies: @copies, log: @log).run(
    ['pull', "--network=#{Shellwords.escape(@opts['network'])}", id.to_s, '--quiet-if-absent']
  )
end