Add a defined timeout on all HTTP calls in notification methods

This commit is contained in:
Joseph Schorr 2016-11-08 18:20:38 -05:00
parent 83e8d62bea
commit 7e78406112
2 changed files with 10 additions and 4 deletions

View file

@ -291,6 +291,9 @@ class DefaultConfig(object):
# How often the Garbage Collection worker runs.
GARBAGE_COLLECTION_FREQUENCY = 30 # seconds
# How long notifications will try to send before timing out.
NOTIFICATION_SEND_TIMEOUT = 10
# Security scanner
FEATURE_SECURITY_SCANNER = False
FEATURE_SECURITY_NOTIFICATIONS = False

View file

@ -14,6 +14,8 @@ from workers.queueworker import JobException
logger = logging.getLogger(__name__)
METHOD_TIMEOUT = app.config.get('NOTIFICATION_SEND_TIMEOUT', 10) # Seconds
class InvalidNotificationMethodException(Exception):
pass
@ -190,7 +192,8 @@ class WebhookMethod(NotificationMethod):
headers = {'Content-type': 'application/json'}
try:
resp = requests.post(url, data=json.dumps(payload), headers=headers, cert=SSLClientCert)
resp = requests.post(url, data=json.dumps(payload), headers=headers, cert=SSLClientCert,
timeout=METHOD_TIMEOUT)
if resp.status_code/100 != 2:
error_message = '%s response for webhook to url: %s' % (resp.status_code, url)
logger.error(error_message)
@ -241,7 +244,7 @@ class FlowdockMethod(NotificationMethod):
}
try:
resp = requests.post(url, data=json.dumps(payload), headers=headers)
resp = requests.post(url, data=json.dumps(payload), headers=headers, timeout=METHOD_TIMEOUT)
if resp.status_code/100 != 2:
error_message = '%s response for flowdock to url: %s' % (resp.status_code, url)
logger.error(error_message)
@ -302,7 +305,7 @@ class HipchatMethod(NotificationMethod):
}
try:
resp = requests.post(url, data=json.dumps(payload), headers=headers)
resp = requests.post(url, data=json.dumps(payload), headers=headers, timeout=METHOD_TIMEOUT)
if resp.status_code/100 != 2:
error_message = '%s response for hipchat to url: %s' % (resp.status_code, url)
logger.error(error_message)
@ -420,7 +423,7 @@ class SlackMethod(NotificationMethod):
}
try:
resp = requests.post(url, data=json.dumps(payload), headers=headers)
resp = requests.post(url, data=json.dumps(payload), headers=headers, timeout=METHOD_TIMEOUT)
if resp.status_code/100 != 2:
error_message = '%s response for Slack to url: %s' % (resp.status_code, url)
logger.error(error_message)