discourse/app/jobs/scheduled/redeliver_web_hook_events.rb
Guhyoun Nam 784c04ea81
FEATURE: Add Mechanism to redeliver all failed webhook events (#27609)
Background:
In order to redrive failed webhook events, an operator has to go through and click on each. This PR is adding a mechanism to retry all failed events to help resolve issues quickly once the underlying failure has been resolved.

What is the change?:
Previously, we had to redeliver each webhook event. This merge is adding a 'Redeliver Failed' button next to the webhook event filter to redeliver all failed events. If there is no failed webhook events to redeliver, 'Redeliver Failed' gets disabled. If you click it, a window pops up to confirm the operator. Failed webhook events will be added to the queue and webhook event list will show the redelivering progress. Every minute, a job will be ran to go through 20 events to redeliver. Every hour, a job will cleanup the redelivering events which have been stored more than 8 hours.
2024-07-08 15:43:16 -05:00

65 lines
1.8 KiB
Ruby

# frozen_string_literal: true
require "excon"
module Jobs
class RedeliverWebHookEvents < ::Jobs::Scheduled
every 1.minute
sidekiq_options queue: "low"
sidekiq_options retry: false
REDELIVERED = "redelivered"
REDELIVERY_FAILED = "redelivery_failed"
LIMIT = 20
def execute(args)
redelivery_events =
RedeliveringWebhookEvent
.where(processing: false)
.includes(web_hook_event: :web_hook)
.limit(LIMIT)
event_ids = redelivery_events.pluck(:id)
redelivery_events.update_all(processing: true)
updated_redelivery_events = RedeliveringWebhookEvent.where(id: event_ids)
updated_redelivery_events.each do |redelivery_event|
begin
web_hook_event = redelivery_event.web_hook_event
web_hook = web_hook_event.web_hook
emitter = WebHookEmitter.new(web_hook, web_hook_event)
emitter.emit!(
headers: MultiJson.load(web_hook_event.headers),
body: web_hook_event.payload,
)
publish_webhook_event(web_hook_event, web_hook, REDELIVERED)
RedeliveringWebhookEvent.delete(redelivery_event)
rescue => e
Discourse.warn_exception(
e,
message: "Error redelivering web_hook_event #{web_hook_event.id}",
)
publish_webhook_event(web_hook_event, web_hook, REDELIVERY_FAILED)
RedeliveringWebhookEvent.delete(redelivery_event)
end
sleep 2
end
end
private
def publish_webhook_event(web_hook_event, web_hook, type)
MessageBus.publish(
"/web_hook_events/#{web_hook.id}",
{
type: type,
web_hook_event: AdminWebHookEventSerializer.new(web_hook_event, root: false).as_json,
},
)
end
end
end