Module: Legion::Data::Archival

Extended by:
Logging::Helper
Defined in:
lib/legion/data/archival.rb,
lib/legion/data/archival/policy.rb

Defined Under Namespace

Classes: Policy

Constant Summary collapse

ARCHIVE_TABLE_MAP =
{
  tasks:            :tasks_archive,
  metering_records: :metering_records_archive
}.freeze

Class Method Summary collapse

Methods included from Logging::Helper

handle_exception

Class Method Details

.archive!(policy: Policy.new, dry_run: false) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/legion/data/archival.rb', line 17

def archive!(policy: Policy.new, dry_run: false)
  log.info "Archival run started dry_run=#{dry_run} tables=#{policy.tables.size}"
  results = {}
  policy.tables.each do |table_name|
    table = table_name.to_sym
    archive_table = ARCHIVE_TABLE_MAP[table]
    next unless archive_table && db_ready?(table) && db_ready?(archive_table)

    log.info "Archiving #{table} -> #{archive_table} (cutoff: #{policy.warm_cutoff}, dry_run: #{dry_run})"
    count = archive_table!(
      source: table, destination: archive_table,
      cutoff: policy.warm_cutoff, batch_size: policy.batch_size, dry_run: dry_run
    )
    results[table] = count
  end
  log.info "Archival run completed tables=#{results.keys.join(',')}" unless results.empty?
  results
rescue StandardError => e
  handle_exception(e, level: :error, handled: false, operation: :archive!, dry_run: dry_run)
  raise
end

.archive_completed_tasks(days_old: 90, batch_size: 1000) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/legion/data/archival.rb', line 79

def archive_completed_tasks(days_old: 90, batch_size: 1000)
  conn = Legion::Data.connection
  cutoff = Time.now - (days_old * 86_400)

  return { archived: 0, cutoff: cutoff.iso8601 } unless conn&.table_exists?(:tasks) && conn.table_exists?(:tasks_archive)

  candidates = conn[:tasks]
               .where(status: %w[completed failed])
               .where(Sequel.lit('created < ?', cutoff))
               .limit(batch_size)

  count = candidates.count
  if count.positive?
    archive_cols = conn.schema(:tasks_archive).to_set(&:first)
    conn.transaction do
      candidates.each do |row|
        archive_row = {
          original_id:         row[:id],
          status:              row[:status],
          relationship_id:     row[:relationship_id],
          original_created_at: row[:created],
          original_updated_at: row[:updated],
          archived_at:         Time.now
        }
        archive_row[:archive_reason] = 'completed_task_archival' if archive_cols.include?(:archive_reason)
        conn[:tasks_archive].insert(archive_row)
      end
      conn[:tasks].where(id: candidates.select(:id)).delete
    end
  end

  log.info "archive_completed_tasks: archived #{count} tasks (cutoff: #{cutoff.iso8601})"
  { archived: count, cutoff: cutoff.iso8601 }
rescue StandardError => e
  handle_exception(e, level: :error, handled: false, operation: :archive_completed_tasks, days_old: days_old, batch_size: batch_size)
  raise
end

.restore(table:, ids:) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/legion/data/archival.rb', line 39

def restore(table:, ids:)
  source_table = table.to_sym
  archive_table = ARCHIVE_TABLE_MAP[source_table]
  return 0 unless archive_table && db_ready?(archive_table)

  conn = Legion::Data.connection
  restored = 0
  conn.transaction do
    conn[archive_table].where(original_id: ids).each do |row|
      restore_row = row.except(:id, :archived_at, :original_id, :original_created_at, :original_updated_at)
      restore_row[:id] = row[:original_id]
      restore_row[:created_at] = row[:original_created_at]
      restore_row[:updated_at] = row[:original_updated_at]
      conn[source_table].insert(restore_row)
      restored += 1
    end
    conn[archive_table].where(original_id: ids).delete
  end
  log.info "Restored #{restored} row(s) from #{archive_table} -> #{source_table}"
  restored
rescue StandardError => e
  handle_exception(e, level: :error, handled: false, operation: :restore, table: source_table, ids: Array(ids))
  raise
end

.run_scheduled_archivalObject



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/legion/data/archival.rb', line 117

def run_scheduled_archival
  log.info 'Running scheduled archival'
  results = {}
  results[:tasks] = archive_completed_tasks

  conn = Legion::Data.connection
  if conn&.table_exists?(:metering_records)
    results[:metering] = Legion::Data::Retention.archive_old_records(
      table: :metering_records, date_column: :recorded_at
    )
  end

  log.info "Scheduled archival completed keys=#{results.keys.join(',')}"
  results
rescue StandardError => e
  handle_exception(e, level: :error, handled: false, operation: :run_scheduled_archival)
  raise
end

.search(table:, where: {}) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/legion/data/archival.rb', line 64

def search(table:, where: {})
  source_table = table.to_sym
  archive_table = ARCHIVE_TABLE_MAP[source_table]
  return [] unless db_ready?(source_table)

  log.info "Archival search table=#{source_table} where_keys=#{where.keys.join(',')}"
  conn = Legion::Data.connection
  hot = conn[source_table].where(where).all
  warm = db_ready?(archive_table) ? conn[archive_table].where(where).all : []
  hot + warm
rescue StandardError => e
  handle_exception(e, level: :error, handled: false, operation: :search, table: source_table, where_keys: where.keys)
  raise
end