From d37659afd859ec330a0bf0648c0ea67fcf12a6de Mon Sep 17 00:00:00 2001 From: Matt Prahl Date: Tue, 20 Sep 2016 08:49:14 -0400 Subject: [PATCH] Modify message functions to accept conf instead of backend --- rida/messaging.py | 30 +++++++++++++++++++----------- rida/models.py | 4 ++-- rida/scheduler/main.py | 2 +- 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/rida/messaging.py b/rida/messaging.py index 8b54a2d6..fa45ee93 100644 --- a/rida/messaging.py +++ b/rida/messaging.py @@ -146,25 +146,33 @@ class RidaModule(BaseMessage): self.module_build_state = module_build_state -def publish(topic, msg, backend, modname='rida'): - """ Publish a single message to a given backend, and return. """ +def publish(topic, msg, conf, modname='rida'): + """ + Publish a single message to a given backend, and return + :param topic: the topic of the message (e.g. module.state.change) + :param msg: the message contents of the message (typically JSON) + :param conf: a Config object from the class in config.py + :param modname: the system that is publishing the message (e.g. rida) + :return: + """ try: - handler = _messaging_backends[backend]['publish'] + handler = _messaging_backends[conf.messaging]['publish'] except KeyError: - raise KeyError("No messaging backend found for %r" % backend) + raise KeyError("No messaging backend found for %r" % conf.messaging) return handler(topic, msg, modname=modname) -def listen(backend, **kwargs): - """ Yield messages from a given messaging backend. - - The ``**kwargs`` arguments will be passed on to the backend to do some - backend-specific connection handling, throttling, or filtering. +def listen(conf, **kwargs): + """ + Yield messages from the messaging backend in conf.messaging. + :param conf: a Config object from the class in config.py + :param kwargs: any additional arguments to pass to the backend handler + :return: yields a message object (child class from BaseMessage) """ try: - handler = _messaging_backends[backend]['listen'] + handler = _messaging_backends[conf.messaging]['listen'] except KeyError: - raise KeyError("No messaging backend found for %r" % backend) + raise KeyError("No messaging backend found for %r" % conf.messaging) for event in handler(**kwargs): yield event diff --git a/rida/models.py b/rida/models.py index c3fc9558..d1d27cb4 100644 --- a/rida/models.py +++ b/rida/models.py @@ -149,7 +149,7 @@ class ModuleBuild(RidaBase): modname='rida', topic='module.state.change', msg=module.json(), # Note the state is "init" here... - backend=conf.messaging, + conf=conf, ) return module @@ -168,7 +168,7 @@ class ModuleBuild(RidaBase): modname='rida', topic='module.state.change', msg=self.json(), # Note the state is "init" here... - backend=conf.messaging, + conf=conf, ) @classmethod diff --git a/rida/scheduler/main.py b/rida/scheduler/main.py index 27623c30..a3cbd468 100644 --- a/rida/scheduler/main.py +++ b/rida/scheduler/main.py @@ -75,7 +75,7 @@ class MessageIngest(threading.Thread): def run(self): - for msg in rida.messaging.listen(conf.messaging): + for msg in rida.messaging.listen(conf): self.outgoing_work_queue.put(msg)