~allanlesage/qakit/qakit

« back to all changes in this revision

Viewing changes to qakit/practitest/util.py

  • Committer: Allan LeSage
  • Date: 2015-04-15 19:08:20 UTC
  • mfrom: (5.1.1 qakit)
  • Revision ID: allan.lesage@canonical.com-20150415190820-lkup7u36wtaagrf5
Merge practitest scripts, work in progress.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
# PractiSanity
 
3
# Copyright (C) 2015 Canonical
 
4
#
 
5
# This program is free software: you can redistribute it and/or modify
 
6
# it under the terms of the GNU General Public License as published by
 
7
# the Free Software Foundation, either version 3 of the License, or
 
8
# (at your option) any later version.
 
9
#
 
10
# This program is distributed in the hope that it will be useful,
 
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
# GNU General Public License for more details.
 
14
#
 
15
# You should have received a copy of the GNU General Public License
 
16
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
17
 
 
18
import functools
 
19
import sys
 
20
import time
 
21
 
 
22
import logging
 
23
 
 
24
 
 
25
logger = logging.getLogger('PractiSanity')
 
26
logger.setLevel(logging.INFO)
 
27
stdout_handler = logging.StreamHandler(sys.stdout)
 
28
stdout_handler.setLevel(logging.DEBUG)
 
29
logger.addHandler(stdout_handler)
 
30
 
 
31
 
 
32
def retry(ExceptionToCheck, tries=4, delay=2, backoff=2, logger=None):
 
33
    """Retry calling the decorated function using an exponential backoff.
 
34
 
 
35
    http://www.saltycrane.com/blog/2009/11/trying-out-retry-decorator-python/
 
36
    Original from: http://wiki.python.org/moin/PythonDecoratorLibrary#Retry
 
37
 
 
38
    :param ExceptionToCheck: the exception to check. may be a tuple of
 
39
        exceptions to check
 
40
    :param tries: number of times to try (not retry) before giving up
 
41
    :param delay: initial delay between retries in seconds
 
42
    :param backoff: backoff multiplier e.g. value of 2 will double the delay
 
43
        each retry
 
44
    :param logger: logger.Logger to use.
 
45
 
 
46
    """
 
47
    def deco_retry(f):
 
48
        @functools.wraps(f)
 
49
        def f_retry(*args, **kwargs):
 
50
            for i in range(tries):
 
51
                try:
 
52
                    return f(*args, **kwargs)
 
53
                except ExceptionToCheck as e:
 
54
                    d = delay * backoff ** i
 
55
                    if logger:
 
56
                        logger.debug(
 
57
                            "%s, retrying in %d seconds. . . ." % (e, d))
 
58
                    time.sleep(d)
 
59
            return f(*args, **kwargs)
 
60
        return f_retry  # true decorator
 
61
    return deco_retry
 
62
 
 
63
 
 
64
def queue_to_list(q):
 
65
    results = []
 
66
    while not q.empty():
 
67
        results.append(q.get())
 
68
    return results