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



69
70
71
72
73
74
75
76
77
78
# File 'lib/txcatcher/cleaner.rb', line 69

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

.clean_transactions(n) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/txcatcher/cleaner.rb', line 52

def clean_transactions(n)
  transactions = Transaction.order(Sequel.asc(:created_at))
  transactions.where(Sequel.~(protected: true)) if Config.protected_transactions

  t = n/100
  t = 1 if t == 0
  t.times do
    limit = n <= 100 ? n : 100
    transactions.limit(limit).each do |t|
      LOGGER.report "- Removing tx #{t.txid}"
      clean_deposits(t)
      TxCatcher.db_connection[:transactions].filter(id: t.id).delete
      @@cleaning_counter[:transactions] += 1
    end
  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
# File 'lib/txcatcher/cleaner.rb', line 14

def start(run_once: false)
  loop do
    @@cleaning_counter = { transactions: 0, deposits: 0, addresses: 0 }
    db_tx_count = if Config.protected_transactions
      TxCatcher::Transaction.where(Sequel.~(protected: true)).count
    else
      TxCatcher::Transaction.count
    end
    LOGGER.report "Cleaning transactions in DB..."
    LOGGER.report "#{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)
      LOGGER.report "DB cleaned: #{@@cleaning_counter.to_s}"
    else
      LOGGER.report "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
end

.start_in_thread(run_once: false) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/txcatcher/cleaner.rb', line 39

def start_in_thread(run_once: false)
  @@stopped = false
  @@stop    = false
  Thread.new do
    start(run_once: run_once)
  end
  @@stopped
end

.stopObject



48
49
50
# File 'lib/txcatcher/cleaner.rb', line 48

def stop
  @@stop = true
end

.stopped?Boolean

Returns:

  • (Boolean)


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

def stopped?
  @@stopped
end