From 77be6dea6fda3b103f5f8d66847156dc39b31816 Mon Sep 17 00:00:00 2001 From: Sean Rankine Date: Fri, 6 Dec 2024 15:51:14 +0000 Subject: [PATCH] Revert "Merge pull request #461 from alphagov/remove-housekeeping" This reverts commit 756a9494cde5b3614f0142748dadde20111b9e44, reversing changes made to 211dadec9870f48955fe8b1fcf1bcbd3adcb6656. --- app/models/publish_intent.rb | 5 +++++ lib/tasks/housekeeping.rake | 6 ++++++ spec/models/publish_intent_spec.rb | 27 +++++++++++++++++++++++++++ 3 files changed, 38 insertions(+) create mode 100644 lib/tasks/housekeeping.rake diff --git a/app/models/publish_intent.rb b/app/models/publish_intent.rb index b2b1085d7..b59bcad90 100644 --- a/app/models/publish_intent.rb +++ b/app/models/publish_intent.rb @@ -47,6 +47,11 @@ def content_item ContentItem.where(base_path:).first end + # Called nightly from a cron job + def self.cleanup_expired + where("publish_time < ?", PUBLISH_TIME_LEEWAY.ago).delete_all + end + def base_path_without_root base_path&.sub(%r{^/}, "") end diff --git a/lib/tasks/housekeeping.rake b/lib/tasks/housekeeping.rake new file mode 100644 index 000000000..b274c08ba --- /dev/null +++ b/lib/tasks/housekeeping.rake @@ -0,0 +1,6 @@ +namespace :housekeeping do + desc "Delete any publish intents in the past" + task cleanup_publish_intents: :environment do + PublishIntent.cleanup_expired + end +end diff --git a/spec/models/publish_intent_spec.rb b/spec/models/publish_intent_spec.rb index 1e60ab781..150c6aac5 100644 --- a/spec/models/publish_intent_spec.rb +++ b/spec/models/publish_intent_spec.rb @@ -155,4 +155,31 @@ ) end end + + describe ".cleanup_expired" do + before :each do + create(:publish_intent, publish_time: 3.days.ago) + create(:publish_intent, publish_time: 2.days.ago) + create(:publish_intent, publish_time: 1.hour.ago) + create(:publish_intent, publish_time: 10.minutes.from_now) + create(:publish_intent, publish_time: 10.days.from_now) + create(:publish_intent, publish_time: 1.year.from_now) + end + + it "deletes all publish_intents with publish_at in the past" do + PublishIntent.cleanup_expired + + expect(PublishIntent.count).to eq(3) + expect(PublishIntent.where(:publish_time.gte => Time.zone.now).count).to eq(3) + end + + it "does not delete very recently passed intents" do + recent = create(:publish_intent, publish_time: 30.seconds.ago) + + PublishIntent.cleanup_expired + + expect(PublishIntent.where(base_path: recent.base_path).first).to be + expect(PublishIntent.count).to eq(4) + end + end end