diff --git a/app/jobs/clock/inbound_webhooks_retry_job.rb b/app/jobs/clock/inbound_webhooks_retry_job.rb new file mode 100644 index 00000000000..4b35a747e43 --- /dev/null +++ b/app/jobs/clock/inbound_webhooks_retry_job.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +module Clock + class InboundWebhooksRetryJob < ApplicationJob + include SentryCronConcern + + queue_as 'clock' + + def perform + InboundWebhook.failed.find_each do |inbound_webhook| + InboundWebhooks::ProcessJob.perform_later(inbound_webhook:) + end + end + end +end diff --git a/clock.rb b/clock.rb index 438110ef7ae..76fdaacfca7 100644 --- a/clock.rb +++ b/clock.rb @@ -152,4 +152,10 @@ module Clockwork .set(sentry: {"slug" => "lago_retry_failed_invoices", "cron" => '*/15 * * * *'}) .perform_later end + + every(15.minutes, "schedule:retry_failed_inbound_webhooks") do + Clock::InboundWebhooksRetryJob + .set(sentry: {"slug" => "lago_retry_failed_inbound_webhooks", "cron" => '*/15 * * * *'}) + .perform_later + end end diff --git a/spec/clockwork_spec.rb b/spec/clockwork_spec.rb index a77fb9fddb4..8e50669d660 100644 --- a/spec/clockwork_spec.rb +++ b/spec/clockwork_spec.rb @@ -198,4 +198,25 @@ expect(Clock::InboundWebhooksCleanupJob).to have_been_enqueued end end + + describe "schedule:retry_failed_inbound_webhooks" do + let(:job) { "schedule:retry_failed_inbound_webhooks" } + let(:start_time) { Time.zone.parse("1 Apr 2022 00:05:00") } + let(:end_time) { Time.zone.parse("1 Apr 2022 00:20:00") } + + it "enqueue a retry failed 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::InboundWebhooksRetryJob).to have_been_enqueued + end + end end diff --git a/spec/jobs/clock/inbound_webhooks_retry_job_spec.rb b/spec/jobs/clock/inbound_webhooks_retry_job_spec.rb new file mode 100644 index 00000000000..e198f7e54f6 --- /dev/null +++ b/spec/jobs/clock/inbound_webhooks_retry_job_spec.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe Clock::InboundWebhooksRetryJob, job: true do + subject(:inbound_webhooks_retry_job) { described_class } + + describe '.perform' do + let(:pending_inbound_webhook) { create :inbound_webhook, status: "pending" } + let(:failed_inbound_webhook) { create :inbound_webhook, status: "failed" } + let(:processing_inbound_webhoook) { create :inbound_webhook, status: "processing" } + let(:processed_inbound_webhook) { create :inbound_webhook, status: "processed" } + + before do + pending_inbound_webhook + failed_inbound_webhook + processed_inbound_webhook + processed_inbound_webhook + end + + it "queues a job to process the failed inbound webhook" do + inbound_webhooks_retry_job.perform_now + + expect(InboundWebhooks::ProcessJob).to have_been_enqueued.once + expect(InboundWebhooks::ProcessJob) + .to have_been_enqueued + .with(inbound_webhook: failed_inbound_webhook) + end + end +end