From 6bc23953ba73e3c82b1c51bda24ed4f7869eadb1 Mon Sep 17 00:00:00 2001 From: Davis Raymond Muro Date: Tue, 9 May 2023 09:49:13 +0300 Subject: [PATCH] bug(webhooks): add configurable request timeout for webhooks - 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..0b417c27ec 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 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 ConnectionError: + pass