Skip to content

Instantly share code, notes, and snippets.

@zhanghui9700
Created March 13, 2017 02:46
Show Gist options
  • Save zhanghui9700/08c12f94bb391ad3928e9c2cd9d0192c to your computer and use it in GitHub Desktop.
Save zhanghui9700/08c12f94bb391ad3928e9c2cd9d0192c to your computer and use it in GitHub Desktop.

nova _notify_about_instance_usag

@zhanghui9700
Copy link
Author

nova notify

config

[default]
notification_driver=messagingv2

[oslo_messaging_notifications]

#
# From oslo.messaging
#

# The Drivers(s) to handle sending notifications. Possible values are
# messaging, messagingv2, routing, log, test, noop (multi valued)
# Deprecated group/name - [DEFAULT]/notification_driver

# notify by 4 drivers(3 below definiation + default notification_driver)
driver = log
driver = test
driver = messaging

# A URL representing the messaging driver to use for notifications. If not set,
# we fall back to the same configuration used for RPC. (string value)
# Deprecated group/name - [DEFAULT]/notification_transport_url
#transport_url = <None>

# AMQP topic used for OpenStack notifications. (list value)
# Deprecated group/name - [rpc_notifier2]/topics
# Deprecated group/name - [DEFAULT]/notification_topics

# queue: xcloud.info|error|warning
topics = notifications, xcloud

code

# compute/manager.py
self._notify_about_instance_usage(context, instance, 'resume.start')
self._notify_about_instance_usage(context, instance, 'resume.end')

# self.notifier = rpc.get_notifier("compute", self.host)

def _notify_about_instance_usage(self, context, instance, event_suffix,
                                 network_info=None, system_metadata=None,
                                 extra_usage_info=None, fault=None):
    compute_utils.notify_about_instance_usage(
            self.notifier, context, instance, event_suffix,
            network_info=network_info,
            system_metadata=system_metadata,
            extra_usage_info=extra_usage_info, fault=fault)

# compute/utils.py
def notify_about_instance_usage(notifier, context, instance, event_suffix,
                            network_info=None, system_metadata=None,
                            extra_usage_info=None, fault=None):
    """Send a notification about an instance.

    :param notifier: a messaging.Notifier
    :param event_suffix: Event type like "delete.start" or "exists"
    :param network_info: Networking information, if provided.
    :param system_metadata: system_metadata DB entries for the instance,
        if provided.
    :param extra_usage_info: Dictionary containing extra values to add or
        override in the notification.
    """
    if not extra_usage_info:
        extra_usage_info = {}

    usage_info = notifications.info_from_instance(context, instance,
            network_info, system_metadata, **extra_usage_info)

    if fault:
        # NOTE(johngarbutt) mirrors the format in wrap_exception
        fault_payload = exception_to_dict(fault)
        LOG.debug(fault_payload["message"], instance=instance)
        usage_info.update(fault_payload)

    if event_suffix.endswith("error"):
        method = notifier.error
    else:
        method = notifier.info

    method(context, 'compute.instance.%s' % event_suffix, usage_info)

extend

use stevendor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment