Module: Athar::Retention

Defined in:
lib/athar/retention.rb

Defined Under Namespace

Classes: Result

Class Method Summary collapse

Class Method Details

.prune!(max_age: nil, max_count: nil, batch_size: nil, max_batches: nil, prune_table_events: nil) ⇒ Object

rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity



12
13
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/athar/retention.rb', line 12

def prune!(max_age: nil, max_count: nil, batch_size: nil, max_batches: nil, prune_table_events: nil) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity
  config = Athar.configuration.retention
  max_age ||= config.max_age
  max_count ||= config.max_count
  batch_size ||= config.batch_size
  max_batches ||= config.max_batches_per_run
  prune_table_events = config.prune_table_events if prune_table_events.nil?

  result = Result.new(deleted_by_age: 0, deleted_by_count: 0, table_events_deleted: 0, batches: 0)

  if max_age
    cutoff = Time.current - max_age
    result.deleted_by_age, age_batches = prune_by_age(
      :athar_deletions,
      "deleted_at",
      cutoff,
      batch_size,
      max_batches
    )
    result.batches += age_batches

    if prune_table_events
      result.table_events_deleted, table_event_batches = prune_by_age(
        :athar_table_events,
        "occurred_at",
        cutoff,
        batch_size,
        [max_batches - result.batches, 0].max
      )
      result.batches += table_event_batches
    end
  end

  if max_count && result.batches < max_batches
    result.deleted_by_count, count_batches = prune_by_count(
      :athar_deletions,
      max_count,
      batch_size,
      max_batches - result.batches
    )
    result.batches += count_batches
  end

  result
end