~ubuntu-branches/ubuntu/vivid/ironic/vivid-updates

« back to all changes in this revision

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

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2015-03-30 11:14:57 UTC
  • mfrom: (1.2.6)
  • Revision ID: package-import@ubuntu.com-20150330111457-kr4ju3guf22m4vbz
Tags: 2015.1~b3-0ubuntu1
* New upstream release.
  + d/control: 
    - Align with upstream dependencies.
    - Add dh-python to build-dependencies.
    - Add psmisc as a dependency. (LP: #1358820)
  + d/p/fix-requirements.patch: Rediffed.
  + d/ironic-conductor.init.in: Fixed typos in LSB headers,
    thanks to JJ Asghar. (LP: #1429962)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright 2011 OpenStack Foundation.
2
 
# Copyright 2012, Red Hat, Inc.
3
 
#
4
 
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
5
 
#    not use this file except in compliance with the License. You may obtain
6
 
#    a copy of the License at
7
 
#
8
 
#         http://www.apache.org/licenses/LICENSE-2.0
9
 
#
10
 
#    Unless required by applicable law or agreed to in writing, software
11
 
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
 
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
 
#    License for the specific language governing permissions and limitations
14
 
#    under the License.
15
 
 
16
 
"""
17
 
Exception related utilities.
18
 
"""
19
 
 
20
 
import logging
21
 
import sys
22
 
import time
23
 
import traceback
24
 
 
25
 
import six
26
 
 
27
 
from ironic.openstack.common.gettextutils import _LE
28
 
 
29
 
 
30
 
class save_and_reraise_exception(object):
31
 
    """Save current exception, run some code and then re-raise.
32
 
 
33
 
    In some cases the exception context can be cleared, resulting in None
34
 
    being attempted to be re-raised after an exception handler is run. This
35
 
    can happen when eventlet switches greenthreads or when running an
36
 
    exception handler, code raises and catches an exception. In both
37
 
    cases the exception context will be cleared.
38
 
 
39
 
    To work around this, we save the exception state, run handler code, and
40
 
    then re-raise the original exception. If another exception occurs, the
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
52
 
 
53
 
    If another exception occurs and reraise flag is False,
54
 
    the saved exception will not be logged.
55
 
 
56
 
    If the caller wants to raise new exception during exception handling
57
 
    he/she sets reraise to False initially with an ability to set it back to
58
 
    True if needed::
59
 
 
60
 
      except Exception:
61
 
          with save_and_reraise_exception(reraise=False) as ctxt:
62
 
              [if statements to determine whether to raise a new exception]
63
 
              # Not raising a new exception, so reraise
64
 
              ctxt.reraise = True
65
 
    """
66
 
    def __init__(self, reraise=True):
67
 
        self.reraise = reraise
68
 
 
69
 
    def __enter__(self):
70
 
        self.type_, self.value, self.tb, = sys.exc_info()
71
 
        return self
72
 
 
73
 
    def __exit__(self, exc_type, exc_val, exc_tb):
74
 
        if exc_type is not None:
75
 
            if self.reraise:
76
 
                logging.error(_LE('Original exception being dropped: %s'),
77
 
                              traceback.format_exception(self.type_,
78
 
                                                         self.value,
79
 
                                                         self.tb))
80
 
            return False
81
 
        if self.reraise:
82
 
            six.reraise(self.type_, self.value, self.tb)
83
 
 
84
 
 
85
 
def forever_retry_uncaught_exceptions(infunc):
86
 
    def inner_func(*args, **kwargs):
87
 
        last_log_time = 0
88
 
        last_exc_message = None
89
 
        exc_count = 0
90
 
        while True:
91
 
            try:
92
 
                return infunc(*args, **kwargs)
93
 
            except Exception as exc:
94
 
                this_exc_message = six.u(str(exc))
95
 
                if this_exc_message == last_exc_message:
96
 
                    exc_count += 1
97
 
                else:
98
 
                    exc_count = 1
99
 
                # Do not log any more frequently than once a minute unless
100
 
                # the exception message changes
101
 
                cur_time = int(time.time())
102
 
                if (cur_time - last_log_time > 60 or
103
 
                        this_exc_message != last_exc_message):
104
 
                    logging.exception(
105
 
                        _LE('Unexpected exception occurred %d time(s)... '
106
 
                            'retrying.') % exc_count)
107
 
                    last_log_time = cur_time
108
 
                    last_exc_message = this_exc_message
109
 
                    exc_count = 0
110
 
                # This should be a very rare event. In case it isn't, do
111
 
                # a sleep.
112
 
                time.sleep(1)
113
 
    return inner_func