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

Add rake task to remove old publishing intents #1360

Merged
merged 1 commit into from
Dec 6, 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
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
Loading