Class: Zold::Routines::Gc

Inherits:
Object
  • Object
show all
Defined in:
lib/zold/commands/routines/gc.rb

Overview

Garbage collector

Instance Method Summary collapse

Constructor Details

#initialize(opts, wallets, log: Log::NULL) ⇒ Gc

Returns a new instance of Gc.



36
37
38
39
40
# File 'lib/zold/commands/routines/gc.rb', line 36

def initialize(opts, wallets, log: Log::NULL)
  @opts = opts
  @wallets = wallets
  @log = log
end

Instance Method Details

#exec(_ = 0) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/zold/commands/routines/gc.rb', line 42

def exec(_ = 0)
  sleep(60) unless @opts['routine-immediately']
  cmd = 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