Class: Rubino::Jobs::Handlers::CleanupSessionsJob

Inherits:
Object
  • Object
show all
Defined in:
lib/rubino/jobs/handlers/cleanup_sessions_job.rb

Overview

Cleans up old ended sessions beyond retention period.

Constant Summary collapse

RETENTION_DAYS =
30

Instance Method Summary collapse

Instance Method Details

#perform(payload) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rubino/jobs/handlers/cleanup_sessions_job.rb', line 10

def perform(payload)
  retention = payload[:retention_days] || RETENTION_DAYS
  cutoff = (Time.now - (retention * 86_400)).utc.iso8601

  db = Rubino.database.db
  old_sessions = db[:sessions]
                 .where(status: "ended")
                 .where { ended_at < cutoff }
                 .select(:id)
                 .all

  repo = Session::Repository.new
  old_sessions.each do |s|
    repo.destroy!(s[:id])
  end
end