Class: TxCatcher::Cleaner

Inherits:
Object
  • Object
show all
Defined in:
lib/txcatcher/cleaner.rb

Overview

Cleans DB so that its size doesn’t go above Config.max_db_transactions_stored

Class Method Summary collapse

Class Method Details

.clean_deposits(transaction) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'lib/txcatcher/cleaner.rb', line 52

def clean_deposits(transaction)
  transaction.deposits.each do |d|
    if d.address && d.address.deposits.count == 1
      d.address.delete
      @@cleaning_counter[:addresses] += 1
    end
    d.delete
    @@cleaning_counter[:deposits] += 1
  end
end

.clean_transactions(n) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/txcatcher/cleaner.rb', line 44

def clean_transactions(n)
  transactions = Transaction.order("created_at ASC").limit(n).each do |t|
    clean_deposits(t)
    t.delete
    @@cleaning_counter[:transactions] += 1
  end
end

.start(run_once: false) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/txcatcher/cleaner.rb', line 14

def start(run_once: false)
  @@stopped = false
  @@stop    = false
  Thread.new do
    loop do
      @@cleaning_counter = { transactions: 0, deposits: 0, addresses: 0 }
      db_tx_count = TxCatcher::Transaction.last.id - TxCatcher::Transaction.first.id + 1
      $stdout.print "Cleaning transactions in DB..."
      $stdout.print "#{db_tx_count} now, needs to be below #{Config.max_db_transactions_stored}\n"
      if db_tx_count > Config.max_db_transactions_stored
        number_to_delete = (db_tx_count - Config.max_db_transactions_stored) + (Config.max_db_transactions_stored*0.1).round
        clean_transactions(number_to_delete)
        $stdout.puts "DB cleaned: #{@@cleaning_counter.to_s}"
      else
        $stdout.puts "Nothing to be cleaned from DB, size is below threshold."
      end
      if @stop || run_once
        @@stopped = true
        break
      end
      sleep Config.db_clean_period_seconds
    end # loop
  end # Thread
  @@stopped
end

.stopObject



40
41
42
# File 'lib/txcatcher/cleaner.rb', line 40

def stop
  @@stop = true
end

.stopped?Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/txcatcher/cleaner.rb', line 10

def stopped?
  @@stopped
end