From 72f8442714b0092061d7fdda953271e16f66f84d Mon Sep 17 00:00:00 2001
From: Karsten Hopp <karsten@redhat.com>
Date: Tue, 12 Nov 2019 14:47:22 +0100
Subject: [PATCH 14/14] add exception handling for fedora-messaging callbacks

Signed-off-by: Karsten Hopp <karsten@redhat.com>
---
 callback_plugins/fedora_messaging_callback.py | 50 ++++++++++++-------
 .../fedora_messaging_callback2.py             | 50 ++++++++++++-------
 2 files changed, 66 insertions(+), 34 deletions(-)

diff --git a/callback_plugins/fedora_messaging_callback.py b/callback_plugins/fedora_messaging_callback.py
index 31e918dde..372080212 100644
--- a/callback_plugins/fedora_messaging_callback.py
+++ b/callback_plugins/fedora_messaging_callback.py
@@ -19,6 +19,7 @@
 
 import os
 import pwd
+import logging
 
 from fedora_messaging.api import Message, publish
 from fedora_messaging.exceptions import PublishReturned, ConnectionException
@@ -29,6 +30,7 @@ except ImportError:
     # Ansible v1 compat
     CallbackBase = object
 
+LOGGER = logging.getLogger(__name__)
 
 def getlogin():
     try:
@@ -59,18 +61,25 @@ class CallbackModule(CallbackBase):
                 return
 
             if not self.playbook_path:
-                msg = Message(
-                    topic="ansible.playbook.start",
-                    body=dict(
-                        playbook=path,
-                        userid=getlogin(),
-                        extra_vars=play.playbook.extra_vars,
-                        inventory=play.playbook.inventory.host_list,
-                        playbook_checksum=play.playbook.check,
-                        check=play.playbook.check,
-                    ),
-                )
-                publish(msg)
+                try:
+                    msg = Message(
+                        topic="ansible.playbook.start",
+                        body=dict(
+                            playbook=path,
+                            userid=getlogin(),
+                            extra_vars=play.playbook.extra_vars,
+                            inventory=play.playbook.inventory.host_list,
+                            playbook_checksum=play.playbook.check,
+                            check=play.playbook.check,
+                        ),
+                    )
+                    publish(msg)
+                except PublishReturned as e:
+                    LOGGER.warning(
+                        "Fedora Messaging broker rejected message %s: %s", msg.id, e
+                    )
+                except ConnectionException as e:
+                    LOGGER.warning("Error sending message %s: %s", msg.id, e)
                 self.playbook_path = path
 
     def playbook_on_stats(self, stats):
@@ -78,8 +87,15 @@ class CallbackModule(CallbackBase):
             return
 
         results = dict([(h, stats.summarize(h)) for h in stats.processed])
-        msg = Message(
-            topic="ansible.playbook.complete",
-            body=dict(playbook=self.playbook_path, userid=getlogin(), results=results),
-        )
-        publish(msg)
+        try:
+            msg = Message(
+                topic="ansible.playbook.complete",
+                body=dict(
+                    playbook=self.playbook_path, userid=getlogin(), results=results
+                ),
+            )
+            publish(msg)
+        except PublishReturned as e:
+            LOGGER.warning("Fedora Messaging broker rejected message %s: %s", msg.id, e)
+        except ConnectionException as e:
+            LOGGER.warning("Error sending message %s: %s", msg.id, e)
diff --git a/callback_plugins/fedora_messaging_callback2.py b/callback_plugins/fedora_messaging_callback2.py
index 3d1e357ac..0b08d6b36 100644
--- a/callback_plugins/fedora_messaging_callback2.py
+++ b/callback_plugins/fedora_messaging_callback2.py
@@ -19,6 +19,7 @@
 
 import os
 import pwd
+import logging
 
 from fedora_messaging.api import Message, publish
 from fedora_messaging.exceptions import PublishReturned, ConnectionException
@@ -34,6 +35,7 @@ try:
 except ImportError:
     from ansible.utils import md5 as secure_hash
 
+LOGGER = logging.getLogger(__name__)
 
 def getlogin():
     try:
@@ -77,18 +79,25 @@ class CallbackModule(CallbackBase):
                 return
 
             if not self.playbook_path:
-                msg = Message(
-                    topic="ansible.playbook.start",
-                    body=dict(
-                        playbook=path,
-                        userid=getlogin(),
-                        extra_vars=play._variable_manager.extra_vars,
-                        inventory=play._variable_manager._inventory._sources,
-                        playbook_checksum=secure_hash(path),
-                        check=self.play_context.check_mode,
-                    ),
-                )
-                publish(msg)
+                try:
+                    msg = Message(
+                        topic="ansible.playbook.start",
+                        body=dict(
+                            playbook=path,
+                            userid=getlogin(),
+                            extra_vars=play._variable_manager.extra_vars,
+                            inventory=play._variable_manager._inventory._sources,
+                            playbook_checksum=secure_hash(path),
+                            check=self.play_context.check_mode,
+                        ),
+                    )
+                    publish(msg)
+                except PublishReturned as e:
+                    LOGGER.warning(
+                        "Fedora Messaging broker rejected message %s: %s", msg.id, e
+                    )
+                except ConnectionException as e:
+                    LOGGER.warning("Error sending message %s: %s", msg.id, e)
                 self.playbook_path = path
 
     def v2_playbook_on_stats(self, stats):
@@ -96,8 +105,15 @@ class CallbackModule(CallbackBase):
             return
 
         results = dict([(h, stats.summarize(h)) for h in stats.processed])
-        msg = Message(
-            topic="ansible.playbook.complete",
-            body=dict(playbook=self.playbook_path, userid=getlogin(), results=results),
-        )
-        publish(msg)
+        try:
+            msg = Message(
+                topic="ansible.playbook.complete",
+                body=dict(
+                    playbook=self.playbook_path, userid=getlogin(), results=results
+                ),
+            )
+            publish(msg)
+        except PublishReturned as e:
+            LOGGER.warning("Fedora Messaging broker rejected message %s: %s", msg.id, e)
+        except ConnectionException as e:
+            LOGGER.warning("Error sending message %s: %s", msg.id, e)
-- 
2.21.0

