##// 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 import string
22 import string
23 import collections
23 import collections
24 import logging
24 import logging
25 import requests
26 from requests.adapters import HTTPAdapter
27 from requests.packages.urllib3.util.retry import Retry
25
28
26 from mako import exceptions
29 from mako import exceptions
27
30
@@ -320,3 +323,33 b' def render_with_traceback(template, *arg'
320 except Exception:
323 except Exception:
321 log.error(exceptions.text_error_template().render())
324 log.error(exceptions.text_error_template().render())
322 raise
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 from rhodecode.lib.celerylib import run_task, async_task, RequestContextTask
31 from rhodecode.lib.celerylib import run_task, async_task, RequestContextTask
32 from rhodecode.lib.colander_utils import strip_whitespace
32 from rhodecode.lib.colander_utils import strip_whitespace
33 from rhodecode.integrations.types.base import (
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 log = logging.getLogger(__name__)
37 log = logging.getLogger(__name__)
37
38
@@ -248,6 +249,6 b' def post_text_to_hipchat(settings, text)'
248 "color": settings.get('color', 'yellow'),
249 "color": settings.get('color', 'yellow'),
249 "notify": settings.get('notify', False),
250 "notify": settings.get('notify', False),
250 }
251 }
251
252 req_session = requests_retry_call()
252 resp = requests.post(settings['server_url'], json=json_message, timeout=60)
253 resp = req_session.post(settings['server_url'], json=json_message, timeout=60)
253 resp.raise_for_status() # raise exception on a failed request
254 resp.raise_for_status() # raise exception on a failed request
@@ -35,7 +35,8 b' from rhodecode.lib import helpers as h'
35 from rhodecode.lib.celerylib import run_task, async_task, RequestContextTask
35 from rhodecode.lib.celerylib import run_task, async_task, RequestContextTask
36 from rhodecode.lib.colander_utils import strip_whitespace
36 from rhodecode.lib.colander_utils import strip_whitespace
37 from rhodecode.integrations.types.base import (
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 log = logging.getLogger(__name__)
41 log = logging.getLogger(__name__)
41
42
@@ -344,6 +345,6 b' def post_text_to_slack(settings, title, '
344 "username": settings.get('username', 'Rhodecode'),
345 "username": settings.get('username', 'Rhodecode'),
345 "attachments": [message_data]
346 "attachments": [message_data]
346 }
347 }
347
348 req_session = requests_retry_call()
348 resp = requests.post(settings['service'], json=json_message, timeout=60)
349 resp = req_session.post(settings['service'], json=json_message, timeout=60)
349 resp.raise_for_status() # raise exception on a failed request
350 resp.raise_for_status() # raise exception on a failed request
@@ -23,17 +23,14 b' from __future__ import unicode_literals'
23 import deform
23 import deform
24 import deform.widget
24 import deform.widget
25 import logging
25 import logging
26 import requests
27 import requests.adapters
28 import colander
26 import colander
29 from requests.packages.urllib3.util.retry import Retry
30
27
31 import rhodecode
28 import rhodecode
32 from rhodecode import events
29 from rhodecode import events
33 from rhodecode.translation import _
30 from rhodecode.translation import _
34 from rhodecode.integrations.types.base import (
31 from rhodecode.integrations.types.base import (
35 IntegrationTypeBase, get_auth, get_web_token, get_url_vars,
32 IntegrationTypeBase, get_auth, get_web_token, get_url_vars,
36 WebhookDataHandler, WEBHOOK_URL_VARS)
33 WebhookDataHandler, WEBHOOK_URL_VARS, requests_retry_call)
37 from rhodecode.lib.celerylib import run_task, async_task, RequestContextTask
34 from rhodecode.lib.celerylib import run_task, async_task, RequestContextTask
38 from rhodecode.model.validation_schema import widgets
35 from rhodecode.model.validation_schema import widgets
39
36
@@ -233,11 +230,6 b' def post_to_webhook(url_calls, settings)'
233 'utc_timestamp': datetime.datetime(2017, 11, 30, 13, 0, 1, 569276)
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 call_headers = {
233 call_headers = {
242 'User-Agent': 'RhodeCode-webhook-caller/{}'.format(
234 'User-Agent': 'RhodeCode-webhook-caller/{}'.format(
243 rhodecode.__version__)
235 rhodecode.__version__)
@@ -247,9 +239,7 b' def post_to_webhook(url_calls, settings)'
247 token = get_web_token(settings)
239 token = get_web_token(settings)
248
240
249 for url, headers, data in url_calls:
241 for url, headers, data in url_calls:
250 req_session = requests.Session()
242 req_session = requests_retry_call()
251 req_session.mount( # retry max N times
252 'http://', requests.adapters.HTTPAdapter(max_retries=retries))
253
243
254 method = settings.get('method_type') or 'post'
244 method = settings.get('method_type') or 'post'
255 call_method = getattr(req_session, method)
245 call_method = getattr(req_session, method)
General Comments 0
You need to be logged in to leave comments. Login now