~marcoceppi/charm-tools/patch-generate-path

« back to all changes in this revision

Viewing changes to tests/trusty/mysql/hooks/charmhelpers/core/decorators.py

  • Committer: Tim Van Steenburgh
  • Date: 2015-08-31 20:23:05 UTC
  • mfrom: (354.2.28 compose)
  • Revision ID: tim.van.steenburgh@canonical.com-20150831202305-6z5ra37q0atmq92b
[bcsaller] Add charm composer and port tests to tox.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# Copyright 2014 Canonical Ltd.
 
3
#
 
4
# Authors:
 
5
#  Edward Hope-Morley <opentastic@gmail.com>
 
6
#
 
7
 
 
8
import time
 
9
 
 
10
from charmhelpers.core.hookenv import (
 
11
    log,
 
12
    INFO,
 
13
)
 
14
 
 
15
 
 
16
def retry_on_exception(num_retries, base_delay=0, exc_type=Exception):
 
17
    """If the decorated function raises exception exc_type, allow num_retries
 
18
    retry attempts before raise the exception.
 
19
    """
 
20
    def _retry_on_exception_inner_1(f):
 
21
        def _retry_on_exception_inner_2(*args, **kwargs):
 
22
            retries = num_retries
 
23
            multiplier = 1
 
24
            while True:
 
25
                try:
 
26
                    return f(*args, **kwargs)
 
27
                except exc_type:
 
28
                    if not retries:
 
29
                        raise
 
30
 
 
31
                delay = base_delay * multiplier
 
32
                multiplier += 1
 
33
                log("Retrying '%s' %d more times (delay=%s)" %
 
34
                    (f.__name__, retries, delay), level=INFO)
 
35
                retries -= 1
 
36
                if delay:
 
37
                    time.sleep(delay)
 
38
 
 
39
        return _retry_on_exception_inner_2
 
40
 
 
41
    return _retry_on_exception_inner_1