def fedmsg_publish(*args, **kwargs): # pragma: no cover """ Try to publish a message on the fedmsg bus. """ if config.config["LEGACY_MESSAGING"]: # We catch Exception if we want :-p # pylint: disable=W0703 # Ignore message about fedmsg import # pylint: disable=F0401 kwargs["modname"] = "anitya" kwargs["cert_prefix"] = "anitya" kwargs["name"] = "relay_inbound" kwargs["active"] = True try: import fedmsg fedmsg.publish(*args, **kwargs) except Exception as err: _log.error(str(err)) else: try: message_class = message.get_class("anitya." + kwargs["topic"]) api.publish( message_class( topic="anitya.{}".format(kwargs["topic"]), body=kwargs["msg"] ) ) except ( fm_exceptions.ConnectionException, fm_exceptions.PublishException, ) as err: # For now, continue just logging the error. Once the messaging has # been untangled into SQLAlchemy events, it should probably result # in an exception and the client should try again later. _log.error(str(err))
def publish_message(topic, project=None, distro=None, message=None): # pragma: no cover """Try to publish a message. Args: topic (str): Topic of the message project (dict): Dictionary representing project distro (str): Name of the distribution message (dict): Additional data needed for the topic """ msg = dict(project=project, distro=distro, message=message) try: message_class = fm_message.get_class("anitya." + topic) api.publish(message_class(topic="anitya.{}".format(topic), body=msg)) except ( fm_exceptions.ConnectionException, fm_exceptions.PublishException, ) as err: # For now, continue just logging the error. Once the messaging has # been untangled into SQLAlchemy events, it should probably result # in an exception and the client should try again later. _log.error(str(err))
def notify(self, package: Package, message: str, opts: dict) -> dict: """ This method is inherited from `hotness.notifiers.Notifier`. It publishes messages to Fedora messaging broker. Params: package: Package to create notification for message: Topic for the message opts: Additional options for fedora message. Example: { "body": {} # Body of the message we want to sent. Look at the # hotness_schema for more info } Returns: Dictionary containing message id Example: { "msg_id": "ae68f" } Raises: NotifierException: When the required `opts` parameters are missing or error happens during publishing. """ topic = self.prefix + "." + message body = opts.get("body", {}) output = {} if not body: raise NotifierException( "Additional parameters are missing! Please provide `body` for the message." ) message_class = fm_message.get_class(topic) if not message_class: raise NotifierException("Unknown topic provided '{}'".format(topic)) _logger.info("publishing topic %r" % topic) try: msg = message_class(topic=topic, body=body) api.publish(msg) except PublishException as e: raise NotifierException( "Fedora messaging broker rejected message {}:{}".format(msg.id, e) ) except ConnectionException as e: raise NotifierException("Error sending the message {}:{}".format(msg.id, e)) finally: output["msg_id"] = msg.id return output
def test_get_class_default(self): """Assert the base class is returns if the class is unknown.""" with mock.patch.dict(message._schema_name_to_class, {}, clear=True): self.assertEqual(message.get_class("no.such.message"), message.Message)
def test_get_class_autoload(self): """Assert the registry is automatically loaded.""" with mock.patch.dict(message._schema_name_to_class, {}, clear=True): self.assertEqual(message.get_class("base.message"), message.Message)
def test_get_class_autoload_first_call(self): """Assert the registry loads classes on first call to get_class.""" with mock.patch.dict(message._class_registry, {}, clear=True): self.assertEqual( message.get_class('fedora_messaging.message:Message'), message.Message)
def test_get_class_autoload(self): """Assert the registry is automatically loaded.""" with mock.patch.dict(message._class_registry, {}, clear=True): self.assertEqual( message.get_class('fedora_messaging.message:Message'), message.Message)