deb-mistral/mistral/messaging.py
Lingxian Kong 2f3d897312 Add event engine service
This patch adds a new optional service in Mistral, event engine
service will listen to the message queue with exchange and topic users
configed, and trigger the corresponding workflow via rpc call to
mistral engine. The event engine service will receive rpc messages
from mistral api

Change-Id: I8bef24822575670ca90ef0479b9ce2e3e330cdeb
Implements: blueprint event-notification-trigger
Co-Authored-By: Lingxian Kong <anlin.kong@gmail.com>
2016-07-22 23:19:16 +12:00

113 lines
3.4 KiB
Python

# Copyright 2016 - IBM Corp.
# Copyright 2016 Catalyst IT Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
This module contains common structures and functions that help to handle
AMQP messages based on olso.messaging framework.
"""
import abc
import socket
from oslo_log import log as logging
import oslo_messaging
from oslo_messaging.notify import dispatcher
from oslo_messaging.notify import listener
from oslo_messaging import target
from oslo_messaging import transport
import six
LOG = logging.getLogger(__name__)
def handle_event(self, ctxt, publisher_id, event_type, payload, metadata):
"""Callback function of each priority of notification messages.
The function is used to construct endpoint class dynamically when starting
listener in event engine service.
After the class is created, 'self' param will make sense.
:param ctxt: the notification context dict
:param publisher_id: always describes where notification is sent from, for
example: 'compute.host1'
:param event_type: describes the event, for example:
'compute.create_instance'
:param payload: the notification payload
:param metadata: the notification metadata, is always a mapping containing
a unique message_id and a timestamp.
"""
LOG.debug('Received notification. publisher_id: %s, event_type: %s, '
'payload: %s, metadata: %s.', publisher_id, event_type, payload,
metadata)
self.event_engine.process_notification_event(
ctxt,
event_type,
payload,
metadata
)
return dispatcher.NotificationResult.HANDLED
@six.add_metaclass(abc.ABCMeta)
class NotificationEndpoint(object):
"""Message listener endpoint.
Only handle notifications that match the NotificationFilter rule set into
the filter_rule attribute of the endpoint.
"""
event_types = []
def __init__(self, event_engine):
self.event_engine = event_engine
self.filter_rule = oslo_messaging.NotificationFilter(
event_type='|'.join(self.event_types))
def get_pool_name(exchange):
"""Get pool name.
Get the pool name for the listener, it will be formatted as
'mistral-exchange-hostname'
:param exchange: exchange name
"""
pool_name = 'mistral-%s-%s' % (exchange, socket.gethostname())
LOG.debug("Listener pool name is %s", pool_name)
return pool_name
def start_listener(conf, exchange, topic, endpoints):
"""Starts up a notification listener."""
trans = transport.get_transport(conf)
targets = [target.Target(exchange=exchange, topic=topic)]
pool_name = get_pool_name(exchange)
notification_listener = listener.get_notification_listener(
trans,
targets,
endpoints,
executor='threading',
allow_requeue=False,
pool=pool_name
)
notification_listener.start()
return notification_listener