Class: SolidObjects::MessagePruner
- Inherits:
-
Object
- Object
- SolidObjects::MessagePruner
- Defined in:
- lib/solid_objects/message_pruner.rb,
sig/generated/lib/solid_objects/message_pruner.rbs
Instance Attribute Summary collapse
-
#batch_size ⇒ Object
readonly
Returns the value of attribute batch_size.
-
#now ⇒ Object
readonly
Returns the value of attribute now.
Instance Method Summary collapse
- #default_retention ⇒ Numeric
-
#initialize(now: SolidObjects.database_adapter.database_now, batch_size: SolidObjects.configuration.prune_batch_size) ⇒ MessagePruner
constructor
A new instance of MessagePruner.
- #policy_relations ⇒ Array[ActiveRecord::Relation[Message]]
- #preview ⇒ Integer
- #prunable ⇒ ActiveRecord::Relation[Message]
- #prune ⇒ Integer
- #prune_relation(relation) ⇒ Integer
- #retention_cutoff(retention) ⇒ Time
- #retention_overrides ⇒ Hash[String, Numeric]
Constructor Details
#initialize(now: SolidObjects.database_adapter.database_now, batch_size: SolidObjects.configuration.prune_batch_size) ⇒ MessagePruner
Returns a new instance of MessagePruner.
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_size ⇒ Object (readonly)
Returns the value of attribute batch_size.
31 32 33 |
# File 'lib/solid_objects/message_pruner.rb', line 31 def batch_size @batch_size end |
#now ⇒ Object (readonly)
Returns the value of attribute now.
31 32 33 |
# File 'lib/solid_objects/message_pruner.rb', line 31 def now @now end |
Instance Method Details
#default_retention ⇒ Numeric
82 83 84 |
# File 'lib/solid_objects/message_pruner.rb', line 82 def default_retention SolidObjects.configuration. end |
#policy_relations ⇒ Array[ActiveRecord::Relation[Message]]
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 |
#preview ⇒ Integer
18 19 20 |
# File 'lib/solid_objects/message_pruner.rb', line 18 def preview policy_relations.sum(&:count) end |
#prunable ⇒ ActiveRecord::Relation[Message]
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 |
#prune ⇒ 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
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 = relation.limit(batch_size).pluck(:id) break if .empty? deleted += Message.where(id: ).delete_all end deleted end |
#retention_cutoff(retention) ⇒ Time
93 94 95 |
# File 'lib/solid_objects/message_pruner.rb', line 93 def retention_cutoff(retention) now - retention end |
#retention_overrides ⇒ Hash[String, Numeric]
87 88 89 90 |
# File 'lib/solid_objects/message_pruner.rb', line 87 def retention_overrides SolidObjects.configuration. .to_h { |actor_type, retention| [ actor_type.to_s, retention ] } end |