Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Purge unsubmitted journey sessions #2951

Merged
merged 1 commit into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions app/jobs/purge_unsubmitted_claims_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ class PurgeUnsubmittedClaimsJob < CronJob
self.cron_expression = "0 0 * * *"

def perform
Rails.logger.info "Purging #{old_unsubmitted_claims.count} old and unsubmitted claims from the database"
old_unsubmitted_claims.destroy_all
Rails.logger.info "Purging #{old_unsubmitted_journeys.count} old and unsubmitted journeys from the database"
old_unsubmitted_journeys.destroy_all
end

private

def old_unsubmitted_claims
Claim.unsubmitted.where("updated_at < ?", 24.hours.ago)
def old_unsubmitted_journeys
Journeys::Session.purgeable
end
end
8 changes: 8 additions & 0 deletions app/models/journeys/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module Journeys
class Session < ApplicationRecord
self.abstract_class = true

self.table_name = "journeys_sessions"

has_one :claim,
dependent: :nullify,
inverse_of: :journey_session,
Expand All @@ -11,6 +13,12 @@ class Session < ApplicationRecord
presence: true,
inclusion: {in: Journeys.all_routing_names}

scope :unsubmitted, -> { where.missing(:claim) }

scope :purgeable, -> do
unsubmitted.where(journeys_sessions: {updated_at: ..24.hours.ago})
end

def submitted?
claim.present?
end
Expand Down
32 changes: 26 additions & 6 deletions spec/jobs/purge_unsubmitted_claims_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,37 @@
let(:four_hours_ago) { 4.hours.ago }

it "destroys any unsubmitted claims that have not been updated in the last 24 hours" do
expired_unsubmitted_claim = create(:claim, updated_at: over_24_hours_ago)
active_unsubmitted_claim = create(:claim, updated_at: four_hours_ago)
submitted_journeys = []
unsubmitted_fresh_journeys = []
unsubmitted_expired_journeys = []

old_submitted_claim = create(:claim, :submitted, updated_at: over_24_hours_ago)
Journeys::JOURNEYS.each do |journey|
submitted_journeys << journey::Session.create!(
journey: journey::ROUTING_NAME,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we might want to consider switching from storing the journey on the journey session like this to proper STI. (Not sure if it's worth the effort as I don't expect us to do much querying on the journey session table)

claim: create(:claim),
updated_at: over_24_hours_ago
)

unsubmitted_fresh_journeys << journey::Session.create!(
journey: journey::ROUTING_NAME,
updated_at: four_hours_ago
)

unsubmitted_expired_journeys << journey::Session.create!(
journey: journey::ROUTING_NAME,
updated_at: over_24_hours_ago
)
end

PurgeUnsubmittedClaimsJob.new.perform

expect(Claim.exists?(expired_unsubmitted_claim.id)).to eq false
expect(submitted_journeys.each(&:reload)).to all be_persisted

expect(unsubmitted_fresh_journeys.each(&:reload)).to all be_persisted

expect(Claim.exists?(old_submitted_claim.id)).to eq true
expect(Claim.exists?(active_unsubmitted_claim.id)).to eq true
expect(
Journeys::Session.where(id: unsubmitted_expired_journeys.map(&:id))
).to be_empty
end
end
end