Class: SolidObjects::MessagePruner

Inherits:
Object
  • Object
show all
Defined in:
lib/solid_objects/message_pruner.rb,
sig/generated/lib/solid_objects/message_pruner.rbs

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(now: SolidObjects.database_adapter.database_now, batch_size: SolidObjects.configuration.prune_batch_size) ⇒ MessagePruner

Returns a new instance of MessagePruner.

RBS:

  • (?now: Time, ?batch_size: Integer) -> void

Parameters:

  • now: (Time) (defaults to: SolidObjects.database_adapter.database_now)
  • batch_size: (Integer) (defaults to: SolidObjects.configuration.prune_batch_size)


9
10
11
12
13
14
15
# File 'lib/solid_objects/message_pruner.rb', line 9

def initialize(
  now: SolidObjects.database_adapter.database_now,
  batch_size: SolidObjects.configuration.prune_batch_size
)
  @now = now
  @batch_size = batch_size
end

Instance Attribute Details

#batch_sizeObject (readonly)

Returns the value of attribute batch_size.

Returns:

  • (Object)


31
32
33
# File 'lib/solid_objects/message_pruner.rb', line 31

def batch_size
  @batch_size
end

#nowObject (readonly)

Returns the value of attribute now.

Returns:

  • (Object)


31
32
33
# File 'lib/solid_objects/message_pruner.rb', line 31

def now
  @now
end

Instance Method Details

#default_retentionNumeric

RBS:

  • () -> Numeric

Returns:

  • (Numeric)


82
83
84
# File 'lib/solid_objects/message_pruner.rb', line 82

def default_retention
  SolidObjects.configuration.message_retention
end

#policy_relationsArray[ActiveRecord::Relation[Message]]

RBS:

  • () -> Array[ActiveRecord::Relation[Message]]

Returns:



34
35
36
37
38
39
40
41
42
# File 'lib/solid_objects/message_pruner.rb', line 34

def policy_relations
  overrides = retention_overrides
  relations = overrides.map do |actor_type, retention|
    prunable.where(actor_type:, completed_at: ...retention_cutoff(retention))
  end
  default_relation = prunable.where(completed_at: ...retention_cutoff(default_retention))
  default_relation = default_relation.where.not(actor_type: overrides.keys) if overrides.any?
  relations << default_relation
end

#previewInteger

RBS:

  • () -> Integer

Returns:

  • (Integer)


18
19
20
# File 'lib/solid_objects/message_pruner.rb', line 18

def preview
  policy_relations.sum(&:count)
end

#prunableActiveRecord::Relation[Message]

RBS:

  • () -> ActiveRecord::Relation[Message]

Returns:



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/solid_objects/message_pruner.rb', line 45

def prunable
  Message
    .where.not(id: ReadyMessage.select(:message_id))
    .where.not(id: ClaimedMessage.select(:message_id))
    .where.not(id: DeadLetter.select(:message_id))
    .where.not(
      id: DeadLetter
        .where.not(retried_message_id: nil)
        .select(:retried_message_id)
    )
    .where.not(
      id: Effect
        .where.not(status: "completed")
        .select(:message_id)
    )
    .where.not(
      id: Broadcast
        .where.not(status: "delivered")
        .select(:message_id)
    )
end

#pruneInteger

RBS:

  • () -> Integer

Returns:

  • (Integer)


23
24
25
26
27
# File 'lib/solid_objects/message_pruner.rb', line 23

def prune
  policy_relations.sum { |relation| prune_relation(relation) }.tap do |count|
    SolidObjects.instrument(:"messages.pruned", count:)
  end
end

#prune_relation(relation) ⇒ Integer

RBS:

  • (ActiveRecord::Relation[Message]) -> Integer

Parameters:

Returns:

  • (Integer)


68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/solid_objects/message_pruner.rb', line 68

def prune_relation(relation)
  deleted = 0

  loop do
    message_ids = relation.limit(batch_size).pluck(:id)
    break if message_ids.empty?

    deleted += Message.where(id: message_ids).delete_all
  end

  deleted
end

#retention_cutoff(retention) ⇒ Time

RBS:

  • (Numeric) -> Time

Parameters:

  • (Numeric)

Returns:

  • (Time)


93
94
95
# File 'lib/solid_objects/message_pruner.rb', line 93

def retention_cutoff(retention)
  now - retention
end

#retention_overridesHash[String, Numeric]

RBS:

  • () -> Hash[String, Numeric]

Returns:

  • (Hash[String, Numeric])


87
88
89
90
# File 'lib/solid_objects/message_pruner.rb', line 87

def retention_overrides
  SolidObjects.configuration.message_retention_by_actor_type
    .to_h { |actor_type, retention| [ actor_type.to_s, retention ] }
end