Skip to content

Commit

Permalink
Add a Retry job for failed inbound webhooks
Browse files Browse the repository at this point in the history
  • Loading branch information
ancorcruz committed Dec 17, 2024
1 parent 88d8eed commit d3e6956
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
15 changes: 15 additions & 0 deletions app/jobs/clock/inbound_webhooks_retry_job.rb
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions clock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
21 changes: 21 additions & 0 deletions spec/clockwork_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
30 changes: 30 additions & 0 deletions spec/jobs/clock/inbound_webhooks_retry_job_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit d3e6956

Please sign in to comment.