Skip to content

Commit

Permalink
Merge pull request #1360 from alphagov/add-publishing-intents-cleanup
Browse files Browse the repository at this point in the history
Add rake task to remove old publishing intents
  • Loading branch information
theseanything authored Dec 6, 2024
2 parents 411a9f4 + 0de17fb commit ef5074d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
5 changes: 5 additions & 0 deletions app/models/publish_intent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions lib/tasks/housekeeping.rake
Original file line number Diff line number Diff line change
@@ -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
27 changes: 27 additions & 0 deletions spec/models/publish_intent_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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: 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

0 comments on commit ef5074d

Please sign in to comment.