~josvaz/charms/trusty/bip/client_side_ssl-with_helper-lp1604894

« back to all changes in this revision

Viewing changes to lib/charm-helpers/charmhelpers/core/decorators.py

  • Committer: Jose Vazquez
  • Date: 2016-07-29 12:54:32 UTC
  • Revision ID: jose.vazquez@canonical.com-20160729125432-7gmm2tpyicju5jl4
Result of removing charmhelpers and re-syncing it

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2014-2015 Canonical Limited.
 
2
#
 
3
# Licensed under the Apache License, Version 2.0 (the "License");
 
4
# you may not use this file except in compliance with the License.
 
5
# You may obtain a copy of the License at
 
6
#
 
7
#  http://www.apache.org/licenses/LICENSE-2.0
 
8
#
 
9
# Unless required by applicable law or agreed to in writing, software
 
10
# distributed under the License is distributed on an "AS IS" BASIS,
 
11
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
12
# See the License for the specific language governing permissions and
 
13
# limitations under the License.
 
14
 
 
15
#
 
16
# Copyright 2014 Canonical Ltd.
 
17
#
 
18
# Authors:
 
19
#  Edward Hope-Morley <opentastic@gmail.com>
 
20
#
 
21
 
 
22
import time
 
23
 
 
24
from charmhelpers.core.hookenv import (
 
25
    log,
 
26
    INFO,
 
27
)
 
28
 
 
29
 
 
30
def retry_on_exception(num_retries, base_delay=0, exc_type=Exception):
 
31
    """If the decorated function raises exception exc_type, allow num_retries
 
32
    retry attempts before raise the exception.
 
33
    """
 
34
    def _retry_on_exception_inner_1(f):
 
35
        def _retry_on_exception_inner_2(*args, **kwargs):
 
36
            retries = num_retries
 
37
            multiplier = 1
 
38
            while True:
 
39
                try:
 
40
                    return f(*args, **kwargs)
 
41
                except exc_type:
 
42
                    if not retries:
 
43
                        raise
 
44
 
 
45
                delay = base_delay * multiplier
 
46
                multiplier += 1
 
47
                log("Retrying '%s' %d more times (delay=%s)" %
 
48
                    (f.__name__, retries, delay), level=INFO)
 
49
                retries -= 1
 
50
                if delay:
 
51
                    time.sleep(delay)
 
52
 
 
53
        return _retry_on_exception_inner_2
 
54
 
 
55
    return _retry_on_exception_inner_1