~ubuntu-branches/ubuntu/trusty/heat/trusty-security

« back to all changes in this revision

Viewing changes to heat/openstack/common/excutils.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2013-09-08 21:51:19 UTC
  • mto: This revision was merged to the branch mainline in revision 14.
  • Revision ID: package-import@ubuntu.com-20130908215119-7tcek6gn73275x5k
Tags: upstream-2013.2~b3
ImportĀ upstreamĀ versionĀ 2013.2~b3

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
Exception related utilities.
20
20
"""
21
21
 
22
 
import contextlib
23
22
import logging
24
23
import sys
 
24
import time
25
25
import traceback
26
26
 
27
 
from heat.openstack.common.gettextutils import _
28
 
 
29
 
 
30
 
@contextlib.contextmanager
31
 
def save_and_reraise_exception():
 
27
from heat.openstack.common.gettextutils import _  # noqa
 
28
 
 
29
 
 
30
class save_and_reraise_exception(object):
32
31
    """Save current exception, run some code and then re-raise.
33
32
 
34
33
    In some cases the exception context can be cleared, resulting in None
40
39
    To work around this, we save the exception state, run handler code, and
41
40
    then re-raise the original exception. If another exception occurs, the
42
41
    saved exception is logged and the new exception is re-raised.
 
42
 
 
43
    In some cases the caller may not want to re-raise the exception, and
 
44
    for those circumstances this context provides a reraise flag that
 
45
    can be used to suppress the exception.  For example:
 
46
 
 
47
    except Exception:
 
48
        with save_and_reraise_exception() as ctxt:
 
49
            decide_if_need_reraise()
 
50
            if not should_be_reraised:
 
51
                ctxt.reraise = False
43
52
    """
44
 
    type_, value, tb = sys.exc_info()
45
 
    try:
46
 
        yield
47
 
    except Exception:
48
 
        logging.error(_('Original exception being dropped: %s'),
49
 
                      traceback.format_exception(type_, value, tb))
50
 
        raise
51
 
    raise type_, value, tb
 
53
    def __init__(self):
 
54
        self.reraise = True
 
55
 
 
56
    def __enter__(self):
 
57
        self.type_, self.value, self.tb, = sys.exc_info()
 
58
        return self
 
59
 
 
60
    def __exit__(self, exc_type, exc_val, exc_tb):
 
61
        if exc_type is not None:
 
62
            logging.error(_('Original exception being dropped: %s'),
 
63
                          traceback.format_exception(self.type_,
 
64
                                                     self.value,
 
65
                                                     self.tb))
 
66
            return False
 
67
        if self.reraise:
 
68
            raise self.type_, self.value, self.tb
 
69
 
 
70
 
 
71
def forever_retry_uncaught_exceptions(infunc):
 
72
    def inner_func(*args, **kwargs):
 
73
        last_log_time = 0
 
74
        last_exc_message = None
 
75
        exc_count = 0
 
76
        while True:
 
77
            try:
 
78
                return infunc(*args, **kwargs)
 
79
            except Exception as exc:
 
80
                if exc.message == last_exc_message:
 
81
                    exc_count += 1
 
82
                else:
 
83
                    exc_count = 1
 
84
                # Do not log any more frequently than once a minute unless
 
85
                # the exception message changes
 
86
                cur_time = int(time.time())
 
87
                if (cur_time - last_log_time > 60 or
 
88
                        exc.message != last_exc_message):
 
89
                    logging.exception(
 
90
                        _('Unexpected exception occurred %d time(s)... '
 
91
                          'retrying.') % exc_count)
 
92
                    last_log_time = cur_time
 
93
                    last_exc_message = exc.message
 
94
                    exc_count = 0
 
95
                # This should be a very rare event. In case it isn't, do
 
96
                # a sleep.
 
97
                time.sleep(1)
 
98
    return inner_func