class Zold::Routines::Gc

Gargage collecting. It goes through the list of all wallets and removes those that are older than 10 days and don't have any transactions inside.

Author

Yegor Bugayenko (yegor256@gmail.com)

Copyright

Copyright © 2018 Yegor Bugayenko

License

MIT

Public Class Methods

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

Public Instance Methods

exec(_ = 0) click to toggle source
# File lib/zold/commands/routines/gc.rb, line 39
def exec(_ = 0)
  sleep(60) unless @opts['routine-immediately']
  cmd = Zold::Remove.new(wallets: @wallets, log: @log)
  args = ['remove']
  seen = 0
  removed = 0
  @wallets.all.each do |id|
    seen += 1
    next unless @wallets.acq(id) { |w| w.exists? && w.mtime < Time.now - @opts['gc-age'] && w.txns.empty? }
    cmd.run(args + [id.to_s])
    removed += 1
  end
  @log.info("Removed #{removed} empty+old wallets out of #{seen} total") unless removed.zero?
end