Use oslo_utils reflection to get 'f' callable name
Using the utility function gets a better name. For example: $ python >>> from oslo_utils import reflection >>> class A(object): ... def m(self): ... pass ... >>> z = A() >>> reflection.get_callable_name(z.m) '__main__.A.m' Versus: >>> z.m.__name__ 'm' Change-Id: I2eb9f49f6d7578d5d4e3a40cbd695e6622f3f850
This commit is contained in:
parent
5800337edd
commit
9f624f7405
|
@ -21,6 +21,7 @@ import sys
|
||||||
from eventlet import event
|
from eventlet import event
|
||||||
from eventlet import greenthread
|
from eventlet import greenthread
|
||||||
from oslo_utils import excutils
|
from oslo_utils import excutils
|
||||||
|
from oslo_utils import reflection
|
||||||
from oslo_utils import timeutils
|
from oslo_utils import timeutils
|
||||||
import six
|
import six
|
||||||
|
|
||||||
|
@ -46,7 +47,7 @@ class LoopingCallDone(Exception):
|
||||||
self.retvalue = retvalue
|
self.retvalue = retvalue
|
||||||
|
|
||||||
|
|
||||||
def _safe_wrapper(f, kind):
|
def _safe_wrapper(f, kind, func_name):
|
||||||
"""Wrapper that calls into wrapped function and logs errors as needed."""
|
"""Wrapper that calls into wrapped function and logs errors as needed."""
|
||||||
|
|
||||||
def func(*args, **kwargs):
|
def func(*args, **kwargs):
|
||||||
|
@ -56,7 +57,7 @@ def _safe_wrapper(f, kind):
|
||||||
raise # let the outer handler process this
|
raise # let the outer handler process this
|
||||||
except Exception:
|
except Exception:
|
||||||
LOG.error(_LE('%(kind)s %(func_name)r failed'),
|
LOG.error(_LE('%(kind)s %(func_name)r failed'),
|
||||||
{'kind': kind, 'func_name': f},
|
{'kind': kind, 'func_name': func_name},
|
||||||
exc_info=True)
|
exc_info=True)
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
@ -100,7 +101,9 @@ class LoopingCallBase(object):
|
||||||
|
|
||||||
def _run_loop(self, kind, event, idle_for_func,
|
def _run_loop(self, kind, event, idle_for_func,
|
||||||
initial_delay=None, stop_on_exception=True):
|
initial_delay=None, stop_on_exception=True):
|
||||||
func = self.f if stop_on_exception else _safe_wrapper(self.f, kind)
|
func_name = reflection.get_callable_name(self.f)
|
||||||
|
func = self.f if stop_on_exception else _safe_wrapper(self.f, kind,
|
||||||
|
func_name)
|
||||||
if initial_delay:
|
if initial_delay:
|
||||||
greenthread.sleep(initial_delay)
|
greenthread.sleep(initial_delay)
|
||||||
try:
|
try:
|
||||||
|
@ -114,7 +117,7 @@ class LoopingCallBase(object):
|
||||||
idle = idle_for_func(result, watch.elapsed())
|
idle = idle_for_func(result, watch.elapsed())
|
||||||
LOG.debug('%(kind)s %(func_name)r sleeping '
|
LOG.debug('%(kind)s %(func_name)r sleeping '
|
||||||
'for %(idle).02f seconds',
|
'for %(idle).02f seconds',
|
||||||
{'func_name': self.f, 'idle': idle,
|
{'func_name': func_name, 'idle': idle,
|
||||||
'kind': kind})
|
'kind': kind})
|
||||||
greenthread.sleep(idle)
|
greenthread.sleep(idle)
|
||||||
except LoopingCallDone as e:
|
except LoopingCallDone as e:
|
||||||
|
@ -123,7 +126,7 @@ class LoopingCallBase(object):
|
||||||
exc_info = sys.exc_info()
|
exc_info = sys.exc_info()
|
||||||
try:
|
try:
|
||||||
LOG.error(_LE('%(kind)s %(func_name)r failed'),
|
LOG.error(_LE('%(kind)s %(func_name)r failed'),
|
||||||
{'kind': kind, 'func_name': self.f},
|
{'kind': kind, 'func_name': func_name},
|
||||||
exc_info=exc_info)
|
exc_info=exc_info)
|
||||||
event.send_exception(*exc_info)
|
event.send_exception(*exc_info)
|
||||||
finally:
|
finally:
|
||||||
|
@ -145,9 +148,10 @@ class FixedIntervalLoopingCall(LoopingCallBase):
|
||||||
def _idle_for(result, elapsed):
|
def _idle_for(result, elapsed):
|
||||||
delay = elapsed - interval
|
delay = elapsed - interval
|
||||||
if delay > 0:
|
if delay > 0:
|
||||||
|
func_name = reflection.get_callable_name(self.f)
|
||||||
LOG.warning(_LW('Function %(func_name)r run outlasted '
|
LOG.warning(_LW('Function %(func_name)r run outlasted '
|
||||||
'interval by %(delay).2f sec'),
|
'interval by %(delay).2f sec'),
|
||||||
{'func_name': self.f, 'delay': delay})
|
{'func_name': func_name, 'delay': delay})
|
||||||
return -delay if delay < 0 else 0
|
return -delay if delay < 0 else 0
|
||||||
return self._start(_idle_for, initial_delay=initial_delay,
|
return self._start(_idle_for, initial_delay=initial_delay,
|
||||||
stop_on_exception=stop_on_exception)
|
stop_on_exception=stop_on_exception)
|
||||||
|
@ -215,9 +219,9 @@ class RetryDecorator(object):
|
||||||
self._sleep_time = 0
|
self._sleep_time = 0
|
||||||
|
|
||||||
def __call__(self, f):
|
def __call__(self, f):
|
||||||
|
func_name = reflection.get_callable_name(f)
|
||||||
|
|
||||||
def _func(*args, **kwargs):
|
def _func(*args, **kwargs):
|
||||||
func_name = f.__name__
|
|
||||||
result = None
|
result = None
|
||||||
try:
|
try:
|
||||||
if self._retry_count:
|
if self._retry_count:
|
||||||
|
@ -235,12 +239,14 @@ class RetryDecorator(object):
|
||||||
exc_info=True)
|
exc_info=True)
|
||||||
if (self._max_retry_count != -1 and
|
if (self._max_retry_count != -1 and
|
||||||
self._retry_count >= self._max_retry_count):
|
self._retry_count >= self._max_retry_count):
|
||||||
LOG.error(_LE("Cannot retry upon suggested exception "
|
LOG.error(_LE("Cannot retry %(func_name)s upon "
|
||||||
|
"suggested exception "
|
||||||
"since retry count (%(retry_count)d) "
|
"since retry count (%(retry_count)d) "
|
||||||
"reached max retry count "
|
"reached max retry count "
|
||||||
"(%(max_retry_count)d)."),
|
"(%(max_retry_count)d)."),
|
||||||
{'retry_count': self._retry_count,
|
{'retry_count': self._retry_count,
|
||||||
'max_retry_count': self._max_retry_count})
|
'max_retry_count': self._max_retry_count,
|
||||||
|
'func_name': func_name})
|
||||||
else:
|
else:
|
||||||
ctxt.reraise = False
|
ctxt.reraise = False
|
||||||
self._retry_count += 1
|
self._retry_count += 1
|
||||||
|
@ -252,7 +258,7 @@ class RetryDecorator(object):
|
||||||
def func(*args, **kwargs):
|
def func(*args, **kwargs):
|
||||||
loop = DynamicLoopingCall(_func, *args, **kwargs)
|
loop = DynamicLoopingCall(_func, *args, **kwargs)
|
||||||
evt = loop.start(periodic_interval_max=self._max_sleep_time)
|
evt = loop.start(periodic_interval_max=self._max_sleep_time)
|
||||||
LOG.debug("Waiting for function %s to return.", f.__name__)
|
LOG.debug("Waiting for function %s to return.", func_name)
|
||||||
return evt.wait()
|
return evt.wait()
|
||||||
|
|
||||||
return func
|
return func
|
||||||
|
|
Loading…
Reference in New Issue