From 03f3a016d0bb2b3245d7e400b5589215f27afbe1 Mon Sep 17 00:00:00 2001
From: Tanner Prestegard <tanner.prestegard@ligo.org>
Date: Wed, 17 Oct 2018 09:13:31 -0500
Subject: [PATCH] Update failover to lvalert_send

Failover to lvalert_send now uses new LVALert Python API, rather
than forking a Process to call the lvalert_send script.
---
 gracedb/alerts/lvalert.py | 17 +++++++++++++++++
 gracedb/alerts/xmpp.py    | 12 ++++++------
 2 files changed, 23 insertions(+), 6 deletions(-)

diff --git a/gracedb/alerts/lvalert.py b/gracedb/alerts/lvalert.py
index 7e3166301..fb9256fd7 100644
--- a/gracedb/alerts/lvalert.py
+++ b/gracedb/alerts/lvalert.py
@@ -2,6 +2,8 @@ import logging
 from multiprocessing import Process
 import os
 from subprocess import Popen, PIPE
+
+from ligo.lvalert import LVAlertClient
 from ligo.overseer.overseer_client import send_to_overseer
 
 # Set up logger
@@ -31,6 +33,21 @@ def send_with_lvalert_overseer(node_name, message, manager, port):
     return True if rdict.get('success', None) is not None else False
 
 
+def send_with_lvalert_client(node, message, server):
+
+    # Instantiate client
+    client = LVAlertClient(server=server)
+
+    # Client setup
+    client.connect(reattempt=False)
+    client.auto_reconnect = False
+    client.process(block=False)
+
+    # Send message
+    client.publish(node, message)
+
+
+# OLD
 def send_with_lvalert_send(node, message, server):
 
     # Set up environment for running lvalert_send executable
diff --git a/gracedb/alerts/xmpp.py b/gracedb/alerts/xmpp.py
index cb136a708..9dac600c0 100644
--- a/gracedb/alerts/xmpp.py
+++ b/gracedb/alerts/xmpp.py
@@ -13,7 +13,7 @@ from events.permission_utils import is_external
 from events.query import filter_for_labels
 from events.shortcuts import is_event
 from superevents.shortcuts import is_superevent
-from .lvalert import send_with_lvalert_overseer, send_with_lvalert_send
+from .lvalert import send_with_lvalert_overseer, send_with_lvalert_client
 
 # Set up logger
 logger = logging.getLogger(__name__)
@@ -129,11 +129,11 @@ def issue_xmpp_alerts(event_or_superevent, alert_type, serialized_object,
                         "LVAlert Overseer failed, trying lvalert_send"))
 
             # If not using LVAlert Overseer or if sending with overseer failed,
-            # use basic lvalert_send executable (gross)
+            # use basic lvalert-client send
             if (not settings.USE_LVALERT_OVERSEER) or (not success):
-                success, err = send_with_lvalert_send(node_name, msg, server)
-
-                if not success:
+                try:
+                    send_with_lvalert_client(node_name, msg, server)
+                except Exception as e:
                     logger.critical(("issue_xmpp_alerts: error sending "
-                        "message with lvalert_send: {e}").format(e=err))
+                        "message with lvalert client: {e}").format(e=e))
 
-- 
GitLab