3604df
From 08b8a1b7ea47b13c7e82178adc1f0de4ade1f9ee Mon Sep 17 00:00:00 2001
3604df
From: Aravinda VK <avishwan@redhat.com>
3604df
Date: Wed, 26 Oct 2016 15:51:17 +0530
3604df
Subject: [PATCH 204/206] eventsapi: Auto reload Webhooks data when modified
3604df
3604df
glustereventsd depends on reload signal to reload the
3604df
Webhooks configurations. But if reload signal missed, no
3604df
events will be sent to newly added Webhook.
3604df
3604df
Added auto reload based on webhooks file mtime. Before pushing
3604df
events to Webhooks, reloads webhooks configurations if previously
3604df
recorded mtime is different than current mtime.
3604df
3604df
> Reviewed-on: http://review.gluster.org/15731
3604df
> Reviewed-by: Prashanth Pai <ppai@redhat.com>
3604df
> Smoke: Gluster Build System <jenkins@build.gluster.org>
3604df
> NetBSD-regression: NetBSD Build System <jenkins@build.gluster.org>
3604df
> CentOS-regression: Gluster Build System <jenkins@build.gluster.org>
3604df
3604df
BUG: 1384316
3604df
Change-Id: I83a41d6a52d8fa1d70e88294298f4a5c396d4158
3604df
Signed-off-by: Aravinda VK <avishwan@redhat.com>
3604df
Reviewed-on: https://code.engineering.redhat.com/gerrit/91515
3604df
Reviewed-by: Atin Mukherjee <amukherj@redhat.com>
3604df
---
3604df
 events/src/utils.py | 21 ++++++++++++++++++++-
3604df
 1 file changed, 20 insertions(+), 1 deletion(-)
3604df
3604df
diff --git a/events/src/utils.py b/events/src/utils.py
3604df
index dadd9ae..2e7bf0e 100644
3604df
--- a/events/src/utils.py
3604df
+++ b/events/src/utils.py
3604df
@@ -25,6 +25,7 @@ import eventtypes
3604df
 
3604df
 # Webhooks list
3604df
 _webhooks = {}
3604df
+_webhooks_file_mtime = 0
3604df
 # Default Log Level
3604df
 _log_level = "INFO"
3604df
 # Config Object
3604df
@@ -109,10 +110,12 @@ def load_webhooks():
3604df
     Load/Reload the webhooks list. This function will
3604df
     be triggered during init and when SIGUSR2.
3604df
     """
3604df
-    global _webhooks
3604df
+    global _webhooks, _webhooks_file_mtime
3604df
     _webhooks = {}
3604df
     if os.path.exists(WEBHOOKS_FILE):
3604df
         _webhooks = json.load(open(WEBHOOKS_FILE))
3604df
+        st = os.lstat(WEBHOOKS_FILE)
3604df
+        _webhooks_file_mtime = st.st_mtime
3604df
 
3604df
 
3604df
 def load_all():
3604df
@@ -130,6 +133,8 @@ def publish(ts, event_key, data):
3604df
     if NodeID is None:
3604df
         NodeID = get_node_uuid()
3604df
 
3604df
+    autoload_webhooks()
3604df
+
3604df
     message = {
3604df
         "nodeid": NodeID,
3604df
         "ts": int(ts),
3604df
@@ -143,6 +148,20 @@ def publish(ts, event_key, data):
3604df
         pass
3604df
 
3604df
 
3604df
+def autoload_webhooks():
3604df
+    global _webhooks_file_mtime
3604df
+    try:
3604df
+        st = os.lstat(WEBHOOKS_FILE)
3604df
+    except OSError:
3604df
+        st = None
3604df
+
3604df
+    if st is not None:
3604df
+        # If Stat is available and mtime is not matching with
3604df
+        # previously recorded mtime, reload the webhooks file
3604df
+        if st.st_mtime != _webhooks_file_mtime:
3604df
+            load_webhooks()
3604df
+
3604df
+
3604df
 def plugin_webhook(message):
3604df
     # Import requests here since not used in any other place
3604df
     import requests
3604df
-- 
3604df
2.9.3
3604df