From 3e3e6622382149e7c1affd3fd63ac951f15912bc Mon Sep 17 00:00:00 2001 From: Davis Raymond Date: Thu, 11 May 2023 11:51:30 +0300 Subject: [PATCH] bug(webhooks): add configurable request timeout for webhooks (#2419) - Handle unhandled connection error while sending webhooks --- onadata/apps/restservice/services/generic_json.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/onadata/apps/restservice/services/generic_json.py b/onadata/apps/restservice/services/generic_json.py index 24c97e693d..591b72cdbd 100644 --- a/onadata/apps/restservice/services/generic_json.py +++ b/onadata/apps/restservice/services/generic_json.py @@ -4,10 +4,15 @@ """ import json +from django.conf import settings + import requests +from requests.exceptions import ConnectionError as RequestsConnectionError from onadata.apps.restservice.RestServiceInterface import RestServiceInterface +WEBHOOK_TIMEOUT = getattr(settings, "WEBHOOK_TIMEOUT", 30) + class ServiceDefinition(RestServiceInterface): """Post submisison JSON data to an external service that accepts a JSON post.""" @@ -21,4 +26,9 @@ def send(self, url, data=None): if data: post_data = json.dumps(data.json) headers = {"Content-Type": "application/json"} - requests.post(url, headers=headers, data=post_data) + try: + requests.post( + url, headers=headers, data=post_data, timeout=WEBHOOK_TIMEOUT + ) + except RequestsConnectionError: + pass