~mattyw/charms/trusty/mongodb/mongodb-deployu

« back to all changes in this revision

Viewing changes to hooks/charmhelpers/core/decorators.py

  • Committer: Jorge Niedbalski
  • Author(s): Mario Splivalo
  • Date: 2015-01-22 17:09:22 UTC
  • mfrom: (56.1.66 mongodb.replsets-fix-try)
  • Revision ID: jorge.niedbalski@canonical.com-20150122170922-nz94p2e0j4xqbbda
[mariosplivalo, r=niedbalski,freyes] Fixes various replicaset issues bug LP: #1403698, LP: #1370542, LP: #1379604

unit tests Ok, amulet tests OK.

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