feat: Mark the messages as failed if the API channel webhooks fail for any reason. (#8277)

This commit is contained in:
Muhsin Keloth
2023-11-04 12:26:28 +05:30
committed by GitHub
parent 3b84b0fc47
commit b4d20689b7
7 changed files with 129 additions and 21 deletions

View File

@@ -1,13 +1,57 @@
class Webhooks::Trigger
def self.execute(url, payload)
response = RestClient::Request.execute(
SUPPORTED_ERROR_HANDLE_EVENTS = %w[message_created message_updated].freeze
def initialize(url, payload, webhook_type)
@url = url
@payload = payload
@webhook_type = webhook_type
end
def self.execute(url, payload, webhook_type)
new(url, payload, webhook_type).execute
end
def execute
perform_request
rescue RestClient::Exceptions::Timeout, RestClient::ExceptionWithResponse => e
handle_error(e)
Rails.logger.warn "Exception: Invalid webhook URL #{@url} : #{e.message}"
end
private
def perform_request
RestClient::Request.execute(
method: :post,
url: url, payload: payload.to_json,
url: @url,
payload: @payload.to_json,
headers: { content_type: :json, accept: :json },
timeout: 5
)
Rails.logger.info "Performed Request: Code - #{response.code}"
rescue StandardError => e
Rails.logger.warn "Exception: invalid webhook url #{url} : #{e.message}"
end
def handle_error(error)
return unless should_handle_error?
return unless message
update_message_status(error)
end
def should_handle_error?
@webhook_type == :api_inbox_webhook && SUPPORTED_ERROR_HANDLE_EVENTS.include?(@payload[:event])
end
def update_message_status(error)
message.update!(status: :failed, external_error: error.message)
end
def message
return if message_id.blank?
@message ||= Message.find_by(id: message_id)
end
def message_id
@payload[:id]
end
end