From 9d3b85426d5e1e3193488851fced80f3019a1f6e Mon Sep 17 00:00:00 2001 From: Ralph Bean Date: Thu, 15 Dec 2016 13:13:05 -0500 Subject: [PATCH] Bring back the in-memory messaging backend. --- conf/config.py | 1 + module_build_service/config.py | 2 +- module_build_service/messaging.py | 39 +++++++++++++++++++++- module_build_service/scheduler/consumer.py | 8 ++++- 4 files changed, 47 insertions(+), 3 deletions(-) diff --git a/conf/config.py b/conf/config.py index 5b3e39a0..825dce34 100644 --- a/conf/config.py +++ b/conf/config.py @@ -134,6 +134,7 @@ class TestConfiguration(BaseConfiguration): SQLALCHEMY_DATABASE_URI = 'sqlite:///{0}'.format( path.join(dbdir, 'tests', 'test_module_build_service.db')) DEBUG = True + MESSAGING = 'memory' # Global network-related values, in seconds NET_TIMEOUT = 3 diff --git a/module_build_service/config.py b/module_build_service/config.py index c265234b..aca60a8d 100644 --- a/module_build_service/config.py +++ b/module_build_service/config.py @@ -344,7 +344,7 @@ class Config(object): def _setifok_messaging(self, s): s = str(s) - if s not in ("fedmsg", "amq"): + if s not in ("fedmsg", "amq", "memory"): raise ValueError("Unsupported messaging system.") self.messaging = s diff --git a/module_build_service/messaging.py b/module_build_service/messaging.py index 98e25fc8..9f991ec1 100644 --- a/module_build_service/messaging.py +++ b/module_build_service/messaging.py @@ -33,7 +33,7 @@ except ImportError: from funcsigs import signature from module_build_service import log -import six.moves.queue as queue + class BaseMessage(object): def __init__(self, msg_id): @@ -248,6 +248,40 @@ def _fedmsg_publish(topic, msg, conf, service): import fedmsg return fedmsg.publish(topic, msg=msg, modname=service) + +# A counter used for in-memory messages. +_in_memory_msg_id = 0 +_initial_messages = [] + + +def _in_memory_publish(topic, msg, conf, service): + """ Puts the message into the in memory work queue. """ + # Increment the message ID. + global _in_memory_msg_id + _in_memory_msg_id += 1 + + # Create fake fedmsg from the message so we can reuse + # the BaseMessage.from_fedmsg code to get the particular BaseMessage + # class instance. + wrapped_msg = BaseMessage.from_fedmsg( + service + "." + topic, + {"msg_id": str(_in_memory_msg_id), "msg": msg}, + ) + + # Put the message to queue. + from module_build_service.scheduler.consumer import work_queue_put + try: + work_queue_put(wrapped_msg) + except ValueError as e: + log.warn("No MBSConsumer found. Shutting down? %r" % e) + except AttributeError as e: + # In the event that `moksha.hub._hub` hasn't yet been initialized, we + # need to store messages on the side until it becomes available. + # As a last-ditch effort, try to hang initial messages in the config. + log.warn("Hub not initialized. Queueing on the side.") + _initial_messages.append(wrapped_msg) + + def _amq_get_messenger(conf): import proton for attr in ('amq_private_key_file', 'amq_trusted_cert_file', 'amq_cert_file'): @@ -296,5 +330,8 @@ _messaging_backends = { }, 'amq': { 'publish': _amq_publish, + }, + 'memory': { + 'publish': _in_memory_publish, } } diff --git a/module_build_service/scheduler/consumer.py b/module_build_service/scheduler/consumer.py index 3c0bffb8..27fd0f29 100644 --- a/module_build_service/scheduler/consumer.py +++ b/module_build_service/scheduler/consumer.py @@ -55,6 +55,12 @@ class MBSConsumer(fedmsg.consumers.FedmsgConsumer): for msg in initial_messages: self.incoming.put(msg) + # Furthermore, extend our initial messages with any that were queued up + # in the test environment before our hub was initialized. + while module_build_service.messaging._initial_messages: + msg = module_build_service.messaging._initial_messages.pop() + self.incoming.put(msg) + # These are our main lookup tables for figuring out what to run in # response to what messaging events. self.NO_OP = NO_OP = lambda config, session, msg: True @@ -191,7 +197,7 @@ def get_global_consumer(): if isinstance(consumer, MBSConsumer): return consumer - raise ValueError("No MBSConsumer found.") + raise ValueError("No MBSConsumer found among %r." % len(hub.consumers)) def work_queue_put(msg):