blob: c7bce727383948e1524f5340c8f7118e3fedeb3c [file] [log] [blame]
Matthew Treinish0db53772013-07-26 10:39:35 -04001# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
3# Copyright 2011 OpenStack Foundation.
4# Copyright 2012, Red Hat, Inc.
5#
6# Licensed under the Apache License, Version 2.0 (the "License"); you may
7# not use this file except in compliance with the License. You may obtain
8# a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15# License for the specific language governing permissions and limitations
16# under the License.
17
18"""
19Exception related utilities.
20"""
21
22import logging
23import sys
24import time
25import traceback
26
Matthew Treinishffa94d62013-09-11 18:09:17 +000027import six
28
Matthew Treinish0db53772013-07-26 10:39:35 -040029from tempest.openstack.common.gettextutils import _ # noqa
30
31
32class save_and_reraise_exception(object):
33 """Save current exception, run some code and then re-raise.
34
35 In some cases the exception context can be cleared, resulting in None
36 being attempted to be re-raised after an exception handler is run. This
37 can happen when eventlet switches greenthreads or when running an
38 exception handler, code raises and catches an exception. In both
39 cases the exception context will be cleared.
40
41 To work around this, we save the exception state, run handler code, and
42 then re-raise the original exception. If another exception occurs, the
43 saved exception is logged and the new exception is re-raised.
44
45 In some cases the caller may not want to re-raise the exception, and
46 for those circumstances this context provides a reraise flag that
47 can be used to suppress the exception. For example:
48
49 except Exception:
50 with save_and_reraise_exception() as ctxt:
51 decide_if_need_reraise()
52 if not should_be_reraised:
53 ctxt.reraise = False
54 """
55 def __init__(self):
56 self.reraise = True
57
58 def __enter__(self):
59 self.type_, self.value, self.tb, = sys.exc_info()
60 return self
61
62 def __exit__(self, exc_type, exc_val, exc_tb):
63 if exc_type is not None:
64 logging.error(_('Original exception being dropped: %s'),
65 traceback.format_exception(self.type_,
66 self.value,
67 self.tb))
68 return False
69 if self.reraise:
Matthew Treinishffa94d62013-09-11 18:09:17 +000070 six.reraise(self.type_, self.value, self.tb)
Matthew Treinish0db53772013-07-26 10:39:35 -040071
72
73def forever_retry_uncaught_exceptions(infunc):
74 def inner_func(*args, **kwargs):
75 last_log_time = 0
76 last_exc_message = None
77 exc_count = 0
78 while True:
79 try:
80 return infunc(*args, **kwargs)
81 except Exception as exc:
Matthew Treinishf45528a2013-10-24 20:12:28 +000082 this_exc_message = six.u(str(exc))
Matthew Treinishffa94d62013-09-11 18:09:17 +000083 if this_exc_message == last_exc_message:
Matthew Treinish0db53772013-07-26 10:39:35 -040084 exc_count += 1
85 else:
86 exc_count = 1
87 # Do not log any more frequently than once a minute unless
88 # the exception message changes
89 cur_time = int(time.time())
90 if (cur_time - last_log_time > 60 or
Matthew Treinishffa94d62013-09-11 18:09:17 +000091 this_exc_message != last_exc_message):
Matthew Treinish0db53772013-07-26 10:39:35 -040092 logging.exception(
93 _('Unexpected exception occurred %d time(s)... '
94 'retrying.') % exc_count)
95 last_log_time = cur_time
Matthew Treinishffa94d62013-09-11 18:09:17 +000096 last_exc_message = this_exc_message
Matthew Treinish0db53772013-07-26 10:39:35 -040097 exc_count = 0
98 # This should be a very rare event. In case it isn't, do
99 # a sleep.
100 time.sleep(1)
101 return inner_func