Periodically cleanup deliveries

This commit is contained in:
Stanko K.R.
2025-09-12 17:35:05 +02:00
parent 9d77b17b8c
commit fee25a49ea
4 changed files with 29 additions and 0 deletions
@@ -0,0 +1,7 @@
class Webhook::CleanupDeliveriesJob < ApplicationJob
def perform
ApplicationRecord.with_each_tenant do
Webhook::Delivery.cleanup
end
end
end
+6
View File
@@ -1,4 +1,5 @@
class Webhook::Delivery < ApplicationRecord
STALE_TRESHOLD = 7.days
USER_AGENT = "fizzy/1.0.0 Webhook"
ENDPOINT_TIMEOUT = 7.seconds
DNS_RESOLUTION_TIMEOUT = 2
@@ -18,9 +19,14 @@ class Webhook::Delivery < ApplicationRecord
enum :state, %w[ pending in_progress completed errored ].index_by(&:itself), default: :pending
scope :ordered, -> { order created_at: :desc, id: :desc }
scope :stale, -> { where(created_at: ...STALE_TRESHOLD.ago) }
after_create_commit :deliver_later
def self.cleanup
stale.delete_all
end
def deliver_later
Webhook::DeliveryJob.perform_later(self)
end
+3
View File
@@ -26,6 +26,9 @@ production: &production
generate_weekly_highlights:
command: "User.generate_all_weekly_highlights_later"
schedule: every sunday at noon
cleanup_webhook_deliveries:
command: "Webhook::CleanupDeliveriesJob"
schedule: every 4 hours
beta: *production
staging: *production
+13
View File
@@ -190,4 +190,17 @@ class Webhook::DeliveryTest < ActiveSupport::TestCase
assert_requested request_stub
assert delivery.succeeded?
end
test "cleanup" do
webhook = webhooks(:active)
event = events(:layout_commented)
fresh_delivery = Webhook::Delivery.create!(webhook: webhook, event: event)
stale_delivery = Webhook::Delivery.create!(webhook: webhook, event: event, created_at: 8.days.ago)
Webhook::Delivery.cleanup
assert Webhook::Delivery.exists?(fresh_delivery.id)
assert_not Webhook::Delivery.exists?(stale_delivery.id)
end
end