From b74764c6377af8b6e29d7e835bd56950cfca4737 Mon Sep 17 00:00:00 2001 From: Ancor Cruz Date: Tue, 17 Dec 2024 13:56:54 +0000 Subject: [PATCH] Add clock job to clean old (older than 90 days) inbound webhooks from DB --- .../clock/inbound_webhooks_cleanup_job.rb | 13 +++++++++++ clock.rb | 6 +++++ spec/clockwork_spec.rb | 21 +++++++++++++++++ .../inbound_webhooks_cleanup_job_spec.rb | 23 +++++++++++++++++++ 4 files changed, 63 insertions(+) create mode 100644 app/jobs/clock/inbound_webhooks_cleanup_job.rb create mode 100644 spec/jobs/clock/inbound_webhooks_cleanup_job_spec.rb diff --git a/app/jobs/clock/inbound_webhooks_cleanup_job.rb b/app/jobs/clock/inbound_webhooks_cleanup_job.rb new file mode 100644 index 00000000000..2b8e07ff205 --- /dev/null +++ b/app/jobs/clock/inbound_webhooks_cleanup_job.rb @@ -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 diff --git a/clock.rb b/clock.rb index 3db485ebb01..438110ef7ae 100644 --- a/clock.rb +++ b/clock.rb @@ -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 diff --git a/spec/clockwork_spec.rb b/spec/clockwork_spec.rb index f5a3d65f4b0..a77fb9fddb4 100644 --- a/spec/clockwork_spec.rb +++ b/spec/clockwork_spec.rb @@ -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 diff --git a/spec/jobs/clock/inbound_webhooks_cleanup_job_spec.rb b/spec/jobs/clock/inbound_webhooks_cleanup_job_spec.rb new file mode 100644 index 00000000000..e3765701b80 --- /dev/null +++ b/spec/jobs/clock/inbound_webhooks_cleanup_job_spec.rb @@ -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