From f8b24ebfce2c51b22008853fcd18969cd4c3a95b Mon Sep 17 00:00:00 2001 From: Pete Matsyburka Date: Fri, 9 Feb 2024 23:04:42 +0200 Subject: [PATCH] retry webhooks on non 200 status --- ...send_form_completed_webhook_request_job.rb | 22 ++++++++++++------- .../send_form_started_webhook_request_job.rb | 22 ++++++++++++------- .../send_form_viewed_webhook_request_job.rb | 22 ++++++++++++------- 3 files changed, 42 insertions(+), 24 deletions(-) diff --git a/app/jobs/send_form_completed_webhook_request_job.rb b/app/jobs/send_form_completed_webhook_request_job.rb index 4fe65ff6..9213e643 100644 --- a/app/jobs/send_form_completed_webhook_request_job.rb +++ b/app/jobs/send_form_completed_webhook_request_job.rb @@ -3,6 +3,8 @@ class SendFormCompletedWebhookRequestJob < ApplicationJob USER_AGENT = 'DocuSeal.co Webhook' + NotSuccessStatus = Class.new(StandardError) + def perform(submitter) config = Accounts.load_webhook_configs(submitter.submission.account) @@ -12,13 +14,17 @@ class SendFormCompletedWebhookRequestJob < ApplicationJob ActiveStorage::Current.url_options = Docuseal.default_url_options - Faraday.post(config.value, - { - event_type: 'form.completed', - timestamp: Time.current, - data: Submitters::SerializeForWebhook.call(submitter) - }.to_json, - 'Content-Type' => 'application/json', - 'User-Agent' => USER_AGENT) + resp = Faraday.post(config.value, + { + event_type: 'form.completed', + timestamp: Time.current, + data: Submitters::SerializeForWebhook.call(submitter) + }.to_json, + 'Content-Type' => 'application/json', + 'User-Agent' => USER_AGENT) + + if resp.status.to_i >= 400 && (!Docuseal.multitenant? || submitter.account.account_configs.exists?(key: :plan)) + raise NotSuccessStatus, resp.status.to_s + end end end diff --git a/app/jobs/send_form_started_webhook_request_job.rb b/app/jobs/send_form_started_webhook_request_job.rb index b14e273f..1f8fa206 100644 --- a/app/jobs/send_form_started_webhook_request_job.rb +++ b/app/jobs/send_form_started_webhook_request_job.rb @@ -3,6 +3,8 @@ class SendFormStartedWebhookRequestJob < ApplicationJob USER_AGENT = 'DocuSeal.co Webhook' + NotSuccessStatus = Class.new(StandardError) + def perform(submitter) config = Accounts.load_webhook_configs(submitter.submission.account) @@ -10,13 +12,17 @@ class SendFormStartedWebhookRequestJob < ApplicationJob ActiveStorage::Current.url_options = Docuseal.default_url_options - Faraday.post(config.value, - { - event_type: 'form.started', - timestamp: Time.current, - data: Submitters::SerializeForWebhook.call(submitter) - }.to_json, - 'Content-Type' => 'application/json', - 'User-Agent' => USER_AGENT) + resp = Faraday.post(config.value, + { + event_type: 'form.started', + timestamp: Time.current, + data: Submitters::SerializeForWebhook.call(submitter) + }.to_json, + 'Content-Type' => 'application/json', + 'User-Agent' => USER_AGENT) + + if resp.status.to_i >= 400 && (!Docuseal.multitenant? || submitter.account.account_configs.exists?(key: :plan)) + raise NotSuccessStatus, resp.status.to_s + end end end diff --git a/app/jobs/send_form_viewed_webhook_request_job.rb b/app/jobs/send_form_viewed_webhook_request_job.rb index 65b5c496..0ae32e43 100644 --- a/app/jobs/send_form_viewed_webhook_request_job.rb +++ b/app/jobs/send_form_viewed_webhook_request_job.rb @@ -3,6 +3,8 @@ class SendFormViewedWebhookRequestJob < ApplicationJob USER_AGENT = 'DocuSeal.co Webhook' + NotSuccessStatus = Class.new(StandardError) + def perform(submitter) config = Accounts.load_webhook_configs(submitter.submission.account) @@ -10,13 +12,17 @@ class SendFormViewedWebhookRequestJob < ApplicationJob ActiveStorage::Current.url_options = Docuseal.default_url_options - Faraday.post(config.value, - { - event_type: 'form.viewed', - timestamp: Time.current, - data: Submitters::SerializeForWebhook.call(submitter) - }.to_json, - 'Content-Type' => 'application/json', - 'User-Agent' => USER_AGENT) + resp = Faraday.post(config.value, + { + event_type: 'form.viewed', + timestamp: Time.current, + data: Submitters::SerializeForWebhook.call(submitter) + }.to_json, + 'Content-Type' => 'application/json', + 'User-Agent' => USER_AGENT) + + if resp.status.to_i >= 400 && (!Docuseal.multitenant? || submitter.account.account_configs.exists?(key: :plan)) + raise NotSuccessStatus, resp.status.to_s + end end end