Class: EventStorePurgeWorker

Inherits:
Object
  • Object
show all
Includes:
Sidekiq::Worker
Defined in:
app/workers/event_store_purge_worker.rb

Overview

Deletes event_store_events rows older than RETENTION_DAYS to prevent unbounded
table growth. The FK from event_store_events_in_streams does not cascade, so
we delete stream-link rows first, then the events themselves.

Retention defaults to 90 days. Override via EVENT_STORE_RETENTION_DAYS env var.

Constant Summary collapse

RETENTION_DAYS =
ENV.fetch('EVENT_STORE_RETENTION_DAYS', 90).to_i

Instance Method Summary collapse

Instance Method Details

#performObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/workers/event_store_purge_worker.rb', line 15

def perform
  cutoff = RETENTION_DAYS.days.ago

  conn = ActiveRecord::Base.connection

  # 1. Remove stream-link rows whose parent event is older than cutoff.
  conn.execute(<<~SQL)
    DELETE FROM event_store_events_in_streams
     WHERE event_id IN (
       SELECT event_id FROM event_store_events WHERE created_at < '#{cutoff.utc.iso8601}'
     )
  SQL

  # 2. Remove the events themselves.
  result = conn.execute(<<~SQL)
    DELETE FROM event_store_events
     WHERE created_at < '#{cutoff.utc.iso8601}'
  SQL

  deleted = result.cmd_tuples
  Rails.logger.info "[EventStorePurgeWorker] Deleted #{deleted} events older than #{cutoff.strftime('%Y-%m-%d')}"
end