Skip to content

Commit

Permalink
Add clock job to clean old (older than 90 days) inbound webhooks from DB
Browse files Browse the repository at this point in the history
  • Loading branch information
ancorcruz committed Dec 17, 2024
1 parent f0fa696 commit 88d8eed
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 0 deletions.
13 changes: 13 additions & 0 deletions app/jobs/clock/inbound_webhooks_cleanup_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

module Clock
class InboundWebhooksCleanupJob < ApplicationJob
include SentryCronConcern

queue_as 'clock'

def perform
InboundWebhook.where('updated_at < ?', 90.days.ago).destroy_all
end
end
end
6 changes: 6 additions & 0 deletions clock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ module Clockwork
.perform_later
end

every(1.day, 'schedule:clean_inbound_webhooks', at: '01:10') do
Clock::InboundWebhooksCleanupJob
.set(sentry: {"slug" => 'lago_clean_inbound_webhooks', "cron" => '5 1 * * *'})
.perform_later
end

unless ActiveModel::Type::Boolean.new.cast(ENV['LAGO_DISABLE_EVENTS_VALIDATION'])
every(1.hour, 'schedule:post_validate_events', at: '*:05') do
Clock::EventsValidationJob
Expand Down
21 changes: 21 additions & 0 deletions spec/clockwork_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,25 @@
expect(Clock::ProcessDunningCampaignsJob).to have_been_enqueued
end
end

describe "schedule:clean_inbound_webhooks" do
let(:job) { "schedule:clean_inbound_webhooks" }
let(:start_time) { Time.zone.parse("1 Apr 2022 00:01:00") }
let(:end_time) { Time.zone.parse("2 Apr 2022 00:00:00") }

it "enqueue a clean inbound webhooks job" do
Clockwork::Test.run(
file: clock_file,
start_time:,
end_time:,
tick_speed: 1.minute
)

expect(Clockwork::Test).to be_ran_job(job)
expect(Clockwork::Test.times_run(job)).to eq(1)

Clockwork::Test.block_for(job).call
expect(Clock::InboundWebhooksCleanupJob).to have_been_enqueued
end
end
end
23 changes: 23 additions & 0 deletions spec/jobs/clock/inbound_webhooks_cleanup_job_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

require "rails_helper"

describe Clock::InboundWebhooksCleanupJob, job: true do
subject(:inbound_webhooks_cleanup_job) { described_class }

describe ".perform" do
it "removes all old inbound webhooks" do
create(:inbound_webhook, updated_at: 90.days.ago)

expect { inbound_webhooks_cleanup_job.perform_now }
.to change(InboundWebhook, :count).to(0)
end

it "does not delete recent inbound webhooks" do
create(:inbound_webhook, updated_at: 89.days.ago)

expect { inbound_webhooks_cleanup_job.perform_now }
.not_to change(InboundWebhook, :count)
end
end
end

0 comments on commit 88d8eed

Please sign in to comment.