rpc: Rework 'get_notifier', 'wrap_exception'

The 'nova.exception_wrapper.wrap_exception' decorator accepted either a
pre-configured notifier or a 'get_notifier' function, but the forget was
never provided and the latter was consistently a notifier created via a
call to 'nova.rpc.get_notifier'. Simplify things by passing the
arguments relied by 'get_notifier' into 'wrap_exception', allowing the
latter to create the former for us.

While doing this rework, it became obvious that 'get_notifier' accepted
a 'published_id' that is never provided nowadays, so that is dropped. In
addition, a number of calls to 'get_notifier' were passing in
'host=CONF.host', which duplicated the default value for this parameter
and is therefore unnecessary. Finally, the unit tests are split up by
file, as they should be.

Change-Id: I89e1c13e8a0df18594593b1e80c60d177e0d9c4c
Signed-off-by: Stephen Finucane <stephenfin@redhat.com>
This commit is contained in:
Stephen Finucane
2020-07-17 16:08:46 +01:00
parent c23bda400a
commit e64744b92f
10 changed files with 183 additions and 190 deletions

View File

@@ -25,14 +25,6 @@ from nova import safe_utils
CONF = nova.conf.CONF
def _emit_exception_notification(
notifier, context, exception, function_name, args, source,
):
_emit_legacy_exception_notification(
notifier, context, exception, function_name, args)
_emit_versioned_exception_notification(context, exception, source)
@rpc.if_notifications_enabled
def _emit_versioned_exception_notification(context, exception, source):
payload = exception_obj.ExceptionPayload.from_exception(exception)
@@ -50,13 +42,15 @@ def _emit_versioned_exception_notification(context, exception, source):
notification.emit(context)
def _emit_legacy_exception_notification(notifier, context, ex, function_name,
args):
payload = dict(exception=ex, args=args)
def _emit_legacy_exception_notification(
context, exception, service, function_name, args,
):
notifier = rpc.get_notifier(service)
payload = {'exception': exception, 'args': args}
notifier.error(context, function_name, payload)
def wrap_exception(notifier=None, get_notifier=None, binary=None):
def wrap_exception(service, binary):
"""This decorator wraps a method to catch any exceptions that may
get thrown. It also optionally sends the exception to the notification
system.
@@ -69,14 +63,13 @@ def wrap_exception(notifier=None, get_notifier=None, binary=None):
return f(self, context, *args, **kw)
except Exception as exc:
with excutils.save_and_reraise_exception():
if notifier or get_notifier:
call_dict = _get_call_dict(
f, self, context, *args, **kw)
function_name = f.__name__
_emit_exception_notification(
notifier or get_notifier(), context, exc,
function_name, call_dict, binary)
call_dict = _get_call_dict(f, self, context, *args, **kw)
function_name = f.__name__
_emit_legacy_exception_notification(
context, exc, service, function_name, call_dict)
_emit_versioned_exception_notification(
context, exc, binary)
return functools.wraps(f)(wrapped)
return inner