##// END OF EJS Templates
integrations: implement retry to HTTP[S] calls for integrations.
marcink -
r3110:b4084273 default
parent child Browse files
Show More
@@ -22,6 +22,9 b' import colander'
22 22 import string
23 23 import collections
24 24 import logging
25 import requests
26 from requests.adapters import HTTPAdapter
27 from requests.packages.urllib3.util.retry import Retry
25 28
26 29 from mako import exceptions
27 30
@@ -320,3 +323,33 b' def render_with_traceback(template, *arg'
320 323 except Exception:
321 324 log.error(exceptions.text_error_template().render())
322 325 raise
326
327
328 STATUS_400 = (400, 401, 403)
329 STATUS_500 = (500, 502, 504)
330
331
332 def requests_retry_call(
333 retries=3, backoff_factor=0.3, status_forcelist=STATUS_400+STATUS_500,
334 session=None):
335 """
336 session = requests_retry_session()
337 response = session.get('http://example.com')
338
339 :param retries:
340 :param backoff_factor:
341 :param status_forcelist:
342 :param session:
343 """
344 session = session or requests.Session()
345 retry = Retry(
346 total=retries,
347 read=retries,
348 connect=retries,
349 backoff_factor=backoff_factor,
350 status_forcelist=status_forcelist,
351 )
352 adapter = HTTPAdapter(max_retries=retry)
353 session.mount('http://', adapter)
354 session.mount('https://', adapter)
355 return session
@@ -31,7 +31,8 b' from rhodecode.lib import helpers as h'
31 31 from rhodecode.lib.celerylib import run_task, async_task, RequestContextTask
32 32 from rhodecode.lib.colander_utils import strip_whitespace
33 33 from rhodecode.integrations.types.base import (
34 IntegrationTypeBase, CommitParsingDataHandler, render_with_traceback)
34 IntegrationTypeBase, CommitParsingDataHandler, render_with_traceback,
35 requests_retry_call)
35 36
36 37 log = logging.getLogger(__name__)
37 38
@@ -248,6 +249,6 b' def post_text_to_hipchat(settings, text)'
248 249 "color": settings.get('color', 'yellow'),
249 250 "notify": settings.get('notify', False),
250 251 }
251
252 resp = requests.post(settings['server_url'], json=json_message, timeout=60)
252 req_session = requests_retry_call()
253 resp = req_session.post(settings['server_url'], json=json_message, timeout=60)
253 254 resp.raise_for_status() # raise exception on a failed request
@@ -35,7 +35,8 b' from rhodecode.lib import helpers as h'
35 35 from rhodecode.lib.celerylib import run_task, async_task, RequestContextTask
36 36 from rhodecode.lib.colander_utils import strip_whitespace
37 37 from rhodecode.integrations.types.base import (
38 IntegrationTypeBase, CommitParsingDataHandler, render_with_traceback)
38 IntegrationTypeBase, CommitParsingDataHandler, render_with_traceback,
39 requests_retry_call)
39 40
40 41 log = logging.getLogger(__name__)
41 42
@@ -344,6 +345,6 b' def post_text_to_slack(settings, title, '
344 345 "username": settings.get('username', 'Rhodecode'),
345 346 "attachments": [message_data]
346 347 }
347
348 resp = requests.post(settings['service'], json=json_message, timeout=60)
348 req_session = requests_retry_call()
349 resp = req_session.post(settings['service'], json=json_message, timeout=60)
349 350 resp.raise_for_status() # raise exception on a failed request
@@ -23,17 +23,14 b' from __future__ import unicode_literals'
23 23 import deform
24 24 import deform.widget
25 25 import logging
26 import requests
27 import requests.adapters
28 26 import colander
29 from requests.packages.urllib3.util.retry import Retry
30 27
31 28 import rhodecode
32 29 from rhodecode import events
33 30 from rhodecode.translation import _
34 31 from rhodecode.integrations.types.base import (
35 32 IntegrationTypeBase, get_auth, get_web_token, get_url_vars,
36 WebhookDataHandler, WEBHOOK_URL_VARS)
33 WebhookDataHandler, WEBHOOK_URL_VARS, requests_retry_call)
37 34 from rhodecode.lib.celerylib import run_task, async_task, RequestContextTask
38 35 from rhodecode.model.validation_schema import widgets
39 36
@@ -233,11 +230,6 b' def post_to_webhook(url_calls, settings)'
233 230 'utc_timestamp': datetime.datetime(2017, 11, 30, 13, 0, 1, 569276)
234 231
235 232 """
236 max_retries = 3
237 retries = Retry(
238 total=max_retries,
239 backoff_factor=0.15,
240 status_forcelist=[500, 502, 503, 504])
241 233 call_headers = {
242 234 'User-Agent': 'RhodeCode-webhook-caller/{}'.format(
243 235 rhodecode.__version__)
@@ -247,9 +239,7 b' def post_to_webhook(url_calls, settings)'
247 239 token = get_web_token(settings)
248 240
249 241 for url, headers, data in url_calls:
250 req_session = requests.Session()
251 req_session.mount( # retry max N times
252 'http://', requests.adapters.HTTPAdapter(max_retries=retries))
242 req_session = requests_retry_call()
253 243
254 244 method = settings.get('method_type') or 'post'
255 245 call_method = getattr(req_session, method)
General Comments 0
You need to be logged in to leave comments. Login now